{"id":69110,"date":"2026-01-31T05:32:48","date_gmt":"2026-01-30T21:32:48","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/69110.html"},"modified":"2026-01-31T05:32:48","modified_gmt":"2026-01-30T21:32:48","slug":"%e3%80%90%e5%9b%be%e8%ae%ba%e3%80%91%e6%9c%80%e7%9f%ad%e8%b7%af%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/69110.html","title":{"rendered":"\u3010\u56fe\u8bba\u3011\u6700\u77ed\u8def\u95ee\u9898"},"content":{"rendered":"<h2>\u5355\u6e90\u6700\u77ed\u8def\u95ee\u9898<\/h2>\n<p>\u5355\u6e90\u6700\u77ed\u8def\u95ee\u9898\u662f\u6307\u56fa\u5b9a\u4e00\u4e2a\u8d77\u70b9&#xff0c;\u6c42\u5b83\u5230\u5176\u4ed6\u4efb\u610f\u4e00\u70b9\u7684\u6700\u77ed\u8def\u95ee\u9898\u3002<\/p>\n<h3>Dijkstra\u7b97\u6cd5&#xff08;\u8fea\u514b\u65af\u62c9\u7279\u7b97\u6cd5&#xff09;<\/h3>\n<p>\u9996\u5148\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u8fd9\u4e2a\u7b97\u6cd5\u4ec5\u4ec5\u7528\u4e8e\u56fe\u4e2d\u6ca1\u6709\u8d1f\u8fb9\u7684\u60c5\u51b5\u3002<\/p>\n<h4>\u539f\u7406<\/h4>\n<p>Dijkstra\u672c\u8d28\u903b\u8f91\u5176\u5b9e\u662f\u52a8\u6001\u89c4\u5212&#xff0c;\u6211\u4eec\u8981\u6c42\u4efb\u610f\u70b9i\u5230\u8d77\u70b9&#xff08;\u5047\u8bbe\u4e3a1&#xff09;\u7684\u8ddd\u79bb&#xff0c;\u6211\u4eec\u4e0d\u59a8\u7528\u4e00\u4e2a\u6570\u7ec4dist\u8bb0\u5f55\u8fd9\u4e00\u8ddd\u79bb&#xff0c;\u5373dist[i]\u4e3a1-&gt;i\u7684\u6700\u77ed\u8def\u8ddd\u79bb&#xff0c;\u6240\u4ee5\u73b0\u5728\u6211\u4eec\u9700\u8981\u8003\u8651\u7684\u95ee\u9898\u5c31\u662f\u5982\u4f55\u66f4\u65b0\u5e76\u6c42\u5f97dist&#xff0c;\u8fd9\u4e00\u90e8\u5206\u5c31\u662f\u7b97\u6cd5\u7684\u91cd\u70b9\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u56fe\u7684\u70b9\u5206\u4e3a\u4e24\u4e2a\u96c6\u5408&#xff0c;\u96c6\u5408A\u4e3a\u6700\u77ed\u8def\u5f84\u5df2\u7ecf\u786e\u5b9a\u7684\u70b9&#xff0c;\u96c6\u5408B\u4e3a\u6700\u77ed\u8def\u5f84\u5c1a\u672a\u786e\u5b9a\u7684\u70b9\u3002<\/p>\n<p>\u6bcf\u4e00\u6b21\u6211\u4eec\u4eceB\u4e2d\u9009\u53d6dist\u6700\u5c0f\u7684\u70b9&#xff08;\u8bbe\u4e3ax&#xff09;&#xff0c;\u5c06\u4ed6\u8f6c\u79fb\u5230A\u4e2d&#xff0c;\u540c\u65f6\u6211\u4eec\u5229\u7528x\u7684dist\u6765\u66f4\u65b0x\u7684\u90bb\u5c45&#xff08;\u8bbe\u4e3ap&#xff09;\u7684dist&#xff0c;\u5373<\/p>\n<p>dist[p]&#061;min(dist[p],dist[x]&#043;e(x,p))\/\/e(x,p)\u4e3a\u8fb9xp\u7684\u6743\u503c<\/p>\n<p>\u901a\u8fc7\u53cd\u590d\u8fdb\u884cB-&gt;A\u64cd\u4f5c&#xff0c;\u6700\u7ec8\u5f97\u5230\u5168\u90e8\u7684dist&#xff0c;\u6ca1\u770b\u61c2\u6ca1\u5173\u7cfb&#xff0c;\u6211\u4eec\u7528\u56fe\u6765\u6f14\u793a\u4e00\u904d\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"364\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130213246-697d237eee621.png\" width=\"626\" \/><\/p>\n<p>\u8fd9\u662f\u56fe\u7684\u521d\u59cb\u72b6\u6001&#xff0c;dist\u7b80\u5199\u4e3ad&#xff0c;\u6839\u636e\u5b9a\u4e49\u6211\u4eec\u53ef\u5f97\u8d77\u70b9\u7684d&#061;0&#xff0c;\u800c\u5176\u4ed6\u7684dist\u6211\u4eec\u5168\u90e8\u8bbe\u4e3a\u4e00\u4e2a\u6781\u5927\u7684\u6570&#xff0c;\u65b9\u4fbf\u540e\u7eed\u66f4\u65b0&#xff0c;\u6b64\u65f6\u6211\u4eec\u9009\u53d61\u5e76\u5165A&#xff0c;\u540c\u65f6\u66f4\u65b0\u90bb\u5c45\u7684d\u503c<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"347\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130213247-697d237f2f215.png\" width=\"628\" \/><\/p>\n<p>\u7ea2\u8272\u4e3a\u8fd9\u4e00\u6b21\u53d1\u751f\u4e86\u66f4\u65b0\u7684d&#xff0c;\u9ec4\u8272\u4e3a\u5df2\u7ecf\u56fa\u5b9a\u7684d&#xff0c;\u4e5f\u5c31\u662f\u96c6\u5408A\u4e2d\u7684\u5143\u7d20\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5728B\u4e2d\u9009\u62e9min\u8fdb\u884c\u66f4\u65b0&#xff0c;\u663e\u7136\u8fd9\u6b21\u6211\u4eec\u5e94\u8be5\u9009\u62e92&#xff0c;\u6211\u4eec\u4f1a\u8fdb\u884c\u5982\u4e0b\u66f4\u65b0<\/p>\n<p>d[3]:5-&gt;5(d[2]&#043;4&gt;5)<\/p>\n<p>d[4]:\u221e-&gt;d[2]&#043;4&#061;6<\/p>\n<p>d[5]:\u221e-&gt;d[2]&#043;10&#061;12<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"339\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130213247-697d237f716ea.png\" width=\"623\" \/><\/p>\n<p>\u9009\u62e9\u6700\u5c0f\u7684d[3]<\/p>\n<p>d[4]:6-&gt;6(d[3]&#043;2&gt;6)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"334\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130213247-697d237fb556a.png\" width=\"633\" \/><\/p>\n<p>\u4e0d\u65ad\u91cd\u590d\u8fd9\u4e00\u64cd\u4f5c&#xff0c;\u6700\u7ec8\u4f7f\u5f97\u5168\u90e8d\u53d8\u6210\u9ec4\u8272&#xff0c;\u5373\u5168\u90e8\u5e76\u5165\u96c6\u5408A<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"333\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130213247-697d237fd0612.png\" width=\"622\" \/><\/p>\n<h4>\u6734\u7d20\u5b9e\u73b0<\/h4>\n<p>const int INF&#061;0x3f3f3f3f;<br \/>\nconst int N&#061;1e3&#043;7;\/\/\u4e0a\u9650<br \/>\nstruct edge{<br \/>\n    int to,w;<br \/>\n    edge(int x,int y){to&#061;x;w&#061;y;}<br \/>\n};<br \/>\nvector&lt;vector&lt;edge&gt;&gt; g(N,vector&lt;edge&gt;());<br \/>\n\/\/\u90bb\u63a5\u8868\u5b58\u50a8\u56fe&#xff0c;to\u4e3a\u8fb9\u7684\u7ec8\u70b9\u7aef&#xff0c;w\u4e3a\u6743\u503c<br \/>\nbool vis[N];<br \/>\nint dist[N];<br \/>\nvoid dijkstra(int n,vector&lt;vector&lt;edge&gt;&gt;&amp; g){<br \/>\n    \/\/n\u4e3a\u56fe\u7684\u70b9\u6570,N&gt;n<br \/>\n    for(int i&#061;1;i&lt;&#061;n;i&#043;&#043;){<br \/>\n        dist[i]&#061;INF;<br \/>\n        vis[i]&#061;false;<br \/>\n    }<br \/>\n    dist[1]&#061;0;<br \/>\n    \/\/\u521d\u59cb\u5316<br \/>\n    while(true){<br \/>\n        int v&#061;-1;<br \/>\n        for(int i&#061;1;i&lt;&#061;n;i&#043;&#043;){<br \/>\n            if(vis[i]) continue;<br \/>\n            if(v&#061;&#061;-1||dist[i]&lt;dist[v]) v&#061;i;<br \/>\n        }<br \/>\n        if(v&#061;&#061;-1) break;<br \/>\n        \/\/\u5982\u679c\u4e3a-1\u4ee3\u8868\u5168\u90e8\u90fd\u88ab\u6807\u8bb0&#xff0c;\u5373B\u96c6\u5408\u4e3a\u7a7a<br \/>\n        vis[v]&#061;true;<br \/>\n        for(auto p:g[v])\/\/\u66f4\u65b0\u64cd\u4f5c<br \/>\n            dist[p.to]&#061;min(dist[p.to],dist[v]&#043;p.w);<br \/>\n    }<br \/>\n} <\/p>\n<p>\u6ce8\u610f\u6b64\u5904\u8d77\u70b9\u8bbe\u4e3a1&#xff0c;\u5b9e\u9645\u64cd\u4f5c\u65f6\u6211\u4eec\u53ef\u4ee5\u6309\u7167\u8981\u6c42\u4fee\u6539\u8d77\u70b9\u3002<\/p>\n<p>\u6700\u7ec8\u5b9e\u73b0\u590d\u6742\u5ea6\u4e3aO(V\u00b2)<\/p>\n<h4>\u5806\u4f18\u5316<\/h4>\n<p>\u53ef\u4ee5\u53d1\u73b0&#xff0c;\u590d\u6742\u5ea6\u6765\u6e90\u4e3b\u8981\u662f\u6700\u5c0fdist\u7684\u9009\u53d6\u4ee5\u53ca\u90bb\u5c45\u7684\u66f4\u65b0&#xff0c;\u90bb\u5c45\u66f4\u65b0\u6211\u4eec\u65e0\u6cd5\u7ee7\u7eed\u4f18\u5316&#xff0c;\u4f46\u662f\u6700\u5c0fdist\u7684\u9009\u53d6\u6211\u4eec\u53ef\u4ee5\u7528\u6700\u5c0f\u5806\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<p>\u6700\u5c0f\u5806\u6211\u4eec\u9009\u7528STL\u4e2d\u4f18\u5148\u961f\u5217\u3002<\/p>\n<p>typedef pair&lt;int,int&gt; P;<br \/>\nconst int INF&#061;0x3f3f3f3f;<br \/>\nconst int N&#061;1e3&#043;7;\/\/\u4e0a\u9650<br \/>\nstruct edge{<br \/>\n    int to,w;<br \/>\n    edge(int x,int y){to&#061;x;w&#061;y;}<br \/>\n};<br \/>\nvector&lt;vector&lt;edge&gt;&gt; g(N,vector&lt;edge&gt;());<br \/>\n\/\/\u90bb\u63a5\u8868\u5b58\u50a8\u56fe&#xff0c;to\u4e3a\u8fb9\u7684\u7ec8\u70b9\u7aef&#xff0c;w\u4e3a\u6743\u503c<br \/>\nbool vis[N];<br \/>\nint dist[N];<br \/>\nvoid dijkstra(int n,vector&lt;vector&lt;edge&gt;&gt;&amp; g){<br \/>\n    \/\/n\u4e3a\u56fe\u7684\u70b9\u6570,N&gt;n<br \/>\n    priority_queue&lt; P,vector&lt;P&gt;,greater&lt;P&gt; &gt; q;<br \/>\n    \/\/first\u662f\u6700\u77ed\u8ddd\u79bb&#xff0c;sceond\u662f\u70b9\u7684\u7f16\u53f7<br \/>\n    for(int i&#061;1;i&lt;&#061;n;i&#043;&#043;)<br \/>\n        dist[i]&#061;INF;<br \/>\n    dist[1]&#061;0;<br \/>\n    q.push(P(0,1));<br \/>\n    \/\/\u521d\u59cb\u5316<br \/>\n    while(!q.empty()){<br \/>\n        P x&#061;q.top();q.pop();<br \/>\n        int v&#061;x.second;<br \/>\n        if(dist[v]&lt;x.first) continue;<br \/>\n\/\/\u5982\u679cx\u7684\u8ddd\u79bb\u5927\u4e8edist[v]\u90a3\u5c31\u4e0d\u80fd\u7528\u5b83\u66f4\u65b0\u90bb\u5c45<br \/>\n        for(auto p:g[v]){<br \/>\n            if(dist[p.to]&gt;dist[v]&#043;p.w){<br \/>\n                dist[p.to]&#061;dist[v]&#043;p.w;<br \/>\n                q.push(P(dist[p.to],p.to));<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<p>\u5176\u5b9e\u53ef\u4ee5\u53d1\u73b0&#xff0c;\u6211\u4eec\u5728\u8fd9\u91cc\u5220\u9664\u4e86vis\u6570\u7ec4&#xff0c;\u5b9e\u9645\u4e0a&#xff0c;\u5165\u961f\u64cd\u4f5c\u5b9e\u73b0\u4e86\u8fd9\u4e00\u4f5c\u7528&#xff0c;\u5728\u66f4\u65b0\u8fc7\u7a0b\u4e2d\u6211\u4eec\u5c06\u53d1\u751f\u66f4\u65b0\u7684\u5143\u7d20\u5165\u961f&#xff0c;\u4e5f\u5c31\u662f\u4e0a\u6587\u56fe\u4e2d\u7ea2\u8272\u5143\u7d20&#xff0c;\u800c\u5df2\u7ecf\u56fa\u5b9a\u7684\u9ec4\u8272\u5143\u7d20\u4e0d\u4f1a\u53d1\u751f\u66f4\u65b0\u5219\u88ab\u5254\u9664&#xff0c;\u501f\u6b64\u5b9e\u73b0\u4e86vis\u6570\u7ec4\u7684\u529f\u80fd\u3002<\/p>\n<p>\u6700\u7ec8\u5b9e\u73b0O(ElogV)\u3002<\/p>\n<h3>Bellman-Ford\u7b97\u6cd5&#xff08;\u8d1d\u5c14\u66fc\u798f\u7279\u7b97\u6cd5&#xff09;<\/h3>\n<p>\u76f8\u6bd4\u4e8e\u8fea\u514b\u65af\u62c9\u7279\u7b97\u6cd5&#xff0c;\u8fd9\u4e00\u7b97\u6cd5\u53ef\u4ee5\u5904\u7406\u8d1f\u8fb9\u7684\u60c5\u51b5\u3002<\/p>\n<h4>\u539f\u7406<\/h4>\n<h5>\u57fa\u672c\u539f\u7406<\/h5>\n<p>\u8fd9\u4e2a\u539f\u7406\u7279\u522b\u7b80\u5355&#xff0c;\u6211\u4eec\u7ef4\u62a4\u4e00\u4e2a\u6570\u7ec4d&#xff0c;d[i]\u4ee3\u8868\u4e86\u4ece\u8d77\u70b9s\u5230\u70b9i\u7684\u6700\u77ed\u8def\u5f84&#xff0c;\u90a3\u4e48\u6211\u4eec\u5f97\u5230<\/p>\n<p>d[i]&#061;min{d[j]&#043;e(j,i)}\/\/e(j,i)\u4e3aj\u5230i\u7684\u6743\u503c<\/p>\n<p>\u5982\u679c\u662fDAG&#xff08;\u6709\u5411\u65e0\u73af\u56fe&#xff09;\u6211\u4eec\u5c31\u53ef\u4ee5\u6309\u7167\u62d3\u6251\u5e8f\u904d\u5386\u3002\u4f46\u5982\u679c\u6709\u73af&#xff0c;\u5982\u679c\u4e0d\u662f\u8d1f\u73af&#xff0c;\u8fd9\u4e5f\u5e76\u4e0d\u5f71\u54cd\u7b97\u6cd5\u8fd0\u4f5c&#xff0c;\u5982\u679c\u6709\u8d1f\u73af\u8fd9\u5c31\u662f\u6211\u4eec\u9700\u8981\u7740\u91cd\u8003\u8651\u7684\u90e8\u5206\u4e86\u3002<\/p>\n<p>\u8d1f\u73af&#xff1a;\u73af\u4e0a\u6240\u6709\u7684\u8fb9\u6743\u548c\u4e3a\u8d1f\u6570\u3002<\/p>\n<p>\u5982\u679c\u6709\u8d1f\u73af&#xff0c;\u6211\u4eec\u5c06\u4e00\u6761\u6700\u77ed\u8def\u5f84\u7b80\u5316\u6210\u4e09\u4e2a\u70b9<\/p>\n<p>\u8d77\u70b9&#8212;&#8212;&gt;\u8d1f\u73af&#8212;&#8212;&gt;\u7ec8\u70b9<\/p>\n<p>\u7531\u4e8e\u8d1f\u73af\u7684\u6743\u503c\u4e3a\u8d1f&#xff0c;\u6240\u4ee5\u8d77\u70b9\u5230\u7ec8\u70b9\u7684\u8ddd\u79bb\u53ef\u4ee5\u65e0\u9650\u5c0f&#xff0c;\u56e0\u4e3a\u6bcf\u8d70\u4e00\u6b21\u8d1f\u73af\u8ddd\u79bb\u90fd\u4f1a\u7f29\u77ed&#xff08;\u6709\u70b9\u795e\u5947\u4e0d\u662f\u5417&#xff09;&#xff0c;\u5b9e\u73b0\u8d77\u6765\u5c31\u662f<\/p>\n<p>\u8d77\u70b9&#8212;&#8212;&gt;\u8d1f\u73af\u73af\u73af\u73af\u73af\u73af&#8230;&#8230;..&#8212;&#8212;&gt;\u7ec8\u70b9<\/p>\n<h5>\u8d1f\u73af\u5904\u7406\u539f\u7406<\/h5>\n<p>\u6240\u4ee5\u6211\u4eec\u9700\u8981\u68c0\u6d4b\u662f\u5426\u5b58\u5728\u8d1f\u73af&#xff0c;\u6709\u8d1f\u73af\u5c31\u65e0\u89e3&#xff08;\u6700\u77ed\u8def\u53ef\u4ee5\u65e0\u7a77\u5c0f&#xff09;\u3002<\/p>\n<p>\u5bf9\u4e8e\u4efb\u610f\u6709V\u4e2a\u9876\u70b9\u7684\u56fe&#xff0c;\u8d77\u70b9\u5230\u7ec8\u70b9\u7684\u6700\u77ed\u8def\u6700\u591a\u7ecf\u8fc7&#xff08;v-1)\u6761\u8fb9&#xff0c;\u56e0\u4e3a\u6700\u77ed\u8def\u4e0d\u4f1a\u540c\u65f6\u7ecf\u8fc7\u540c\u4e00\u9876\u70b9\u4e24\u6b21&#xff0c;\u9664\u4e86\u4e00\u79cd\u60c5\u51b5&#xff0c;\u5c31\u662f\u8d1f\u73af\u3002\u56e0\u4e3a\u5bf9\u4e8e\u8d1f\u73af\u6765\u8bf4&#xff0c;\u7ecf\u8fc7\u540c\u4e00\u9876\u70b9\u7684\u4ee3\u4ef7\u662f\u8d1f\u7684\u3002\u6211\u4eec\u5c31\u53ef\u4ee5\u501f\u52a9\u8fd9\u4e00\u7279\u6027\u6765\u68c0\u6d4b\u8d1f\u73af\u3002<\/p>\n<h4>\u5b9e\u73b0<\/h4>\n<p>struct edge{<br \/>\n    int from,to,w;<br \/>\n};<br \/>\nvector&lt;edge&gt; g;\/\/\u6211\u4eec\u9009\u62e9\u8bb0\u5f55\u8fb9<br \/>\nint V,E,s;\/\/\u9876\u70b9\u6570&#xff0c;\u8fb9\u6570&#xff0c;\u8d77\u70b9\u6570<br \/>\nint d[MAXV];<br \/>\nbool check(){<br \/>\n    for(int i&#061;1;i&lt;&#061;V;i&#043;&#043;) d[i]&#061;0;\/\/\u521d\u59cb\u5316\u4e3a0&#xff0c;\u4e3a\u4e86\u5e94\u5bf9\u8d1f\u8fb9<br \/>\n    for(int i&#061;1;i&lt;&#061;V;i&#043;&#043;){<br \/>\n        for(int j&#061;1;j&lt;&#061;E;j&#043;&#043;){<br \/>\n            edge e&#061;g[j];<br \/>\n            if(d[e.to]&gt;d[e.from]&#043;e.w){<br \/>\n                d[e.to]&#061;d[e.from]&#043;e.w;<br \/>\n                if(i&#061;&#061;V) return false;<br \/>\n\/\/\u5982\u679c\u7b2cV\u6b21\u90fd\u66f4\u65b0\u4e86&#xff0c;\u5c31\u4ee3\u8868\u6709\u8d1f\u73af&#xff0c;\u56e0\u4e3a\u6b63\u5e38\u56fe\u6700\u591a\u66f4\u65b0V-1\u6b21<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    return true;<br \/>\n}<br \/>\nvoid Bellman(){<br \/>\n    for(int i&#061;1;i&lt;&#061;V;i&#043;&#043;) d[i]&#061;INF;<br \/>\n    d[s]&#061;0;<br \/>\n    while(true){<br \/>\n        bool update&#061;false;<br \/>\n        for(int i&#061;1;i&lt;&#061;E;i&#043;&#043;){<br \/>\n            edge e&#061;g[i];<br \/>\n            if(d[e.from]!&#061;INF&amp;&amp;d[e.to]&gt;d[e.from]&#043;e.w){<br \/>\n                d[e.to]&#061;d[e.from]&#043;e.w;<br \/>\n                update&#061;true;<br \/>\n            }<br \/>\n        }<br \/>\n        if(!update) break;<br \/>\n    }<br \/>\n} <\/p>\n<p>\u7531\u4e8ewhile\u6700\u591a\u5faa\u73af&#xff08;V-1&#xff09;\u6b21\u6240\u4ee5\u5b9e\u73b0\u4e3aO(V\u00b2)<\/p>\n<h2>\u4efb\u610f\u4e24\u70b9\u6700\u77ed\u8def\u95ee\u9898<\/h2>\n<h3>Floyd-Warshall\u7b97\u6cd5&#xff08;\u5f17\u6d1b\u4f0a\u5fb7-\u6c83\u820d\u5c14&#xff09;<\/h3>\n<p>\u5bf9\u4e8e\u4efb\u610f\u4e24\u70b9\u7684\u95ee\u9898&#xff0c;\u6211\u4eec\u7528Floyd\u7b97\u6cd5\u6765\u89e3\u51b3&#xff0c;\u672c\u8d28\u662f\u52a8\u6001\u89c4\u5212\u3002<\/p>\n<h4 style=\"background-color:transparent\">\u539f\u7406<\/h4>\n<p>\u6211\u4eec\u5b9a\u4e49\u72b6\u6001\u00a0 f[k][i][j]\u00a0\u00a0,\u8868\u793a\u4e3a\u5728\u53ea\u4f7f\u7528\u7f16\u53f71-k\u7684\u9876\u70b9\u5f53\u4f5c\u4e2d\u95f4\u8282\u70b9\u7684\u60c5\u51b5\u4e0b&#xff0c;i\u5230j\u7684\u6700\u77ed\u8def\u3002<\/p>\n<p>\u800cf[0][i][j]\u5219\u4ee3\u8868e(i,j)&#xff0c;\u6ca1\u6709\u5c31\u8bbe\u4e3a\u65e0\u7a77\u5927\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u5c31\u53ef\u4ee5\u628a\u53ea\u75281-k\u9876\u70b9\u7684\u95ee\u9898\u7ea6\u675f\u5230\u53ea\u75281-k-1\u9876\u70b9\u7684\u95ee\u9898\u4e2d<\/p>\n<p>\u5bf9\u4e8e\u9876\u70b9k&#xff0c;\u6211\u4eec\u6709\u4e24\u79cd\u60c5\u51b5&#xff0c;\u7ecf\u8fc7k&#xff0c;\u4e0d\u7ecf\u8fc7k&#xff0c;\u4e8e\u662f\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b<\/p>\n<p>f[k][i][j] &#061; min( f[k-1][i][j] , f[k-1][i][k] &#043; f[k-1][k][j] )<\/p>\n<p>\u5b9e\u9645\u64cd\u4f5c\u4e2d&#xff0c;\u7531\u4e8e\u7b2c\u4e00\u7ef4\u4e0d\u4f1a\u5f71\u54cd\u7b97\u6cd5\u8fdb\u884c&#xff0c;\u6211\u4eec\u53ef\u4ee5\u53bb\u6389\u7b2c\u4e00\u4f4d\u51cf\u5c0f\u7a7a\u95f4\u5f00\u9500\u3002<\/p>\n<p>\u800cFloyd\u5bf9\u4e8e\u8d1f\u73af\u5904\u7406\u6bd4\u8f83\u7b80\u5355&#xff0c;\u53ea\u9700\u8981\u68c0\u6d4be(i,j)\u662f\u5426\u5c0f\u4e8e0\u5373\u53ef&#xff0c;\u8fd9\u91cc\u4e0d\u518d\u5c55\u793a\u3002<\/p>\n<h4>\u5b9e\u73b0<\/h4>\n<p>struct edge{<br \/>\n    int from,to,w;<br \/>\n};<br \/>\nint n,E;\/\/\u70b9\u6570&#xff0c;\u8fb9\u6570<br \/>\nint f[N][N];<br \/>\nvoid init(){<br \/>\n    for(int i&#061;1;i&lt;&#061;n;i&#043;&#043;){<br \/>\n        for(int j&#061;1;j&lt;&#061;n;j&#043;&#043;){<br \/>\n            f[N][N]&#061;INF;<br \/>\n        }<br \/>\n    }<br \/>\n    for(int i&#061;1;i&lt;&#061;E;i&#043;&#043;){<br \/>\n        int u,v;cin&gt;&gt;u&gt;&gt;v;<br \/>\n        cin&gt;&gt;f[u][v];<br \/>\n    }<br \/>\n}<br \/>\nvoid Floyd(){<br \/>\n    for(int k&#061;1;k&lt;&#061;n;k&#043;&#043;){<br \/>\n        for(int i&#061;1;i&lt;&#061;n;i&#043;&#043;){<br \/>\n            for(int j&#061;1;j&lt;&#061;n;j&#043;&#043;){<br \/>\n                f[i][j]&#061;min(f[i][j],f[i][k]&#043;f[k][j]);<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<p>\u6700\u7ec8\u5b9e\u73b0O(n\u00b3)<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u6e90\u6700\u77ed\u8def\u95ee\u9898<br \/>\n\u5355\u6e90\u6700\u77ed\u8def\u95ee\u9898\u662f\u6307\u56fa\u5b9a\u4e00\u4e2a\u8d77\u70b9&#xff0c;\u6c42\u5b83\u5230\u5176\u4ed6\u4efb\u610f\u4e00\u70b9\u7684\u6700\u77ed\u8def\u95ee\u9898\u3002<br \/>\nDijkstra\u7b97\u6cd5&#xff08;\u8fea\u514b\u65af\u62c9\u7279\u7b97\u6cd5&#xff09;<br \/>\n\u9996\u5148\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u8fd9\u4e2a\u7b97\u6cd5\u4ec5\u4ec5\u7528\u4e8e\u56fe\u4e2d\u6ca1\u6709\u8d1f\u8fb9\u7684\u60c5\u51b5\u3002<br \/>\n\u539f\u7406<br \/>\nDijkstra\u672c\u8d28\u903b\u8f91\u5176\u5b9e\u662f\u52a8\u6001\u89c4\u5212&#xff0c;\u6211\u4eec\u8981\u6c42\u4efb\u610f\u70b9i\u5230\u8d77\u70b9&#xff08;\u5047\u8bbe\u4e3a1&#xff09;\u7684\u8ddd\u79bb&#xff0c;\u6211\u4eec\u4e0d\u59a8\u7528\u4e00\u4e2a\u6570\u7ec4dist\u8bb0\u5f55\u8fd9\u4e00\u8ddd\u79bb&#xff0c;\u5373dist[i]\u4e3a1-&gt;i\u7684\u6700\u77ed\u8def\u8ddd\u79bb&amp;<\/p>\n","protected":false},"author":2,"featured_media":69105,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1984,427],"topic":[],"class_list":["post-69110","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-1984","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u56fe\u8bba\u3011\u6700\u77ed\u8def\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/69110.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u56fe\u8bba\u3011\u6700\u77ed\u8def\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5355\u6e90\u6700\u77ed\u8def\u95ee\u9898 \u5355\u6e90\u6700\u77ed\u8def\u95ee\u9898\u662f\u6307\u56fa\u5b9a\u4e00\u4e2a\u8d77\u70b9&#xff0c;\u6c42\u5b83\u5230\u5176\u4ed6\u4efb\u610f\u4e00\u70b9\u7684\u6700\u77ed\u8def\u95ee\u9898\u3002 Dijkstra\u7b97\u6cd5&#xff08;\u8fea\u514b\u65af\u62c9\u7279\u7b97\u6cd5&#xff09; \u9996\u5148\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u8fd9\u4e2a\u7b97\u6cd5\u4ec5\u4ec5\u7528\u4e8e\u56fe\u4e2d\u6ca1\u6709\u8d1f\u8fb9\u7684\u60c5\u51b5\u3002 \u539f\u7406 Dijkstra\u672c\u8d28\u903b\u8f91\u5176\u5b9e\u662f\u52a8\u6001\u89c4\u5212&#xff0c;\u6211\u4eec\u8981\u6c42\u4efb\u610f\u70b9i\u5230\u8d77\u70b9&#xff08;\u5047\u8bbe\u4e3a1&#xff09;\u7684\u8ddd\u79bb&#xff0c;\u6211\u4eec\u4e0d\u59a8\u7528\u4e00\u4e2a\u6570\u7ec4dist\u8bb0\u5f55\u8fd9\u4e00\u8ddd\u79bb&#xff0c;\u5373dist[i]\u4e3a1-&gt;i\u7684\u6700\u77ed\u8def\u8ddd\u79bb&amp;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/69110.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-30T21:32:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130213246-697d237eee621.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/69110.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/69110.html\",\"name\":\"\u3010\u56fe\u8bba\u3011\u6700\u77ed\u8def\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-30T21:32:48+00:00\",\"dateModified\":\"2026-01-30T21:32:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/69110.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/69110.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/69110.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u56fe\u8bba\u3011\u6700\u77ed\u8def\u95ee\u9898\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010\u56fe\u8bba\u3011\u6700\u77ed\u8def\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/69110.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u56fe\u8bba\u3011\u6700\u77ed\u8def\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5355\u6e90\u6700\u77ed\u8def\u95ee\u9898 \u5355\u6e90\u6700\u77ed\u8def\u95ee\u9898\u662f\u6307\u56fa\u5b9a\u4e00\u4e2a\u8d77\u70b9&#xff0c;\u6c42\u5b83\u5230\u5176\u4ed6\u4efb\u610f\u4e00\u70b9\u7684\u6700\u77ed\u8def\u95ee\u9898\u3002 Dijkstra\u7b97\u6cd5&#xff08;\u8fea\u514b\u65af\u62c9\u7279\u7b97\u6cd5&#xff09; \u9996\u5148\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u8fd9\u4e2a\u7b97\u6cd5\u4ec5\u4ec5\u7528\u4e8e\u56fe\u4e2d\u6ca1\u6709\u8d1f\u8fb9\u7684\u60c5\u51b5\u3002 \u539f\u7406 Dijkstra\u672c\u8d28\u903b\u8f91\u5176\u5b9e\u662f\u52a8\u6001\u89c4\u5212&#xff0c;\u6211\u4eec\u8981\u6c42\u4efb\u610f\u70b9i\u5230\u8d77\u70b9&#xff08;\u5047\u8bbe\u4e3a1&#xff09;\u7684\u8ddd\u79bb&#xff0c;\u6211\u4eec\u4e0d\u59a8\u7528\u4e00\u4e2a\u6570\u7ec4dist\u8bb0\u5f55\u8fd9\u4e00\u8ddd\u79bb&#xff0c;\u5373dist[i]\u4e3a1-&gt;i\u7684\u6700\u77ed\u8def\u8ddd\u79bb&amp;","og_url":"https:\/\/www.wsisp.com\/helps\/69110.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-30T21:32:48+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130213246-697d237eee621.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/69110.html","url":"https:\/\/www.wsisp.com\/helps\/69110.html","name":"\u3010\u56fe\u8bba\u3011\u6700\u77ed\u8def\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-30T21:32:48+00:00","dateModified":"2026-01-30T21:32:48+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/69110.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/69110.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/69110.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u56fe\u8bba\u3011\u6700\u77ed\u8def\u95ee\u9898"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/69110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=69110"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/69110\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/69105"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=69110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=69110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=69110"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=69110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}