{"id":18,"date":"2022-11-19T10:20:02","date_gmt":"2022-11-19T02:20:02","guid":{"rendered":"https:\/\/zqy.ac.cn\/?p=18"},"modified":"2022-11-24T08:36:38","modified_gmt":"2022-11-24T00:36:38","slug":"dijkstra%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/acosx.top\/?p=18","title":{"rendered":"Dijkstra\u7b97\u6cd5"},"content":{"rendered":"<h1>\u4ecb\u7ecd<\/h1>\n<p>Dijkstra\u7b97\u6cd5\u662f\u4e00\u4e2a\u5355\u6e90\u6700\u77ed\u8def\u7b97\u6cd5\u3002<\/p>\n<h1>\u7b97\u6cd5\u6d41\u7a0b\uff1a<\/h1>\n<p>1.\u5b9a\u4e49\u6570\u7ec4 $$???$$\uff0c\u4ee3\u8868\u5230\u76ee\u524d\u4e3a\u6b62\u4ece\u8d77\u70b9\u5230\u5404\u4e2a\u70b9\u7684\u6700\u77ed\u8def\u5f84\u957f\u5ea6\u3002\u521d\u59cb\u65f6\u5c06 $$ ???_{s} $$ \u8bbe\u7f6e\u4e3a 0\uff0c\u5176\u4ed6\u70b9\u7684\u8bbe\u7f6e\u4e3a $$+\\infty$$\u3002<br \/>\n2.\u4ece\u6240\u6709\u6ca1\u6709\u9009\u62e9\u8fc7\u7684\u70b9\u4e2d\u9009\u62e9\u4e00\u4e2a $$???$$ \u503c\u6700\u5c0f\u7684\u70b9\uff0c\u8bbe\u4e3a$$u$$\u3002<br \/>\n3.\u5bf9\u4e8e\u4ece $$?$$ \u51fa\u53d1\u7684\u6bcf\u4e00\u6761\u8fb9 $$(?, ?, ?)$$\uff0c\u8fdb\u884c\u4e00\u6b21\u677e\u5f1b\u64cd\u4f5c\u3002\u677e\u5f1b\u6307\u7684\u662f\u66f4\u65b0 $$???? = \\min(????, ???? + ?)$$\u3002<br \/>\n4.\u5c06\u70b9$$?$$\u6807\u8bb0\u4e3a\u5df2\u7ecf\u88ab\u9009\u62e9\u8fc7\uff0c\u56de\u5230\u6b65\u9aa4 2\uff0c\u76f4\u5230\u4e0d\u5b58\u5728\u4e00\u6761\u4ece\u5df2\u7ecf\u9009\u62e9\u8fc7\u7684\u70b9\u51fa\u53d1\u5230\u8fbe\u672a\u9009\u62e9\u8fc7\u7684\u70b9\u7684\u8fb9\u3002<\/p>\n<h1>\u539f\u7248<\/h1>\n<pre><code class=\"language-cpp\">#include&lt;bits\/stdc++.h&gt;\nusing namespace std;\n\nint mp[1000][1000]; \/\/ \u5b58\u56fe\nint dis[10000],vis[10000]; \/\/ \u6700\u77ed\u8ddd\u79bb\u548c\u662f\u5426\u8bbf\u95ee\u8fc7\n\nvoid dijkstra(int n,int s){\n    for(int i = 1;i &lt;= n;i++)dis[i] = INT_MAX,vis[i] = false; \/\/ \u521d\u59cb\u5168\u8d4b\u503c\u4e3a\u6700\u5927\u503c\uff0c\u672a\u7ecf\u8bbf\u95ee\n    dis[s] = 0;\/\/ \u8d77\u70b9\u8ddd\u79bb\u4e3a\u96f6\n    while (1)\n    {\n        int u = 0,val = INT_MAX; \/\/ \u4ece\u8d77\u70b9\u5f00\u59cb\uff0c\u5bfb\u627e\u6700\u5c0fval\n        for(int j = 1;j &lt;= n;j++){\n            if(!vis[j] &amp;&amp; dis[j] &lt; val)u = j,val = dis[j]; \/\/ \u5982\u679c\u672a\u8bbf\u95ee\u4e14val\u5c0f\u4e8e\u6700\u5c0fval,\u66f4\u65b0u\u548cval\n        }\n\n        if(u == 0)break;\n        vis[u] = true; \/\/ \u6253\u6807\u8bb0\n        for(int j = 0;j &lt; 10000;j++)\n            if(mp[u][j])dis[j] = min(dis[j],dis[u]+mp[u][j]);\n    }\n\n}\n\nint main(){\n    int n,m,s,u,v,w;\n    cin&gt;&gt;n&gt;&gt;m&gt;&gt;s;\n    for(int i = 0;i &lt;= m;i++){\n        cin&gt;&gt;u&gt;&gt;v&gt;&gt;w;\n        mp[u][v] = w;\n\n    }\n    dijkstra(n,s);\n    for(int i = 1;i &lt;= n;i++)cout&lt;&lt;dis[i]&lt;&lt;&quot; &quot;;\n}<\/code><\/pre>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong>\uff1a$$O(n^2)$$<\/p>\n<h1>\u4f18\u5148\u961f\u5217\u4f18\u5316<\/h1>\n<pre><code class=\"language-cpp\">#include&lt;bits\/stdc++.h&gt;\nusing namespace std;\nconst int maxn = 20005;\nstruct edge{\n    int v,w;\n};\n\nstruct node{ \/\/ \u5b58\u50a8\u6bcf\u4e2adis\u548c\u5bf9\u5e94\u7684u\n    int dis,u;\n    bool operator&lt;(const node &amp;a)const {return dis &gt; a.dis;}\n};\nvector&lt;edge&gt; e[maxn]; \/\/ \u5b58\u8fb9\nint dis[maxn],vis[maxn]; \/\/ \u8ddd\u79bb\u548c\u662f\u5426\u8bbf\u95ee\u8fc7\npriority_queue&lt;node&gt; pq; \/\/ \u5b58\u50a8dis\u7684\u961f\u5217\nvoid dijkstra(int n,int s){\n    for(int i = 1;i &lt;= n;i++) dis[i] = INT_MAX,vis[i] = false; \/\/ \u521d\u59cb\u5316\n    dis[s] = 0; \/\/ \u8d77\u70b9\u8ddd\u79bb\u4e3a\u96f6\n    pq.push((node){0,s}); \/\/ \u8d77\u70b9\u52a0\u5165\u961f\u5217\n    while(!pq.empty()){ \/\/ \u5f53\u961f\u5217\u4e0d\u4e3a\u7a7a\u65f6\n        int u = pq.top().u;pq.pop(); \/\/ \u53d6\u51fa\u961f\u9996\u4e3au\n        if(vis[u])continue; \/\/ \u5982u\u5df2\u88ab\u8bbf\u95ee\u8fc7\u5219\u4e0b\u4e00\u4e2a\n        vis[u] = true; \/\/ \u6253\u8bbf\u95ee\u6807\u8bb0\n        for(int j = 0;j &lt; e[u].size();j++){\n            edge ed = e[u][j];int v = ed.v,w = ed.w;\n            if(dis[v] &gt; dis[u] + w)dis[v] = dis[u] + w,pq.push((node){dis[v],v}); \/\/ \u66f4\u65b0dis\u5e76\u52a0\u5165\u961f\u5217\n        }\n    }\n}\n\nint main(){\n    int n,m,s,u,v,w;\n    cin&gt;&gt;n&gt;&gt;m&gt;&gt;s; \/\/ n \u4e2a\u70b9\uff0cm\u6761\u8fb9\uff0c\u8d77\u70b9\u4e3as\n    for(int i = 0;i &lt; m;i++){\n        cin&gt;&gt;u&gt;&gt;v&gt;&gt;w; \/\/ \u6709\u8fb9 u -&gt; v \uff0c\u4ef7\u503cw\n        edge ed;\n        ed.v = v;\n        ed.w = w;\n        e[u].push_back(ed); \/\/ \u5c06\u8fb9\u52a0\u5165\u6570\u7ec4\n    }\n    dijkstra(n,s);\n    for(int i = 1;i &lt;= n;i++)cout&lt;&lt;dis[i]&lt;&lt;&quot; &quot;; \/\/ \u8f93\u51fa\n    return 0;\n}<\/code><\/pre>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/strong>$$ O(m\\ log\\ m) $$<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7ecd Dijkstra\u7b97\u6cd5\u662f\u4e00\u4e2a\u5355\u6e90\u6700\u77ed\u8def\u7b97\u6cd5\u3002 \u7b97\u6cd5\u6d41\u7a0b\uff1a 1.\u5b9a\u4e49\u6570\u7ec4 $$???$$\uff0c\u4ee3\u8868\u5230\u76ee\u524d\u4e3a\u6b62\u4ece\u8d77&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/acosx.top\/index.php?rest_route=\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/acosx.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/acosx.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/acosx.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/acosx.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":0,"href":"https:\/\/acosx.top\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"https:\/\/acosx.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/acosx.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/acosx.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}