{"id":54426,"date":"2025-08-13T05:50:00","date_gmt":"2025-08-12T21:50:00","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/54426.html"},"modified":"2025-08-13T05:50:00","modified_gmt":"2025-08-12T21:50:00","slug":"%e3%80%90%e7%8b%82%e7%83%ad%e7%ae%97%e6%b3%95%e7%af%87%e3%80%91%e6%8e%a2%e5%af%bb%e5%9b%be%e8%ae%ba%e5%b9%bd%e5%be%84%e4%b9%8bspfa%e7%ae%97%e6%b3%95%ef%bc%9a%e5%9b%be%e8%ae%ba%e8%bf%b7%e5%ae%ab","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/54426.html","title":{"rendered":"\u3010\u72c2\u70ed\u7b97\u6cd5\u7bc7\u3011\u63a2\u5bfb\u56fe\u8bba\u5e7d\u5f84\u4e4bSPFA\u7b97\u6cd5\uff1a\u56fe\u8bba\u8ff7\u5bab\u91cc\u7684\u95ea\u7535\u5bfb\u5f84\u8005(\u901a\u4fd7\u6613\u61c2\u7248)"},"content":{"rendered":"<p>\u00a0\u200b\u200b\u200b\u200b\u200b\u672c\u7bc7\u5e26\u5927\u5bb6\u63a2\u7a76\u7684\u662fSPFA\u7b97\u6cd5&#xff1b;\u4ece\u57fa\u672c\u7406\u89e3&#xff0c;\u753b\u56fe\u5206\u6790\u5c55\u793a&#xff0c;\u518d\u5230\u6700\u540e\u7684\u4ee3\u7801\u5b9e\u73b0&#xff0c;\u4ee5\u53ca\u4e3a\u4f55\u8981\u8fd9\u6837\u5b9e\u73b0\u4ee3\u7801&#xff0c;\u7b49\u4e00\u4e9b\u7ec6\u8282\u95ee\u9898\u505a\u89e3\u91ca&#xff0c;\u76f8\u5173\u9898\u578b\u5e94\u7528&#xff0c;\u975e\u5e38\u503c\u5f97\u54df&#xff0c;\u5c24\u5176\u662f\u521a\u5165\u95e8\u7684\u5c0f\u767d\u5b66\u4e60&#xff1b;\u5e72\u8d27\u6ee1\u6ee1&#xff0c;\u901a\u4fd7\u6613\u61c2&#xff1b;\u6b22\u8fce\u5927\u5bb6\u70b9\u8d5e\u6536\u85cf\u9605\u8bfb\u5440&#xff01;&#xff01;&#xff01;<\/p>\n<h2><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214955-689bb703a9484.gif\" \/><\/h2>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214956-689bb70432690.gif\" \/><\/p>\n<p>\u00a0\u00a0\u6b22\u8fce\u62dc\u8bbf&#xff1a;\u7f91\u60bb\u7684\u5c0f\u6740\u9a6c\u7279.-CSDN\u535a\u5ba2<\/p>\n<p>\u672c\u7bc7\u4e3b\u9898&#xff1a;\u79d2\u61c2\u767e\u79d1\u4e4bSPFA\u7b97\u6cd5\u7684\u6df1\u5ea6\u5256\u6790<\/p>\n<p>\u5236\u4f5c\u65e5\u671f&#xff1a;2025.08.12<\/p>\n<p>\u96b6\u5c5e\u4e13\u680f&#xff1a;\u00a0\u56fe\u8bba\u5b66\u4e60\u4e13\u680f\u00a0 \u00a0\u7f8e\u5999\u7684\u7b97\u6cd5\u4e16\u754c\u4e13\u680f<\/p>\n<p>\u56fe\u8bba\u7cfb\u5217\u7b97\u6cd5\u53c2\u8003\u56fe&#xff1a;\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"567\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214956-689bb7044293f.png\" width=\"1105\" \/><\/p>\n<p>\u76ee\u5f55&#xff1a;<\/p>\n<p style=\"margin-left:0px\">\u4e00\u00b7\u672c\u7b97\u6cd5\u80cc\u666f&#xff1a;<\/p>\n<p style=\"margin-left:0px\">\u4e8c\u00b7\u7b97\u6cd5\u6838\u5fc3\u601d\u60f3\u53ca\u5b9e\u4f8b\u6a21\u62df\u64cd\u4f5c&#xff1a;<\/p>\n<p style=\"margin-left:0px\">\u4e09.\u7b97\u6cd5\u8be6\u7ec6\u6b65\u9aa4:\u00a0<\/p>\n<p style=\"margin-left:0px\">3.1\u521d\u59cb\u5316\u64cd\u4f5c&#xff1a;<\/p>\n<p style=\"margin-left:0px\">3.2\u961f\u5217\u8fed\u4ee3\u677e\u5f1b\u9636\u6bb5&#xff1a;<\/p>\n<p style=\"margin-left:0px\">3.3\u63d0\u901f\u5c0f\u4f18\u5316&#xff1a;<\/p>\n<p style=\"margin-left:0px\">3.4\u68c0\u67e5\u662f\u5426\u5b58\u5728\u8d1f\u73af&#xff1a;<\/p>\n<p style=\"margin-left:0px\">\u56db.\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/p>\n<p style=\"margin-left:0px\">\u4e94\u00b7\u4ee3\u7801\u5b9e\u4f8b\u6d4b\u8bd5&#xff1a;<\/p>\n<p style=\"margin-left:0px\">\u516d.\u5c0f\u7ec6\u8282\u53ca\u7591\u70b9\u5256\u6790&#xff1a;<\/p>\n<p style=\"margin-left:0px\">6.1 \u4e3a\u4ec0\u4e48\u53ef\u4ee5\u901a\u8fc7\u961f\u5217\u8fdb\u884cBellman-Ford\u7b97\u6cd5\u7684\u4f18\u5316\u64cd\u4f5c&#xff1a;<\/p>\n<p style=\"margin-left:0px\">6.2 cnt\u8bb0\u5f55\u6bcf\u4e2a\u70b9\u5165\u961f\u5217\u7684\u6b21\u6570\u4e3a\u4ec0\u4e48\u80fd\u5b9e\u73b0\u5bf9\u8d1f\u73af\u7684\u5224\u65ad&#xff1a;<\/p>\n<p style=\"margin-left:0px\">6.3\u4e3a\u4ec0\u4e48in_q\u8981\u8bb0\u5f55\u67d0\u4e2a\u70b9\u662f\u5426\u5728\u961f\u5217\u91cc&#xff1a;<\/p>\n<p style=\"margin-left:0px\">\u4e03.\u7b97\u6cd5\u590d\u6742\u5ea6\u5206\u6790&#xff1a;<\/p>\n<p style=\"margin-left:0px\">7.1\u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;<\/p>\n<p style=\"margin-left:0px\">7.2\u7a7a\u95f4\u590d\u6742\u5ea6&#xff1a;<\/p>\n<p style=\"margin-left:0px\">\u516b\u00b7SLF\u4f18\u5316\u53caLLL\u4f18\u5316\u7b56\u7565&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.1SLF&#xff08;Small Label First&#xff09;\u4f18\u5316&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.1.1\u4f18\u5316\u539f\u7406&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.1.2\u5b9e\u73b0\u65b9\u5f0f&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.1.3 SLF\u4f18\u5316\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.2LLL&#xff08;Large Label Last&#xff09;\u4f18\u5316&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.2.1\u4f18\u5316\u539f\u7406&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.2.2\u5b9e\u73b0\u65b9\u5f0f&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.2.3LL\u4f18\u5316\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.3SLF\u4e0eLLL\u8054\u5408\u4f18\u5316&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.3.1\u4f18\u5316\u539f\u7406&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.3.2\u5b9e\u73b0\u65b9\u5f0f&#xff1a;<\/p>\n<p style=\"margin-left:0px\">8.3.3SLF\u4e0eLLL\u8054\u5408\u4f18\u5316\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/p>\n<p style=\"margin-left:0px\">\u4e5d.\u7b97\u6cd5\u4f18\u52bf\u4e0e\u5c40\u9650\u6027:<\/p>\n<p style=\"margin-left:80px\">9.1\u4f18\u52bf&#xff1a;<\/p>\n<p style=\"margin-left:80px\">9.2\u5c40\u9650\u6027:<\/p>\n<p style=\"margin-left:0px\">\u5341\u00b7\u5e94\u7528\u573a\u666f&#xff1a;<\/p>\n<p style=\"margin-left:0px\">10.1\u4ea4\u901a\u7f51\u7edc\u89c4\u5212&#xff1a;<\/p>\n<p style=\"margin-left:0px\">10.2\u91d1\u878d\u5957\u5229\u5206\u6790&#xff1a;<\/p>\n<p style=\"margin-left:0px\">10.3\u793e\u4ea4\u7f51\u7edc\u4fe1\u606f\u4f20\u64ad&#xff1a;<\/p>\n<p style=\"margin-left:0px\">10.4\u7ade\u8d5b\u4e0e\u5b66\u4e60&#xff1a;<\/p>\n<p style=\"margin-left:0px\">\u5341\u4e00\u00b7\u4e2a\u4eba\u5c0f\u603b\u7ed3&#xff1a;<\/p>\n<hr \/>\n<\/p>\n<h2>\u4e00\u00b7\u672c\u7b97\u6cd5\u80cc\u666f&#xff1a;<\/h2>\n<p>\u5728\u56fe\u8bba\u7684\u7b97\u6cd5\u4f53\u7cfb\u4e2d&#xff0c;\u6c42\u89e3\u5355\u6e90\u6700\u77ed\u8def\u5f84\u95ee\u9898\u662f\u4e00\u4e2a\u6838\u5fc3\u4e14\u57fa\u7840\u7684\u4efb\u52a1&#xff0c;\u5176\u5728\u4f17\u591a\u9886\u57df\u6709\u7740\u5e7f\u6cdb\u4e14\u5173\u952e\u7684\u5e94\u7528\u3002SPFA&#xff08;Shortest Path Faster Algorithm&#xff09;\u7b97\u6cd5\u4f5c\u4e3a\u8fd9\u4e00\u9886\u57df\u7684\u7ecf\u5178\u7b97\u6cd5\u4e4b\u4e00&#xff0c;\u51ed\u501f\u81ea\u8eab\u72ec\u7279\u7684\u4f18\u52bf&#xff0c;\u5728\u5904\u7406\u590d\u6742\u56fe\u7ed3\u6784\u65f6\u53d1\u6325\u7740\u91cd\u8981\u4f5c\u7528\u3002\u5b83\u4e0d\u4ec5\u80fd\u591f\u5904\u7406\u5305\u542b\u8d1f\u6743\u8fb9\u7684\u56fe&#xff0c;\u8fd8\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\u5c55\u73b0\u51fa\u8f83\u9ad8\u7684\u6548\u7387&#xff0c;\u8fd9\u4f7f\u5f97\u5b83\u5728\u4ea4\u901a\u89c4\u5212\u3001\u901a\u4fe1\u7f51\u7edc\u4f18\u5316\u3001\u7ecf\u6d4e\u51b3\u7b56\u7b49\u591a\u4e2a\u5b9e\u9645\u573a\u666f\u4e2d\u5907\u53d7\u9752\u7750\u3002<\/p>\n<h2>\u4e8c\u00b7\u7b97\u6cd5\u6838\u5fc3\u601d\u60f3\u53ca\u5b9e\u4f8b\u6a21\u62df\u64cd\u4f5c&#xff1a;<\/h2>\n<p>SPFA \u7b97\u6cd5\u672c\u8d28\u4e0a\u662f\u5bf9 Bellman &#8211; Ford \u7b97\u6cd5\u7684\u4e00\u79cd\u4f18\u5316\u3002<\/p>\n<p>Bellman &#8211; Ford \u7b97\u6cd5\u901a\u8fc7\u5bf9\u56fe\u4e2d\u6240\u6709\u8fb9\u8fdb\u884cn-1\u6b21\u677e\u5f1b\u64cd\u4f5c&#xff08;n\u4e3a\u9876\u70b9\u6570&#xff09;\u6765\u786e\u5b9a\u6700\u77ed\u8def\u5f84&#xff0c;\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u80fd\u4fdd\u8bc1\u6b63\u786e\u6027&#xff0c;\u4f46\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u8fdb\u884c\u4e86\u5927\u91cf\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3002<\/p>\n<p>SPFA \u7b97\u6cd5\u5219\u5f15\u5165\u4e86\u961f\u5217\u7684\u601d\u60f3&#xff0c;\u5b83\u53ea\u5bf9\u90a3\u4e9b\u8ddd\u79bb\u53ef\u80fd\u88ab\u66f4\u65b0\u7684\u9876\u70b9\u8fdb\u884c\u5904\u7406&#xff0c;\u4ece\u800c\u6781\u5927\u5730\u51cf\u5c11\u4e86\u5197\u4f59\u8ba1\u7b97\u3002<\/p>\n<p>\u7c7b\u4f3c\u4e8eBFS\u4f46\u662f\u53c8\u4e0d\u5168\u662f&#xff1b;\u5b83\u8fdb\u8fc7\u961f\u5217\u7684\u70b9\u51fa\u53bb\u540e\u8fd8\u53ef\u80fd\u518d\u6b21\u4f5c\u4e3a\u6700\u77ed\u8def\u5f84\u7684\u4e2d\u95f4\u70b9\u7b49\u518d\u6b21\u5165\u3002<\/p>\n<p>\u603b\u4e4b&#xff0c;\u5c31\u662f\u5165\u961f\u4e0d\u4e00\u5b9a\u5c31\u662f\u6700\u77ed\u8def\u5f84\u7684\u70b9&#xff1b;\u8fd8\u6709\u53ef\u80fd\u88ab\u66f4\u65b0&#xff1b;\u540c\u65f6\u51fa\u961f\u540e&#xff1b;\u8fd8\u6709\u53ef\u80fd\u6709\u6307\u5411\u5b83\u7684\u70b9u&#xff1b;\u501f\u52a9\u8fd9\u4e2a\u70b9u\u5230\u5b83\u66f4\u8fd1&#xff1b;\u4e5f\u5c31\u662f\u518d\u6b21\u66f4\u65b0\u8fd9\u4e2av\u7684dist\u4f7f\u5f97\u5b83\u518d\u6b21\u5165\u961f\u3002\u00a0<\/p>\n<p>\u8fd9\u91cc\u867d\u7136\u8bf4SPFA\u7b97\u6cd5\u76f8\u5bf9BEllman-Ford\u7b97\u6cd5\u4f1a\u5feb&#xff1b;\u4f46\u662f\u6709\u65f6\u5019\u7531\u4e8e\u56fe\u7684\u7279\u70b9\u8fd8\u662f\u4f1a\u53d1\u751f\u8d85\u65f6\u7b49\u64cd\u4f5c&#xff1b;\u6240\u4ee5\u4f7f\u7528\u4e5f\u9700\u8981\u4ed4\u7ec6\u8003\u8651\u3002\u00a0<\/p>\n<p>\u9996\u5148\u9700\u8981\u7684\u8868&#xff1a;<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:500px\">\n<tbody>\n<tr>\n<td>\n<p>vector&lt;pair&lt;int,int&gt;&gt; v[N]<\/p>\n<\/td>\n<td>\n<p>in_q\u6570\u7ec4<\/p>\n<\/td>\n<td>cnt\u6570\u7ec4<\/td>\n<td>pre\u6570\u7ec4<\/td>\n<td>\u961f\u5217<\/td>\n<td>dsit\u6570\u7ec4<\/td>\n<\/tr>\n<tr>\n<td>\u50a8\u5b58u\u70b9\u5230\u8fbe\u7684\u6240\u6709v\u70b9\u53ca\u5176\u6743\u503c\u7684\u6570\u7ec4&#xff08;u\u4e3a\u8d77\u59cb&#xff0c;v\u4e3a\u7ec8\u6b62&#xff09;<\/td>\n<td>\u68c0\u67e5\u67d0\u4e2a\u70b9\u662f\u5426\u5b58\u5728\u961f\u5217<\/td>\n<td>\u8bb0\u5f55\u6bcf\u4e2a\u70b9\u8fdb\u5165\u961f\u5217\u7684\u6b21\u6570<\/td>\n<td>\u8bb0\u5f55\u524d\u9a71\u8282\u70b9<\/td>\n<td>\u8ba9\u53ef\u80fd\u662f\u6700\u77ed\u8def\u5f84\u7684\u70b9\u5165\u8fdb\u6765<\/td>\n<td>\u8bb0\u5f55\u6e90\u70b9\u8ddd<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0b\u9762\u4e3e\u4f8b\u5b50\u6a21\u62df\u4e00\u4e0b&#xff1a;<\/p>\n<p>\u5b8c\u6210\u521d\u59cb\u5316&#xff1a;<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214956-689bb704b8373.png\" \/><\/p>\n<p>\u5177\u4f53\u64cd\u4f5c\u5982\u56fe&#xff1a;<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214956-689bb704f3f82.png\" \/><\/p>\n<p>\u8fd9\u6837\u4e00\u6765\u5176\u5b9e\u86ee\u7b80\u5355\u7684\u5427&#xff1b;\u5f53\u7136\u4ee3\u7801\u4e5f\u662f\u5982\u6b64&#xff1b;\u6ce8\u610f\u4e00\u4e9b\u7ec6\u8282\u5c31\u597d\u5566&#xff01;\u00a0<\/p>\n<h2>\u4e09.\u7b97\u6cd5\u8be6\u7ec6\u6b65\u9aa4:\u00a0<\/h2>\n<p>\u4e0b\u9762\u6211\u4eec\u5206\u56db\u6b65\u6765\u5bf9\u4ee3\u7801\u8fdb\u884c\u5dee\u5206\u8fc7\u7a0b&#xff1b;\u8fdb\u884c\u76f8\u5173\u4ee3\u7801\u5256\u6790\u4e66\u5199\u64cd\u4f5c&#xff1a;<\/p>\n<h2>3.1\u521d\u59cb\u5316\u64cd\u4f5c&#xff1a;<\/h2>\n<p>\u4e0b\u9762\u6211\u4eec\u6839\u636e\u4e0a\u9762\u7684\u4f8b\u5b50\u53ca\u8868\u683c\u5b8c\u6210\u5bf9\u5e94\u4ee3\u7801\u4e66\u5199&#xff1a;<\/p>\n<p>\u521d\u59cb\u5316\u90bb\u63a5\u8868&#xff1a;<\/p>\n<p>cin &gt;&gt; n &gt;&gt; m;<br \/>\nfor (int i &#061; 0; i &lt; m; i&#043;&#043;) {<br \/>\nint x, y, z;<br \/>\n\/\/\u5355\u53cc\u5411\u8fb9\u90fd\u53ef&#xff1a;<br \/>\ncin &gt;&gt; x &gt;&gt; y &gt;&gt; z;<br \/>\nv[x].emplace_back(y, z);<br \/>\n} <\/p>\n<p>\u00a0\u521d\u59cb\u5316pre\u6570\u7ec4&#xff1a;<\/p>\n<p>void init_pre() {<\/p>\n<p>for (int i &#061; 1; i &lt;&#061; n; i&#043;&#043;)pre[i] &#061; i;<br \/>\n} <\/p>\n<p>\u521d\u59cb\u5316dist&#xff1a;<\/p>\n<p>fill(dist &#043; 1, dist &#043; n &#043; 1, maxi);\/\/\u521d\u59cb\u5316dist\u8981\u6ce8\u610f&#xff1a;\u6211\u4eec\u4ece1-n\u7684\u70b9\u7684\u7f16\u53f7&#xff1b;\u4e5f\u5c31\u662f\u521d\u59cb\u5316\u8fd9\u4e2a\u533a\u95f4&#xff08;[\u8fed\u4ee3\u5668)&#xff09; <\/p>\n<p>\u5176\u4ed6&#xff1b;\u8fd9\u6837\u7c7b\u4f3c\u7684\u521d\u59cb\u5316\u548c\u6211\u4eec\u4e0a\u4e00\u7bc7\u7684Bellman-Ford\u7b97\u6cd5\u5dee\u4e0d\u591a&#xff1b;\u53ef\u4ee5\u53c2\u8003&#xff1a;<\/p>\n<p>\u3010\u72c2\u70ed\u7b97\u6cd5\u7bc7\u3011\u63a2\u5bfb\u56fe\u8bba\u5e7d\u5f84&#xff1a;Bellman &#8211; Ford \u7b97\u6cd5\u7684\u6d6a\u6f2b\u5f81\u7a0b&#xff08;\u901a\u4fd7\u6613\u61c2\u7248&#xff09;-CSDN\u535a\u5ba2<\/p>\n<\/p>\n<h2>3.2\u961f\u5217\u8fed\u4ee3\u677e\u5f1b\u9636\u6bb5&#xff1a;<\/h2>\n<p>\u4e5f\u5c31\u662f\u8bf4\u53ea\u8981\u961f\u5217\u4e0d\u5b58\u5728&#xff1b;\u7136\u540e\u6211\u4eec\u5bf9\u5230\u8fbe\u8fd9\u4e2ap\u70b9\u7684\u8fb9\u8fdb\u884c\u677e\u5f1b\u6210\u529f\u4e86&#xff1b;\u5373\u6b64\u65f6\u7684\u8def\u5f84\u6709\u53ef\u80fd\u662f\u5230\u8fbep\u6216\u8005\u901a\u8fc7p\u5230\u8fbe\u5176\u4ed6\u70b9\u7684\u6700\u77ed\u8def\u5f84\u3002 \u00a0 \u00a0 \u00a0 \u56e0\u6b64\u53ef\u80fd\u4f5c\u4e3a\u6700\u77ed\u8def\u5f84\u53ca\u5b83\u7684\u4e00\u90e8\u5206&#xff1b;\u6545\u5165\u961f\u5217&#xff1b;\u4f46\u662f\u8fd8\u662f\u4e0d\u592a\u5b8c\u7f8e&#xff1b;\u56e0\u6b64\u5c31\u8981\u7528\u5230\u4e0b\u9762\u7684\u4f18\u5316\u4e86\u3002<\/p>\n<p>dist[s] &#061; 0;<br \/>\nq.push(s);<br \/>\nwhile (!q.empty()) {<br \/>\nint cur &#061; q.front();<br \/>\nq.pop();<br \/>\nfor (auto [p,w] : v[cur]) {<\/p>\n<p>if (dist[cur] !&#061; maxi &amp;&amp; dist[cur] &#043; w &lt; dist[p]) {<br \/>\ndist[p] &#061; dist[cur] &#043; w;<br \/>\npre[p] &#061; cur;<br \/>\nq.push(p);<\/p>\n<p>}<br \/>\n}<\/p>\n<p>}<\/p>\n<h2>3.3\u63d0\u901f\u5c0f\u4f18\u5316&#xff1a;<\/h2>\n<p>\u8bb0\u5f55\u662f\u5426\u5728\u961f\u5217\u4e2d\u9632\u6b62\u91cd\u590d\u64cd\u4f5c&#xff1b;\u56e0\u4e3a\u5982\u679c\u961f\u5217\u6709\u76f8\u540c\u7684\u70b9&#xff1b;\u90a3\u4e48\u5bf9\u4e00\u4e2a\u70b9\u64cd\u4f5c\u5b8c\u53ea\u53ef\u80fd\u6539\u53d8dist p&#xff1b;\u81ea\u8eab\u7684dist\u4e0d\u4f1a\u53d8&#xff1b;\u4e5f\u5c31\u662f\u5f53\u518d\u6b21\u904d\u5386\u5230\u4e0b\u4e00\u4e2a\u540c\u70b9\u7684\u65f6\u5019&#xff1b;\u4e0d\u4f1a\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\u3002<\/p>\n<p>\u6240\u4ee5\u53ea\u9700\u8981\u5728\u4e0a\u9762\u4ee3\u7801\u52a0\u4e0a\u68c0\u67e5\u6570\u7ec4\u5373\u53ef&#xff1a;<\/p>\n<p>dist[s] &#061; 0;<br \/>\nq.push(s);<br \/>\nin_q[s] &#061; 1;<br \/>\nwhile (!q.empty()) {<br \/>\nint cur &#061; q.front();<br \/>\nq.pop();<br \/>\nin_q[cur] &#061; 0;<br \/>\nfor (auto [p,w] : v[cur]) {<\/p>\n<p>if (dist[cur] !&#061; maxi &amp;&amp; dist[cur] &#043; w &lt; dist[p]) {<br \/>\ndist[p] &#061; dist[cur] &#043; w;<br \/>\npre[p] &#061; cur;<br \/>\nif (!in_q[p]) {\/\/ \u4e0d\u5728\u961f\u5217\u4e2d\u624d\u5165\u961f<br \/>\nq.push(p);<br \/>\nin_q[p] &#061; 1;<\/p>\n<p>}<br \/>\n}<br \/>\n}<\/p>\n<p>}<\/p>\n<h2>3.4\u68c0\u67e5\u662f\u5426\u5b58\u5728\u8d1f\u73af&#xff1a;<\/h2>\n<p>\u8fd9\u91cc\u6211\u4eec\u5148\u653e\u7ed3\u8bba&#xff1b;\u5982\u679c\u4e00\u4e2a\u70b9\u88ab\u5165\u961f\u5217\u6b21\u6570\u8d85\u8fc7\u4e86n\u6b21&#xff1b;\u90a3\u4e48\u5b83\u4e00\u5b9a\u662f\u5b58\u5728\u8d1f\u73af\u7684&#xff08;\u6b63\u73af\u62160\u73af\u53ef\u4ee5\u63d0\u524d\u627e\u5230\u6700\u77ed\u8def\u5f84&#xff1b;\u53bb\u73af\u65e0\u5f71\u54cd&#xff1b;\u6545\u4e0d\u53ef\u80fd\u53d1\u751f&#xff09;&#xff1a;<\/p>\n<p>dist[s] &#061; 0;<br \/>\nq.push(s);<br \/>\nin_q[s] &#061; 1;<br \/>\ncnt[s]&#043;&#043;;<br \/>\nwhile (!q.empty()) {<br \/>\nint cur &#061; q.front();<br \/>\nq.pop();<br \/>\nin_q[cur] &#061; 0;<br \/>\nfor (auto [p,w] : v[cur]) {<br \/>\nif (dist[cur] !&#061; maxi &amp;&amp; dist[cur] &#043; w &lt; dist[p]) {<br \/>\ndist[p] &#061; dist[cur] &#043; w;<br \/>\npre[p] &#061; cur;<br \/>\nif (!in_q[p]) {\/\/ \u4e0d\u5728\u961f\u5217\u4e2d\u624d\u5165\u961f<br \/>\nq.push(p);<br \/>\nin_q[p] &#061; 1;<br \/>\ncnt[p]&#043;&#043;;<br \/>\nif (cnt[p] &gt; n) return 0;\/\/\u5224\u65ad\u8d1f\u73af<\/p>\n<p>}<br \/>\n}<br \/>\n}<\/p>\n<p>}<\/p>\n<h2>\u56db.\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/h2>\n<p>#include&lt;iostream&gt;<br \/>\n#include&lt;vector&gt;<br \/>\n#include&lt;algorithm&gt;<br \/>\n#include&lt;map&gt;<br \/>\n#include&lt;queue&gt;<br \/>\nusing namespace std;<br \/>\nint maxi &#061; 0x3f3f3f3f;<br \/>\nconst int N &#061; 1e6 &#043; 1;<br \/>\nint dist[N], n, m;<br \/>\nint pre[N];\/\/\u524d\u9a71\u8282\u70b9<br \/>\nvector&lt;pair&lt;int,int&gt;&gt; v[N];\/\/\u90bb\u63a5\u8868&#xff1a;\u50a8\u5b58u\u53ca\u6240\u8fde\u63a5\u7684v\u548c\u4e4b\u95f4\u7684w<br \/>\nqueue&lt;int&gt;q;<br \/>\nbool in_q[N] &#061; { 0 };\/\/\u8bb0\u5f55\u662f\u5426\u5728\u961f\u5217\u4e2d\u9632\u6b62\u91cd\u590d\u64cd\u4f5c&#xff1b;\u56e0\u4e3a\u5982\u679c\u961f\u5217\u6709\u76f8\u540c\u7684\u70b9&#xff1b;\u90a3\u4e48\u5bf9\u4e00\u4e2a\u70b9\u64cd\u4f5c\u5b8c\u53ea\u53ef\u80fd\u6539\u53d8dist p&#xff1b;\u81ea\u8eab\u7684dist<br \/>\n  \/\/\u4e0d\u4f1a\u53d8&#xff1b;\u4e5f\u5c31\u662f\u5f53\u518d\u6b21\u904d\u5386\u5230\u4e0b\u4e00\u4e2a\u540c\u70b9\u7684\u65f6\u5019&#xff1b;\u4e0d\u4f1a\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\u3002<br \/>\nint cnt[N] &#061; { 0 };\/\/\u8bb0\u5f55\u8282\u70b9\u5165\u961f\u5217\u6b21\u6570&#xff1b;\u5224\u65ad\u662f\u5426\u51fa\u73b0\u8d1f\u73af<br \/>\nbool SPFA(int s,int n) {\/\/n\u4e2a\u70b9<br \/>\nfill(dist &#043; 1, dist &#043; n &#043; 1, maxi);\/\/\u521d\u59cb\u5316dist\u8981\u6ce8\u610f&#xff1a;\u6211\u4eec\u4ece1-n\u7684\u70b9\u7684\u7f16\u53f7&#xff1b;\u4e5f\u5c31\u662f\u521d\u59cb\u5316\u8fd9\u4e2a\u533a\u95f4&#xff08;[\u8fed\u4ee3\u5668)&#xff09;<br \/>\ndist[s] &#061; 0;<br \/>\nq.push(s);<br \/>\nin_q[s] &#061; 1;<br \/>\ncnt[s]&#043;&#043;;<br \/>\nwhile (!q.empty()) {<br \/>\nint cur &#061; q.front();<br \/>\nq.pop();<br \/>\nin_q[cur] &#061; 0;<br \/>\nfor (auto [p,w] : v[cur]) {<br \/>\n           \/\/\u8fd9\u91cc\u4e3a\u4f55\u4f1a\u5165\u961f\u5217&#xff1f;<br \/>\n        \/\/\u4e5f\u5c31\u662f\u8bf4\u53ea\u8981\u961f\u5217\u4e0d\u5b58\u5728&#xff1b;\u7136\u540e\u6211\u4eec\u5bf9\u5230\u8fbe\u8fd9\u4e2ap\u70b9\u7684\u8fb9\u8fdb\u884c\u677e\u5f1b\u6210\u529f\u4e86&#xff1b;\u5373\u6b64\u65f6\u7684\u8def\u5f84\u6709\u53ef\u80fd\u662f\u5230\u8fbep\u6216\u8005\u901a\u8fc7p\u5230\u8fbe\u5176\u4ed6\u70b9\u7684\u6700\u77ed\u8def\u5f84<br \/>\n        \/\/\u56e0\u6b64\u53ef\u80fd\u4f5c\u4e3a\u6700\u77ed\u8def\u5f84\u53ca\u5b83\u7684\u4e00\u90e8\u5206&#xff1b;\u6545\u5165\u961f\u5217\u3002<br \/>\nif (dist[cur] !&#061; maxi &amp;&amp; dist[cur] &#043; w &lt; dist[p]) {<br \/>\ndist[p] &#061; dist[cur] &#043; w;<br \/>\npre[p] &#061; cur;<br \/>\nif (!in_q[p]) {\/\/ \u4e0d\u5728\u961f\u5217\u4e2d\u624d\u5165\u961f<br \/>\nq.push(p);<br \/>\nin_q[p] &#061; 1;<br \/>\ncnt[p]&#043;&#043;;<br \/>\nif (cnt[p] &gt; n) return 0;\/\/\u5224\u65ad\u8d1f\u73af<\/p>\n<p>}<br \/>\n}<br \/>\n}<\/p>\n<p>}<\/p>\n<p>return 1;<\/p>\n<p>}<\/p>\n<p>void init_pre() {<\/p>\n<p>for (int i &#061; 1; i &lt;&#061; n; i&#043;&#043;)pre[i] &#061; i;<br \/>\n}<br \/>\nint getpath(int x) {<br \/>\npre[x] &#061;&#061; x ? x : getpath(pre[x]);<br \/>\ncout &lt;&lt; x &lt;&lt; &#034; &#034;;<br \/>\nreturn x;<br \/>\n}<\/p>\n<p>int main() {<br \/>\ncin &gt;&gt; n &gt;&gt; m;<br \/>\nfor (int i &#061; 0; i &lt; m; i&#043;&#043;) {<br \/>\nint x, y, z;<br \/>\n\/\/\u5355\u53cc\u5411\u8fb9\u90fd\u53ef&#xff1a;<br \/>\ncin &gt;&gt; x &gt;&gt; y &gt;&gt; z;<br \/>\nv[x].emplace_back(y, z);<br \/>\n}<br \/>\ninit_pre();<\/p>\n<p>bool flag&#061;SPFA(1,n);<br \/>\nif (flag) {<br \/>\nif (dist[n] &#061;&#061; maxi) cout &lt;&lt; &#034;unconnected&#034; &lt;&lt; endl;<br \/>\nelse {<br \/>\ncout &lt;&lt; dist[n] &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;\u6700\u77ed\u8def\u5f84&#xff1a;&#034; &lt;&lt; endl;<br \/>\ngetpath(n);<br \/>\n}<br \/>\n}<br \/>\nelse {<br \/>\ncout &lt;&lt; &#034;exist negative circle &#034; &lt;&lt; endl;<br \/>\n}<br \/>\nreturn 0;<\/p>\n<p>} <\/p>\n<h2>\u4e94\u00b7\u4ee3\u7801\u5b9e\u4f8b\u6d4b\u8bd5&#xff1a;<\/h2>\n<p>\u4e0b\u9762\u6211\u4eec\u5c31\u6765\u7528\u51e0\u7ec4\u6570\u636e\u6d4b\u8bd5\u4e00\u4e0b\u4e0a\u8ff0\u7684\u4ee3\u7801&#xff1a;<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214957-689bb7058427e.png\" \/><\/p>\n<p>\u2460\u770b\u4e00\u4e0b\u5230\u8fbe6\u53f7\u70b9\u6700\u77ed\u8def\u5f84\u662f\u4e0d\u662f1&#xff1b;\u8def\u9014\u662f\u4e0d\u662f1 2 5 6&#xff1a;<\/p>\n<p>\u00a0<img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214957-689bb705a29a7.png\" \/><\/p>\n<p>\u4e5f\u662f\u6210\u7acb\u7684\u3002<\/p>\n<p>\u518d\u6d4b\u8bd5\u4e00\u4e0b&#xff1a;<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214957-689bb705affad.png\" \/><\/p>\n<p>\u2461\u770b\u4e00\u4e0b\u6700\u7ec81\u52304\u6700\u77ed\u8def\u5f84\u662f\u4e0d\u662f-3 \u8def\u9014\u662f\u4e0d\u662f1 3 4&#xff1a;\u00a0<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214957-689bb705c436f.png\" \/><\/p>\n<p>\u2462\u6d4b\u8bd5\u4e00\u4e0b\u8d1f\u73af&#xff1a;<\/p>\n<p>\u00a0<img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214957-689bb705d6891.png\" \/><\/p>\n<p>\u6700\u7ec8\u4e5f\u662f\u6210\u7acb\u7684&#xff1b;\u4f46\u662f\u6709\u4e9b\u60c5\u51b5SPFA\u7b97\u6cd5\u6709\u65f6\u5019\u4f1a\u8d85\u65f6&#xff1b;\u56e0\u6b64\u9009\u62e9\u7684\u65f6\u5019\u8fd8\u9700\u6ce8\u610f\u3002\u00a0<\/p>\n<h2>\u516d.\u5c0f\u7ec6\u8282\u53ca\u7591\u70b9\u5256\u6790&#xff1a;<\/h2>\n<p>\u6211\u4eec\u4e0b\u9762\u4ece\u8fd9\u51e0\u4e2a\u53ef\u80fd\u4f1a\u6709\u7591\u60d1\u7684\u70b9\u6765\u5206\u6790\u4e00\u4e0b&#xff1a;<\/p>\n<h2>6.1 \u4e3a\u4ec0\u4e48\u53ef\u4ee5\u901a\u8fc7\u961f\u5217\u8fdb\u884cBellman-Ford\u7b97\u6cd5\u7684\u4f18\u5316\u64cd\u4f5c&#xff1a;<\/h2>\n<p>\u6211\u4eec\u4e0a\u7bc7\u8bb2\u8ff0\u4e86Bellman-Ford\u7b97\u6cd5\u7684\u539f\u7406&#xff1a;\u5bf9\u5168\u8fb9n-1\u6b21\u677e\u5f1b&#xff1b;\u4f46\u662f\u8fd9\u6837\u5c31\u7b97\u63d0\u524d\u7ed3\u675f\u4e5f\u80af\u5b9a\u4f1a\u6709\u7684\u8fb9\u867d\u7136\u8ba9\u5b83\u677e\u5f1b\u4f46\u662f\u4e5f\u4e0d\u4f1a\u66f4\u65b0dist\u503c&#xff1b;\u4e5f\u5c31\u662f\u91cd\u590d\u64cd\u4f5c\u3002<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u8fd9\u91cc\u5c31\u53ef\u4ee5\u7528\u961f\u5217&#xff1a;\u6bd4\u5982\u67d0\u4e2a\u70b9u\u677e\u5f1b\u540edist\u66f4\u65b0\u4e86\u4e5f\u5c31\u662f\u5b83\u627e\u5230\u4e86\u6700\u77ed\u8def\u5f84\u6216\u8005\u5176\u4ed6\u70b9\u53ef\u4ee5\u501f\u52a9\u8fd9\u4e2a\u70b9u\u8fdb\u884c\u627e\u6700\u77ed\u8def\u5f84;\u56e0\u6b64\u628a\u5b83\u5165\u961f\u5217\u65b9\u4fbf\u627e\u5230\u53ef\u4ee5\u501f\u52a9\u5b83\u4f5c\u4e3a\u4e2d\u95f4\u70b9\u7684\u5176\u4ed6\u70b9v&#xff08;\u53ef\u80fd\u5c31\u662f\u6700\u77ed\u8def\u5f84&#xff09;\u3002<\/p>\n<p>\u00a0\u8fd9\u91cc\u4e3a\u4f55\u4f1a\u5165\u961f\u5217&#xff1f;\u00a0 \u4e5f\u5c31\u662f\u8bf4\u53ea\u8981\u961f\u5217\u4e0d\u5b58\u5728&#xff1b;\u7136\u540e\u6211\u4eec\u5bf9\u5230\u8fbe\u8fd9\u4e2ap\u70b9\u7684\u8fb9\u8fdb\u884c\u677e\u5f1b\u6210\u529f\u4e86&#xff1b;\u5373\u6b64\u65f6\u7684\u8def\u5f84\u6709\u53ef\u80fd\u662f\u5230\u8fbep\u6216\u8005\u901a\u8fc7p\u5230\u8fbe\u5176\u4ed6\u70b9\u7684\u6700\u77ed\u8def\u5f84\u56e0\u6b64\u53ef\u80fd\u4f5c\u4e3a\u6700\u77ed\u8def\u5f84\u53ca\u5b83\u7684\u4e00\u90e8\u5206&#xff1b;\u6545\u5165\u961f\u5217\u3002<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214957-689bb705e3703.png\" \/><\/p>\n<p>\u603b\u7ed3\u4e00\u4e0b&#xff1a;\u51e1\u662f\u5165\u961f\u5217\u7684\u70b9\u8981\u4e48\u5df2\u7ecf\u627e\u5230\u4e86\u6700\u77ed\u8def\u5f84\u8981\u4e48\u53ef\u80fd\u662f\u6700\u77ed\u8def\u5f84\u8981\u4e48\u5176\u4ed6\u70b9\u53ef\u4ee5\u501f\u52a9\u5165\u8fdb\u7684\u8fd9\u4e2a\u70b9\u7684dist\u627e\u5230\u81ea\u5df1\u7684\u6700\u77ed\u8def\u5f84\u3002\u00a0<\/p>\n<h2>6.2 cnt\u8bb0\u5f55\u6bcf\u4e2a\u70b9\u5165\u961f\u5217\u7684\u6b21\u6570\u4e3a\u4ec0\u4e48\u80fd\u5b9e\u73b0\u5bf9\u8d1f\u73af\u7684\u5224\u65ad&#xff1a;<\/h2>\n<p>\u6211\u4eecSPFA\u7b97\u6cd5\u540cBellman-Ford\u7b97\u6cd5\u4e00\u6837\u662f\u53ef\u4ee5\u68c0\u6d4b\u51fa\u8d1f\u73af\u7684&#xff1b;\u4f46\u662f\u8fd9\u91ccSPFA\u662f\u600e\u4e48\u68c0\u6d4b\u7684\u5462&#xff1f;<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\u5f53\u6709n\u6761\u8fb9\u7684\u65f6\u5019&#xff1b;\u6e90\u70b9\u5230\u8fbe\u4e00\u4e2a\u70b9\u7684\u6700\u77ed\u8def\u5f84\u9664\u4e86\u6b63\u73af\u548c\u96f6\u73af&#xff08;\u5b83\u4eec\u7684\u6700\u77ed\u8def\u662f\u7edd\u5bf9\u4e0d\u5305\u542b\u73af\u7684&#xff1b;\u56e0\u4e3a\u6709\u73af\u53ea\u4f1a\u5ef6\u957f\u6700\u77ed\u8def&#xff09;\u4e00\u5b9a\u6700\u957f\u662fn-1\u6761\u8fb9&#xff08;\u4e5f\u5c31\u5bf9\u5e94\u7740\u6211\u4eecBellman-Ford\u7b97\u6cd5\u4e3a\u5565\u8fdb\u884cn-1\u6b21\u5168\u8fb9\u677e\u5f1b\u64cd\u4f5c\u4e86\u3002&#xff09;&#xff1b;\u90a3\u4e48\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u7684\u7ed3\u8bba&#xff1a;<\/p>\n<p>n\u4e2a\u70b9\u7684\u65f6\u5019&#xff1b;\u90a3\u4e48\u6e90\u70b9\u5230\u8fbe\u67d0\u4e2a\u70b9\u7684\u8def\u5f84\u6700\u957f\u5c31\u662f\u8981\u7ecf\u8fc7n-1\u6761\u8fb9&#xff1b;\u7136\u540e\u4f1a\u6709\u6700\u591an\u6761\u8def\u5f84&#xff1b;\u4e5f\u5c31\u662f\u67d0\u4e2a\u70b9\u7684dist\u53ef\u80fd\u6700\u591a\u88ab\u66f4\u65b0n\u6b21&#xff1b;\u4e5f\u5c31\u662f\u6700\u591a\u4e00\u4e2a\u70b9\u53ef\u80fd\u4f1a\u5165\u961f\u5217n\u6b21&#xff1b;\u4e5f\u5c31\u662f\u6211\u4eec\u7684\u5982\u679c\u5b58\u5728\u6700\u5c0f\u8def\u5f84\u7684\u60c5\u51b5\u4e0b&#xff08;\u65e0\u73af&#xff0c;\u6b63\u73af&#xff0c;\u96f6\u73af&#xff09;&#xff1b;\u56e0\u6b64\u5982\u679c\u5b83\u8fdb\u5165\u7684\u6b21\u6570\u5927\u4e8en\u4e5f\u5c31\u8bf4\u660e\u662f\u8d1f\u73af\u7684\u60c5\u51b5\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u5c31\u4e3e\u4e00\u4e0b\u5f53\u6070\u597d\u4e00\u4e2a\u70b9\u5165\u961f\u5217\u53ef\u80fd\u8fben\u6b21\u7684\u60c5\u51b5&#xff1a;<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214958-689bb70615029.png\" \/><\/p>\n<h2>6.3\u4e3a\u4ec0\u4e48in_q\u8981\u8bb0\u5f55\u67d0\u4e2a\u70b9\u662f\u5426\u5728\u961f\u5217\u91cc&#xff1a;<\/h2>\n<p>\u5176\u5b9e\u5c31\u662f\u9632\u91cd\u590d\u64cd\u4f5c&#xff1a;<\/p>\n<p>\u8bb0\u5f55\u662f\u5426\u5728\u961f\u5217\u4e2d\u9632\u6b62\u91cd\u590d\u64cd\u4f5c&#xff1b;\u56e0\u4e3a\u5982\u679c\u961f\u5217\u6709\u76f8\u540c\u7684\u70b9&#xff1b;\u90a3\u4e48\u5bf9\u4e00\u4e2a\u70b9\u64cd\u4f5c\u5b8c\u53ea\u53ef\u80fd\u6539\u53d8dist p&#xff1b;\u81ea\u8eab\u7684dist\u4e0d\u4f1a\u53d8&#xff1b;\u4e5f\u5c31\u662f\u5f53\u518d\u6b21\u904d\u5386\u5230\u4e0b\u4e00\u4e2a\u540c\u70b9\u7684\u65f6\u5019&#xff1b;\u4e0d\u4f1a\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u4e3e\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\u4e00\u4e0b&#xff1a;<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214958-689bb7064f885.png\" \/><\/p>\n<p>\u662f\u4e0d\u662f\u4e00\u4e0b\u5b50\u5c31\u604d\u7136\u5927\u609f\u4e86\u54c8\u54c8\u54c8&#xff01;&#xff01;&#xff01;\u00a0<\/p>\n<\/p>\n<h2>\u4e03.\u7b97\u6cd5\u590d\u6742\u5ea6\u5206\u6790&#xff1a;<\/h2>\n<p>\u4e0b\u9762\u6211\u4eec\u4ece\u65f6\u95f4\u548c\u7a7a\u95f4\u4e24\u65b9\u9762\u6765\u5206\u6790\u4e00\u4e0b&#xff1a;<\/p>\n<h2>7.1\u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;<\/h2>\n<li>\u5e73\u5747\u60c5\u51b5&#xff1a;\u5728\u5e73\u5747\u60c5\u51b5\u4e0b&#xff0c;SPFA \u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(km)&#xff0c;\u5176\u4e2d\u662f\u4e00\u4e2a\u8f83\u5c0f\u7684\u5e38\u6570&#xff0c;\u901a\u5e38\u8fdc\u5c0f\u4e8e\u9876\u70b9\u6570n&#xff0c;m\u662f\u8fb9\u6570\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u901a\u8fc7\u961f\u5217\u4f18\u5316&#xff0c;\u53ea\u5904\u7406\u90a3\u4e9b\u53ef\u80fd\u4f1a\u66f4\u65b0\u8ddd\u79bb\u7684\u9876\u70b9&#xff0c;\u907f\u514d\u4e86 Bellman &#8211; Ford \u7b97\u6cd5\u4e2d\u5bf9\u6240\u6709\u8fb9\u7684\u591a\u6b21\u65e0\u6548\u677e\u5f1b\u64cd\u4f5c&#xff0c;\u5927\u5927\u51cf\u5c11\u4e86\u8ba1\u7b97\u91cf\u3002<\/li>\n<li>\u6700\u574f\u60c5\u51b5&#xff1a;\u7136\u800c&#xff0c;\u5728\u6700\u574f\u60c5\u51b5\u4e0b&#xff0c;\u6bd4\u5982\u5f53\u56fe\u5b58\u5728\u5927\u91cf\u8d1f\u6743\u8fb9\u4e14\u7ed3\u6784\u7279\u6b8a\u65f6&#xff0c;SPFA \u7b97\u6cd5\u4f1a\u9000\u5316\u4e3a Bellman &#8211; Ford \u7b97\u6cd5\u3002\u6b64\u65f6&#xff0c;\u5b83\u9700\u8981\u5bf9\u6240\u6709\u8fb9\u8fdb\u884cn-1\u6b21\u677e\u5f1b\u64cd\u4f5c&#xff08;n\u4e3a\u9876\u70b9\u6570&#xff09;&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u53d8\u4e3aO(nm)\u3002\u4f8b\u5982&#xff0c;\u5f53\u56fe\u662f\u4e00\u4e2a\u94fe\u72b6\u7ed3\u6784\u4e14\u5b58\u5728\u8d1f\u6743\u8fb9\u65f6&#xff0c;SPFA \u7b97\u6cd5\u53ef\u80fd\u4f1a\u4e0d\u65ad\u5730\u5bf9\u94fe\u4e0a\u7684\u9876\u70b9\u8fdb\u884c\u5165\u961f\u548c\u677e\u5f1b\u64cd\u4f5c&#xff0c;\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6\u6025\u5267\u4e0a\u5347\u3002<\/li>\n<h2>7.2\u7a7a\u95f4\u590d\u6742\u5ea6&#xff1a;<\/h2>\n<li>\u5b58\u50a8\u56fe\u7684\u7ed3\u6784&#xff1a;\u7b97\u6cd5\u9700\u8981\u5b58\u50a8\u56fe\u7684\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\u7684\u7ed3\u6784\u3002\u5bf9\u4e8e\u4e00\u4e2a\u6709m\u6761\u8fb9\u7684\u56fe&#xff0c;\u5b58\u50a8\u90bb\u63a5\u8868\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(m)\u3002<\/li>\n<li>\u8f85\u52a9\u6570\u7ec4&#xff1a;\u8fd8\u9700\u8981\u5b58\u50a8\u8ddd\u79bb\u6570\u7ec4dist\u3001\u9876\u70b9\u662f\u5426\u5728\u961f\u5217\u4e2d\u7684\u6570\u7ec4in_q\u4ee5\u53ca\u5165\u961f\u6b21\u6570\u6570\u7ec4cnt\u3002\u8fd9\u4e9b\u6570\u7ec4\u7684\u5927\u5c0f\u90fd\u4e0e\u9876\u70b9\u6570n\u76f8\u5173&#xff0c;\u6bcf\u4e2a\u6570\u7ec4\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\u3002\u56e0\u6b64&#xff0c;\u603b\u4f53\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(n&#043;m)\u3002<\/li>\n<h2>\u516b\u00b7SLF\u4f18\u5316\u53caLLL\u4f18\u5316\u7b56\u7565&#xff1a;<\/h2>\n<p>\u6211\u4eec\u4e0d\u96be\u53d1\u73b0\u8fd9\u6837\u666e\u901a\u7684SPFA\u7b97\u6cd5\u6bcf\u6b21\u53ea\u80fd\u53d6\u5230\u961f\u5934\u7684\u5143\u7d20&#xff1b;\u8fd9\u6837\u6709\u53ef\u80fddist\u6bd4\u8f83\u5c0f\u7684\u4e0d\u662f\u5148\u53d6\u5230&#xff1b;\u5bfc\u81f4\u4e86\u672c\u6765\u53ef\u4ee5\u63d0\u524d\u786e\u5b9a\u597d\u6700\u77ed\u8def\u5f84&#xff1b;\u6216\u53ef\u80fd\u662f\u6700\u77ed\u8def\u5f84\u7684\u70b9\u5728\u540e\u9762\u624d\u4f1a\u88ab\u64cd\u4f5c&#xff1b;\u8fd9\u6837\u4e0d\u5c31\u76f8\u5f53\u4e8e\u9ebb\u70e6\u901f\u5ea6\u53d8\u6162\u4e86\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528deque\u6839\u636e\u4e0b\u9762\u4e24\u4e2a\u4f18\u5316\u65b9\u9762\u8fdb\u884c\u5c0f\u5c0f\u4f18\u5316&#xff1a;<\/p>\n<p>SLF&#xff1a;\u5373\u63d2\u5165\u65f6\u5019\u505a\u4f18\u5316&#xff1b;LLL&#xff1a;\u9009\u62e9\u5143\u7d20\u5220\u9664\u961f\u5217\u65f6\u505a\u4f18\u5316\u3002<\/p>\n<h2>8.1SLF&#xff08;Small Label First&#xff09;\u4f18\u5316&#xff1a;<\/h2>\n<h2>8.1.1\u4f18\u5316\u539f\u7406&#xff1a;<\/h2>\n<p>\u5728\u6bcf\u6b21\u53d6\u51fa\u961f\u9996\u9876\u70b9\u65f6&#xff0c;LLL \u4f18\u5316\u7b56\u7565\u4f1a\u68c0\u67e5\u961f\u9996\u9876\u70b9\u7684\u8ddd\u79bb\u662f\u5426\u5927\u4e8e\u5f53\u524d\u5df2\u786e\u5b9a\u7684\u6700\u77ed\u8def\u5f84\u957f\u5ea6\u7684\u5e73\u5747\u503c\u3002\u5982\u679c\u5927\u4e8e&#xff0c;\u5219\u5c06\u961f\u9996\u9876\u70b9\u79fb\u5230\u961f\u5c3e&#xff0c;\u4f18\u5148\u5904\u7406\u8ddd\u79bb\u8f83\u5c0f\u7684\u9876\u70b9\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u907f\u514d\u957f\u65f6\u95f4\u5904\u7406\u8ddd\u79bb\u8f83\u5927\u7684\u9876\u70b9&#xff0c;\u9632\u6b62\u7b97\u6cd5\u5728\u4e00\u4e9b\u8ddd\u79bb\u8f83\u5927\u4f46\u5bf9\u6700\u7ec8\u7ed3\u679c\u5f71\u54cd\u8f83\u5c0f\u7684\u9876\u70b9\u4e0a\u6d6a\u8d39\u8fc7\u591a\u65f6\u95f4&#xff0c;\u4ece\u800c\u63d0\u9ad8\u7b97\u6cd5\u7684\u6574\u4f53\u6548\u7387\u3002<\/p>\n<h2>8.1.2\u5b9e\u73b0\u65b9\u5f0f&#xff1a;<\/h2>\n<p>\u5728\u53d6\u51fa\u961f\u9996\u9876\u70b9\u65f6\u589e\u52a0\u5224\u65ad\u903b\u8f91&#xff0c;\u6839\u636e\u6761\u4ef6\u5c06\u9876\u70b9\u79fb\u5230\u961f\u5c3e\u3002\u5b9e\u73b0\u65f6\u9700\u8981\u989d\u5916\u8bb0\u5f55\u5df2\u786e\u5b9a\u7684\u6700\u77ed\u8def\u5f84\u957f\u5ea6\u4e4b\u548c\u4ee5\u53ca\u5df2\u5904\u7406\u7684\u9876\u70b9\u6570&#xff0c;\u4ee5\u4fbf\u8ba1\u7b97\u5e73\u5747\u503c\u3002<\/p>\n<p>\u7b80\u5355\u6765\u8bf4\u5c31\u662f&#xff1a;\u8fd9\u91cc\u7528\u53cc\u7aef\u961f\u5217\u8fdb\u884c\u4f18\u5316&#xff1b;\u53ef\u4ee5\u66f4\u5feb\u7684\u786e\u5b9a\u67d0\u70b9\u7684\u6700\u77ed\u8def\u5f84&#xff1b;\u63d0\u524d\u66f4\u65b0\u51fa\u5230\u67d0\u70b9\u7684\u53ef\u80fd\u662f\u7684\u6700\u77ed\u8def\u5f84&#xff1b;\u52a0\u5feb\u4e86\u627e\u5230\u771f\u6b63\u7684\u6700\u77ed\u8def\u5f84\u6216\u8005\u4ee5\u53ca\u627e\u5230\u5f53\u524d\u70b9\u7684\u6700\u77ed\u8def\u5f84&#xff1b;\u4ee5\u53ca\u540e\u5e8f\u7684\u5176\u4ed6\u70b9\u7684\u6700\u77ed\u8def\u7684\u901f\u5ea6\u3002<\/p>\n<p>\u56e0\u6b64\u6211\u4eec\u53ea\u9700\u8981\u5728\u6e90\u4ee3\u7801\u7279\u5224\u4e00\u4e0b&#xff1a;\u5728\u5165\u961f\u5217\u7684\u65f6\u5019\u548c\u961f\u5934\u5143\u7d20\u6bd4\u8f83\u4e00\u4e0b&#xff1b;\u5982\u679cdist\u8981\u5c0f\u4e8e\u961f\u5934&#xff1b;\u90a3\u4e48\u5c31\u63d2\u961f\u5934\u5426\u5219\u63d2\u961f\u5c3e\u3002<\/p>\n<h2>8.1.3 SLF\u4f18\u5316\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/h2>\n<p>#include&lt;iostream&gt;<br \/>\n#include&lt;vector&gt;<br \/>\n#include&lt;algorithm&gt;<br \/>\n#include&lt;map&gt;<br \/>\n#include&lt;queue&gt;<br \/>\nusing namespace std;<br \/>\nint maxi &#061; 0x3f3f3f3f;<br \/>\nconst int N &#061; 1e6 &#043; 1;<br \/>\nint dist[N], n, m;<br \/>\nint pre[N];\/\/\u524d\u9a71\u8282\u70b9<br \/>\nvector&lt;pair&lt;int, int&gt;&gt; v[N];<br \/>\ndeque&lt;int&gt;q;<br \/>\nbool in_q[N] &#061; { 0 };<br \/>\nint cnt[N] &#061; { 0 };\/\/\u8bb0\u5f55\u8282\u70b9\u5165\u961f\u5217\u6b21\u6570<br \/>\nbool SPFA(int s, int n) {\/\/n\u4e2a\u70b9<br \/>\nfill(dist &#043; 1, dist &#043; n &#043; 1, maxi);\/\/\u521d\u59cb\u5316dist\u8981\u6ce8\u610f&#xff1a;\u6211\u4eec\u4ece1-n\u7684\u70b9\u7684\u7f16\u53f7&#xff1b;\u4e5f\u5c31\u662f\u521d\u59cb\u5316\u8fd9\u4e2a\u533a\u95f4&#xff08;[\u8fed\u4ee3\u5668)&#xff09;<br \/>\ndist[s] &#061; 0;<br \/>\nq.push_back(s);<br \/>\nin_q[s] &#061; 1;<br \/>\ncnt[s]&#043;&#043;;<br \/>\nwhile (!q.empty()) {<br \/>\nint cur &#061; q.front();<br \/>\nq.pop_front();<br \/>\nin_q[cur] &#061; 0;<br \/>\nfor (auto [p, w] : v[cur]) {<br \/>\nif (dist[cur] !&#061; maxi &amp;&amp; dist[cur] &#043; w &lt; dist[p]) {<br \/>\ndist[p] &#061; dist[cur] &#043; w;<br \/>\npre[p] &#061; cur;<br \/>\nif (!in_q[p]) {<br \/>\n\/\/\u8fd9\u91cc\u7528\u53cc\u7aef\u961f\u5217\u8fdb\u884c\u4f18\u5316&#xff1b;\u53ef\u4ee5\u66f4\u5feb\u7684\u786e\u5b9a\u67d0\u70b9\u7684\u6700\u77ed\u8def\u5f84&#xff1b;\u63d0\u524d\u66f4\u65b0\u51fa\u5230\u67d0\u70b9\u7684\u53ef\u80fd\u662f\u7684\u6700\u77ed\u8def\u5f84<br \/>\n\/\/\u52a0\u5feb\u4e86\u627e\u5230\u771f\u6b63\u7684\u6700\u77ed\u8def\u5f84\u6216\u8005\u4ee5\u53ca\u627e\u5230\u5f53\u524d\u70b9\u7684\u6700\u77ed\u8def\u5f84&#xff1b;\u4ee5\u53ca\u540e\u5e8f\u7684\u5176\u4ed6\u70b9\u7684\u6700\u77ed\u8def\u7684\u901f\u5ea6<br \/>\nif (!q.empty() &amp;&amp; dist[p] &lt; dist[q.front()]) {<br \/>\nq.push_front(p);<br \/>\n}<br \/>\nelse {<br \/>\nq.push_back(p);<br \/>\n}<br \/>\nin_q[p] &#061; 1;<br \/>\ncnt[p]&#043;&#043;;<br \/>\nif (cnt[p] &gt; n) return 0;<\/p>\n<p>}<br \/>\n}<br \/>\n}<\/p>\n<p>}<\/p>\n<p>return 1;<\/p>\n<p>}<\/p>\n<p>void init_pre() {<\/p>\n<p>for (int i &#061; 1; i &lt;&#061; n; i&#043;&#043;)pre[i] &#061; i;<br \/>\n}<br \/>\nint getpath(int x) {<br \/>\npre[x] &#061;&#061; x ? x : getpath(pre[x]);<br \/>\ncout &lt;&lt; x &lt;&lt; &#034; &#034;;<br \/>\nreturn x;<br \/>\n}<\/p>\n<p>int main() {<br \/>\ncin &gt;&gt; n &gt;&gt; m;<br \/>\nfor (int i &#061; 0; i &lt; m; i&#043;&#043;) {<br \/>\nint x, y, z;<br \/>\n\/\/\u5355\u53cc\u5411\u8fb9\u90fd\u53ef&#xff1a;<br \/>\ncin &gt;&gt; x &gt;&gt; y &gt;&gt; z;<br \/>\nv[x].emplace_back(y, z);<br \/>\n}<br \/>\ninit_pre();<\/p>\n<p>bool flag &#061; SPFA(1, n);<br \/>\nif (flag) {<br \/>\nif (dist[n] &#061;&#061; maxi) cout &lt;&lt; &#034;unconnected&#034; &lt;&lt; endl;<br \/>\nelse {<br \/>\ncout &lt;&lt; dist[n] &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;\u6700\u77ed\u8def\u5f84&#xff1a;&#034; &lt;&lt; endl;<br \/>\ngetpath(n);<br \/>\n}<br \/>\n}<br \/>\nelse {<br \/>\ncout &lt;&lt; &#034;exist negative circle &#034; &lt;&lt; endl;<br \/>\n}<br \/>\nreturn 0;<\/p>\n<p>}<\/p>\n<h2>8.2LLL&#xff08;Large Label Last&#xff09;\u4f18\u5316&#xff1a;<\/h2>\n<h2>8.2.1\u4f18\u5316\u539f\u7406&#xff1a;<\/h2>\n<p>\u5728\u6bcf\u6b21\u53d6\u51fa\u961f\u9996\u9876\u70b9\u65f6&#xff0c;LLL \u4f18\u5316\u7b56\u7565\u4f1a\u68c0\u67e5\u961f\u9996\u9876\u70b9\u7684\u8ddd\u79bb\u662f\u5426\u5927\u4e8e\u5f53\u524d\u5df2\u786e\u5b9a\u7684\u6700\u77ed\u8def\u5f84\u957f\u5ea6\u7684\u5e73\u5747\u503c\u3002\u5982\u679c\u5927\u4e8e&#xff0c;\u5219\u5c06\u961f\u9996\u9876\u70b9\u79fb\u5230\u961f\u5c3e&#xff0c;\u4f18\u5148\u5904\u7406\u8ddd\u79bb\u8f83\u5c0f\u7684\u9876\u70b9\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u907f\u514d\u957f\u65f6\u95f4\u5904\u7406\u8ddd\u79bb\u8f83\u5927\u7684\u9876\u70b9&#xff0c;\u9632\u6b62\u7b97\u6cd5\u5728\u4e00\u4e9b\u8ddd\u79bb\u8f83\u5927\u4f46\u5bf9\u6700\u7ec8\u7ed3\u679c\u5f71\u54cd\u8f83\u5c0f\u7684\u9876\u70b9\u4e0a\u6d6a\u8d39\u8fc7\u591a\u65f6\u95f4&#xff0c;\u4ece\u800c\u63d0\u9ad8\u7b97\u6cd5\u7684\u6574\u4f53\u6548\u7387\u3002<\/p>\n<h2>8.2.2\u5b9e\u73b0\u65b9\u5f0f&#xff1a;<\/h2>\n<p>\u5728\u53d6\u51fa\u961f\u9996\u9876\u70b9\u65f6\u589e\u52a0\u5224\u65ad\u903b\u8f91&#xff0c;\u6839\u636e\u6761\u4ef6\u5c06\u9876\u70b9\u79fb\u5230\u961f\u5c3e\u3002\u5b9e\u73b0\u65f6\u9700\u8981\u989d\u5916\u8bb0\u5f55\u5df2\u786e\u5b9a\u7684\u6700\u77ed\u8def\u5f84\u957f\u5ea6\u4e4b\u548c\u4ee5\u53ca\u5df2\u5904\u7406\u7684\u9876\u70b9\u6570&#xff0c;\u4ee5\u4fbf\u8ba1\u7b97\u5e73\u5747\u503c\u3002<\/p>\n<p>\u7b80\u5355\u6765\u8bf4\u5c31\u662f&#xff1a;\u5728\u7b26\u5408front\u5904\u7684\u8282\u70b9\u5927\u4e8e\u5e73\u5747\u503c\u7684\u65f6\u5019&#xff1b;\u4e00\u76f4\u628a\u5b83\u79fb\u52a8\u5230back\u5904&#xff1b;\u76f4\u5230\u53d1\u73b0\u5c0f\u4e8e\u4e86&#xff1b;\u505c\u6b62&#xff1b;\u65b9\u4fbf\u540e\u9762\u53d6\u5f97front\u7684\u65f6\u5019\u662f\u76f8\u5bf9\u5c0f\u7684dsit&#xff1b;\u540cslf\u4f18\u5316&#xff1b;\u5c3d\u53ef\u80fd\u5148\u64cd\u4f5c\u8f83\u5c0f\u7684dist\u7684\u70b9\u3002<\/p>\n<p>\u4e5f\u5c31\u662f&#xff1a;\u6211\u4eec\u6bcf\u6b21\u9009\u62e9\u961f\u5934\u5143\u7d20\u65f6\u5019\u628a\u5b83\u7684dsit\u548c\u5168\u961f\u7684dist\u6bd4\u8f83\u5982\u679c\u5c0f\u5c31\u9000\u51fa\u9009\u961f\u5934\u5f97\u65f6\u5019\u5c31\u4f1a\u9009\u5b83&#xff1b;\u5426\u5219\u628a\u5b83\u5e72\u5230\u961f\u5c3e&#xff1b;\u518d\u6b21\u6bd4\u8f83\u961f\u5934\u548c\u5e73\u5747\u503c\u3002<\/p>\n<h2>8.2.3LLL\u4f18\u5316\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/h2>\n<\/p>\n<p>#include&lt;iostream&gt;<br \/>\n#include&lt;vector&gt;<br \/>\n#include&lt;algorithm&gt;<br \/>\n#include&lt;map&gt;<br \/>\n#include&lt;queue&gt;<br \/>\nusing namespace std;<br \/>\nint maxi &#061; 0x3f3f3f3f;<br \/>\nconst int N &#061; 1e6 &#043; 1;<br \/>\nint dist[N], n, m;<br \/>\nint pre[N];\/\/\u524d\u9a71\u8282\u70b9<br \/>\nvector&lt;pair&lt;int, int&gt;&gt; v[N];<br \/>\ndeque&lt;int&gt;q;<br \/>\nbool in_q[N] &#061; { 0 };<br \/>\nint cnt[N] &#061; { 0 };\/\/\u8bb0\u5f55\u8282\u70b9\u5165\u961f\u5217\u6b21\u6570<br \/>\nbool SPFA(int s, int n) {\/\/n\u4e2a\u70b9<br \/>\nfill(dist &#043; 1, dist &#043; n &#043; 1, maxi);\/\/\u521d\u59cb\u5316dist\u8981\u6ce8\u610f&#xff1a;\u6211\u4eec\u4ece1-n\u7684\u70b9\u7684\u7f16\u53f7&#xff1b;\u4e5f\u5c31\u662f\u521d\u59cb\u5316\u8fd9\u4e2a\u533a\u95f4&#xff08;[\u8fed\u4ee3\u5668)&#xff09;<br \/>\ndist[s] &#061; 0;<br \/>\nq.push_back(s);<br \/>\nin_q[s] &#061; 1;<br \/>\ncnt[s]&#043;&#043;;<br \/>\nint sum_dist &#061; 0;  \/\/ \u961f\u5217\u4e2d\u6240\u6709\u8282\u70b9\u7684\u8ddd\u79bb\u603b\u548c<br \/>\nint queue_size &#061; 1;  \/\/ \u961f\u5217\u4e2d\u8282\u70b9\u7684\u6570\u91cf<br \/>\nwhile (!q.empty()) {<br \/>\n   \/\/\/LLL\u4f18\u5316&#xff1a;\u5728\u7b26\u5408front\u5904\u7684\u8282\u70b9\u5927\u4e8e\u5e73\u5747\u503c\u7684\u65f6\u5019&#xff1b;\u4e00\u76f4\u628a\u5b83\u79fb\u52a8\u5230back\u5904&#xff1b;\u76f4\u5230\u53d1\u73b0\u5c0f\u4e8e\u4e86&#xff1b;\u505c\u6b62&#xff1b;\u65b9\u4fbf\u540e\u9762\u53d6\u5f97front\u7684\u65f6\u5019\u662f\u76f8\u5bf9\u5c0f\u7684dsit<br \/>\n \/\/\/\u540cslf\u4f18\u5316&#xff1b;\u5c3d\u53ef\u80fd\u5148\u64cd\u4f5c\u8f83\u5c0f\u7684dist\u7684\u70b9<br \/>\nwhile (!q.empty() &amp;&amp; dist[q.front()] * queue_size &gt; sum_dist) {<br \/>\nint front_node &#061; q.front();<br \/>\nq.pop_front();<br \/>\nq.push_back(front_node);<br \/>\n}<\/p>\n<p>int cur &#061; q.front();<br \/>\nq.pop_front();<br \/>\nin_q[cur] &#061; 0;<br \/>\nsum_dist -&#061; dist[cur];<br \/>\nqueue_size&#8211;;<\/p>\n<p>for (auto [p, w] : v[cur]) {<br \/>\nif (dist[cur] !&#061; maxi &amp;&amp; dist[cur] &#043; w &lt; dist[p]) {<br \/>\ndist[p] &#061; dist[cur] &#043; w;<br \/>\npre[p] &#061; cur;<br \/>\nif (!in_q[p]) {<br \/>\nq.push_back(p);<br \/>\nin_q[p] &#061; 1;<br \/>\ncnt[p]&#043;&#043;;<br \/>\nsum_dist &#043;&#061; dist[p];<br \/>\nqueue_size&#043;&#043;;<\/p>\n<p>if (cnt[p] &gt; n) return 0;<\/p>\n<p>}<br \/>\n}<br \/>\n}<\/p>\n<p>}<\/p>\n<p>return 1;<\/p>\n<p>}<\/p>\n<p>void init_pre() {<\/p>\n<p>for (int i &#061; 1; i &lt;&#061; n; i&#043;&#043;)pre[i] &#061; i;<br \/>\n}<br \/>\nint getpath(int x) {<br \/>\npre[x] &#061;&#061; x ? x : getpath(pre[x]);<br \/>\ncout &lt;&lt; x &lt;&lt; &#034; &#034;;<br \/>\nreturn x;<br \/>\n}<\/p>\n<p>int main() {<br \/>\ncin &gt;&gt; n &gt;&gt; m;<br \/>\nfor (int i &#061; 0; i &lt; m; i&#043;&#043;) {<br \/>\nint x, y, z;<br \/>\n\/\/\u5355\u53cc\u5411\u8fb9\u90fd\u53ef&#xff1a;<br \/>\ncin &gt;&gt; x &gt;&gt; y &gt;&gt; z;<br \/>\nv[x].emplace_back(y, z);<br \/>\n}<br \/>\ninit_pre();<\/p>\n<p>bool flag &#061; SPFA(1, n);<br \/>\nif (flag) {<br \/>\nif (dist[n] &#061;&#061; maxi) cout &lt;&lt; &#034;unconnected&#034; &lt;&lt; endl;<br \/>\nelse {<br \/>\ncout &lt;&lt; dist[n] &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;\u6700\u77ed\u8def\u5f84&#xff1a;&#034; &lt;&lt; endl;<br \/>\ngetpath(n);<br \/>\n}<br \/>\n}<br \/>\nelse {<br \/>\ncout &lt;&lt; &#034;exist negative circle &#034; &lt;&lt; endl;<br \/>\n}<br \/>\nreturn 0;<\/p>\n<p>}<\/p>\n<p>LLL&#043;SLF\u7684deque\u4f18\u5316&#xff1a;<\/p>\n<h2>8.3SLF\u4e0eLLL\u8054\u5408\u4f18\u5316&#xff1a;<\/h2>\n<h2>8.3.1\u4f18\u5316\u539f\u7406&#xff1a;<\/h2>\n<p>\u5c06 SLF \u548c LLL \u4e24\u79cd\u4f18\u5316\u65b9\u6cd5\u7ed3\u5408\u4f7f\u7528&#xff0c;\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u7b97\u6cd5\u5728\u5404\u79cd\u56fe\u7ed3\u6784\u4e0b\u7684\u6027\u80fd\u3002SLF \u7b56\u7565\u5728\u5165\u961f\u65f6\u4f18\u5148\u5904\u7406\u8ddd\u79bb\u5c0f\u7684\u9876\u70b9&#xff0c;\u800c LLL \u7b56\u7565\u5728\u51fa\u961f\u65f6\u907f\u514d\u957f\u65f6\u95f4\u5904\u7406\u8ddd\u79bb\u5927\u7684\u9876\u70b9&#xff0c;\u4e24\u8005\u76f8\u4e92\u914d\u5408&#xff0c;\u80fd\u66f4\u6709\u6548\u5730\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97&#xff0c;\u52a0\u5feb\u7b97\u6cd5\u6536\u655b\u3002<\/p>\n<h2>8.3.2\u5b9e\u73b0\u65b9\u5f0f&#xff1a;<\/h2>\n<p>\u5728\u4ee3\u7801\u4e2d\u540c\u65f6\u5b9e\u73b0 SLF \u548c LLL \u7684\u903b\u8f91\u3002\u5728\u5165\u961f\u65f6\u91c7\u7528 SLF \u7684\u5165\u961f\u65b9\u5f0f&#xff0c;\u5728\u51fa\u961f\u65f6\u91c7\u7528 LLL \u7684\u5224\u65ad\u548c\u5904\u7406\u65b9\u5f0f&#xff0c;\u901a\u8fc7\u5408\u7406\u7684\u903b\u8f91\u7ec4\u5408&#xff0c;\u5145\u5206\u53d1\u6325\u4e24\u79cd\u4f18\u5316\u7b56\u7565\u7684\u4f18\u52bf\u3002<\/p>\n<p>\u8fd8\u662f\u7b80\u5355\u8bf4\u4e00\u4e0b&#xff1a;\u6211\u4eec\u7684LLL\u548cSLF\u4f18\u5316\u90fd\u662f\u8ba9\u6bcf\u6b21\u9009\u62e9\u961f\u5934\u5143\u7d20\u7684\u65f6\u5019\u64cd\u4f5c\u7684\u5c3d\u91cf\u662f\u6e90\u70b9\u8ddd\u5c0f\u7684\u70b9&#xff1b;\u90a3\u4e48\u5b83\u6709\u53ef\u80fd\u5c31\u662f\u6700\u77ed\u6e90\u70b9\u8ddd&#xff1b;\u6216\u8005\u5176\u4ed6\u70b9\u53ef\u4ee5\u501f\u52a9\u5b83\u5b9e\u73b0\u6700\u77ed\u6e90\u70b9\u8ddd\u7684\u67e5\u627e\u4ece\u800c\u66f4\u5feb\u7684\u627e\u5230\u5bf9\u5e94\u7684\u6700\u7ec8\u7684dist\u503c\u3002<\/p>\n<p>\u5f52\u6839\u7ed3\u5e95&#xff1a;\u5c31\u662f\u4e3a\u4e86\u63d0\u524d\u64cd\u4f5c\u6e90\u70b9\u8ddd\u66f4\u5c0f\u7684\u70b9&#xff1b;\u8ba9\u6bcf\u4e2a\u70b9\u7684\u6700\u7ec8dist\u503c\u5c3d\u5feb\u66f4\u65b0\u5b8c&#xff1b;\u51cf\u5c11\u70b9\u5165\u961f\u5217\u7684\u6b21\u6570&#xff0c;\u63d0\u524d\u7ec8\u6b62\u5165\u51fa\u961f\u5217\u64cd\u4f5c\u3002<\/p>\n<h2>8.3.3SLF\u4e0eLLL\u8054\u5408\u4f18\u5316\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/h2>\n<p>#include&lt;iostream&gt;<br \/>\n#include&lt;vector&gt;<br \/>\n#include&lt;algorithm&gt;<br \/>\n#include&lt;map&gt;<br \/>\n#include&lt;queue&gt;<br \/>\nusing namespace std;<br \/>\nint maxi &#061; 0x3f3f3f3f;<br \/>\nconst int N &#061; 1e6 &#043; 1;<br \/>\nint dist[N], n, m;<br \/>\nint pre[N];\/\/\u524d\u9a71\u8282\u70b9<br \/>\nvector&lt;pair&lt;int, int&gt;&gt; v[N];<br \/>\ndeque&lt;int&gt;q;<br \/>\nbool in_q[N] &#061; { 0 };<br \/>\nint cnt[N] &#061; { 0 };\/\/\u8bb0\u5f55\u8282\u70b9\u5165\u961f\u5217\u6b21\u6570<br \/>\nbool SPFA(int s, int n) {\/\/n\u4e2a\u70b9<br \/>\nfill(dist &#043; 1, dist &#043; n &#043; 1, maxi);\/\/\u521d\u59cb\u5316dist\u8981\u6ce8\u610f&#xff1a;\u6211\u4eec\u4ece1-n\u7684\u70b9\u7684\u7f16\u53f7&#xff1b;\u4e5f\u5c31\u662f\u521d\u59cb\u5316\u8fd9\u4e2a\u533a\u95f4&#xff08;[\u8fed\u4ee3\u5668)&#xff09;<br \/>\ndist[s] &#061; 0;<br \/>\nq.push_back(s);<br \/>\nin_q[s] &#061; 1;<br \/>\ncnt[s]&#043;&#043;;<br \/>\nint sum_dist &#061; 0;  \/\/ \u961f\u5217\u4e2d\u6240\u6709\u8282\u70b9\u7684\u8ddd\u79bb\u603b\u548c<br \/>\nint queue_size &#061; 1;  \/\/ \u961f\u5217\u4e2d\u8282\u70b9\u7684\u6570\u91cf<br \/>\nwhile (!q.empty()) {<br \/>\n\/\/\/LLL\u4f18\u5316&#xff1a;\u5728\u7b26\u5408front\u5904\u7684\u8282\u70b9\u5927\u4e8e\u5e73\u5747\u503c\u7684\u65f6\u5019&#xff1b;\u4e00\u76f4\u628a\u5b83\u79fb\u52a8\u5230back\u5904&#xff1b;\u76f4\u5230\u53d1\u73b0\u5c0f\u4e8e\u4e86&#xff1b;\u505c\u6b62&#xff1b;\u65b9\u4fbf\u540e\u9762\u53d6\u5f97front\u7684\u65f6\u5019\u662f\u76f8\u5bf9\u5c0f\u7684dsit<br \/>\n\/\/ \/\/\/\u540cslf\u4f18\u5316&#xff1b;\u5c3d\u53ef\u80fd\u5148\u64cd\u4f5c\u8f83\u5c0f\u7684dist\u7684\u70b9<br \/>\nwhile (!q.empty() &amp;&amp; dist[q.front()] * queue_size &gt; sum_dist) {<br \/>\nint front_node &#061; q.front();<br \/>\nq.pop_front();<br \/>\nq.push_back(front_node);<br \/>\n}<\/p>\n<p>int cur &#061; q.front();<br \/>\nq.pop_front();<br \/>\nin_q[cur] &#061; 0;<br \/>\nsum_dist -&#061; dist[cur];<br \/>\nqueue_size&#8211;;<\/p>\n<p>for (auto [p, w] : v[cur]) {<br \/>\nif (dist[cur] !&#061; maxi &amp;&amp; dist[cur] &#043; w &lt; dist[p]) {<br \/>\ndist[p] &#061; dist[cur] &#043; w;<br \/>\npre[p] &#061; cur;<br \/>\nif (!in_q[p]) {<br \/>\n\/\/\u8fd9\u91cc\u7528\u53cc\u7aef\u961f\u5217\u8fdb\u884c\u4f18\u5316&#xff1b;\u53ef\u4ee5\u66f4\u5feb\u7684\u786e\u5b9a\u67d0\u70b9\u7684\u6700\u77ed\u8def\u5f84&#xff1b;\u63d0\u524d\u66f4\u65b0\u51fa\u5230\u67d0\u70b9\u7684\u53ef\u80fd\u662f\u7684\u6700\u77ed\u8def\u5f84<br \/>\n\/\/\/\/\u52a0\u5feb\u4e86\u627e\u5230\u771f\u6b63\u7684\u6700\u77ed\u8def\u5f84\u6216\u8005\u4ee5\u53ca\u627e\u5230\u5f53\u524d\u70b9\u7684\u6700\u77ed\u8def\u5f84&#xff1b;\u4ee5\u53ca\u540e\u5e8f\u7684\u5176\u4ed6\u70b9\u7684\u6700\u77ed\u8def\u7684\u901f\u5ea6<br \/>\nif (!q.empty() &amp;&amp; dist[p] &lt; dist[q.front()]) {<br \/>\nq.push_front(p);<br \/>\n}<br \/>\nelse {<br \/>\nq.push_back(p);<br \/>\n}<br \/>\nin_q[p] &#061; 1;<br \/>\ncnt[p]&#043;&#043;;<br \/>\nsum_dist &#043;&#061; dist[p];<br \/>\nqueue_size&#043;&#043;;<\/p>\n<p>if (cnt[p] &gt; n) return 0;<\/p>\n<p>}<br \/>\n}<br \/>\n}<\/p>\n<p>}<\/p>\n<p>return 1;<\/p>\n<p>}<\/p>\n<p>void init_pre() {<\/p>\n<p>for (int i &#061; 1; i &lt;&#061; n; i&#043;&#043;)pre[i] &#061; i;<br \/>\n}<br \/>\nint getpath(int x) {<br \/>\npre[x] &#061;&#061; x ? x : getpath(pre[x]);<br \/>\ncout &lt;&lt; x &lt;&lt; &#034; &#034;;<br \/>\nreturn x;<br \/>\n}<\/p>\n<p>int main() {<br \/>\ncin &gt;&gt; n &gt;&gt; m;<br \/>\nfor (int i &#061; 0; i &lt; m; i&#043;&#043;) {<br \/>\nint x, y, z;<br \/>\n\/\/\u5355\u53cc\u5411\u8fb9\u90fd\u53ef&#xff1a;<br \/>\ncin &gt;&gt; x &gt;&gt; y &gt;&gt; z;<br \/>\nv[x].emplace_back(y, z);<br \/>\n}<br \/>\ninit_pre();<\/p>\n<p>bool flag &#061; SPFA(1, n);<br \/>\nif (flag) {<br \/>\nif (dist[n] &#061;&#061; maxi) cout &lt;&lt; &#034;unconnected&#034; &lt;&lt; endl;<br \/>\nelse {<br \/>\ncout &lt;&lt; dist[n] &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;\u6700\u77ed\u8def\u5f84&#xff1a;&#034; &lt;&lt; endl;<br \/>\ngetpath(n);<br \/>\n}<br \/>\n}<br \/>\nelse {<br \/>\ncout &lt;&lt; &#034;exist negative circle &#034; &lt;&lt; endl;<br \/>\n}<br \/>\nreturn 0;<\/p>\n<p>}<\/p>\n<h2>\u4e5d.\u7b97\u6cd5\u4f18\u52bf\u4e0e\u5c40\u9650\u6027:<\/h2>\n<h4>9.1\u4f18\u52bf&#xff1a;<\/h4>\n<li>\u53ef\u5904\u7406\u8d1f\u6743\u8fb9&#xff1a;\u80fd\u5e94\u5bf9\u5305\u542b\u8d1f\u6743\u8fb9\u7684\u56fe&#xff0c;\u50cf\u4ea4\u901a\u7f51\u7edc\u4e2d\u8fd0\u8f93\u8865\u8d34\u60c5\u51b5&#xff0c;\u53ef\u51c6\u786e\u8ba1\u7b97\u6700\u4f18\u8def\u5f84\u3002<\/li>\n<li>\u5e73\u5747\u590d\u6742\u5ea6\u4f4e&#xff1a;\u501f\u52a9\u961f\u5217\u4f18\u5316&#xff0c;\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(km) &#xff0c;\u5728\u7a00\u758f\u56fe\u4e2d\u6536\u655b\u5feb\u3002<\/li>\n<li>\u80fd\u68c0\u6d4b\u8d1f\u6743\u73af&#xff1a;\u901a\u8fc7\u8bb0\u5f55\u9876\u70b9\u5165\u961f\u6b21\u6570\u5224\u65ad\u8d1f\u6743\u73af&#xff0c;\u52a9\u4e8e\u91d1\u878d\u5957\u5229\u7b49\u573a\u666f\u51b3\u7b56\u3002<\/li>\n<li>\u5b9e\u73b0\u7b80\u5355&#xff1a;\u57fa\u4e8e\u961f\u5217\u548c\u677e\u5f1b\u64cd\u4f5c&#xff0c;\u4ee3\u7801\u7ed3\u6784\u6e05\u6670&#xff0c;\u9002\u5408\u521d\u5b66\u8005\u548c\u5feb\u901f\u5b9e\u73b0\u9700\u6c42\u3002<\/li>\n<h4>9.2\u5c40\u9650\u6027:<\/h4>\n<li>\u6700\u574f\u590d\u6742\u5ea6\u9ad8&#xff1a;\u7279\u6b8a\u56fe\u7ed3\u6784\u4e0b&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u4e3a O(nm)&#xff0c;\u8fd0\u884c\u65f6\u95f4\u957f\u3002<\/li>\n<li>\u6613\u88ab\u7279\u6b8a\u56fe\u5361\u65f6&#xff1a;\u5982\u5b8c\u5168\u56fe\u4f1a\u4f7f\u7b97\u6cd5\u9891\u7e41\u5165\u961f\u51fa\u961f&#xff0c;\u964d\u4f4e\u6027\u80fd\u3002<\/li>\n<li>\u7a7a\u95f4\u5f00\u9500\u5927&#xff1a;\u9700\u961f\u5217\u53ca\u591a\u4e2a\u6570\u7ec4&#xff0c;\u5927\u89c4\u6a21\u56fe\u6570\u636e\u53ef\u80fd\u5185\u5b58\u4e0d\u8db3\u3002<\/li>\n<li>\u4e0d\u9002\u7528\u4e8e\u7a20\u5bc6\u56fe&#xff1a;\u7a20\u5bc6\u56fe\u4e2d&#xff0c;\u961f\u5217\u548c\u677e\u5f1b\u64cd\u4f5c\u591a&#xff0c;\u6548\u7387\u4e0d\u5982\u4e13\u95e8\u7b97\u6cd5\u3002<\/li>\n<h2>\u5341\u00b7\u5e94\u7528\u573a\u666f&#xff1a;<\/h2>\n<h2>10.1\u4ea4\u901a\u7f51\u7edc\u89c4\u5212&#xff1a;<\/h2>\n<p>\u2460\u53ef\u5904\u7406\u8d1f\u6743\u8fb9&#xff0c;\u80fd\u5e94\u5bf9\u9053\u8def\u56e0\u7279\u6b8a\u6d3b\u52a8&#xff08;\u5982\u4fc3\u9500\u6d3b\u52a8\u5e26\u6765\u8fd0\u8f93\u8865\u8d34&#xff09;\u5bfc\u81f4\u6210\u672c\u4e3a\u8d1f\u7684\u60c5\u51b5&#xff0c;\u89c4\u5212\u51fa\u6700\u4f18\u8fd0\u8f93\u8def\u5f84\u3002<\/p>\n<p>\u2461\u5e73\u5747\u590d\u6742\u5ea6\u4f4e&#xff0c;\u5728\u57ce\u5e02\u4ea4\u901a\u7b49\u7a00\u758f\u56fe\u7ed3\u6784\u4e2d&#xff0c;\u80fd\u5feb\u901f\u7b97\u51fa\u4e24\u70b9\u95f4\u7684\u6700\u77ed\u51fa\u884c\u8def\u7ebf\u3002<\/p>\n<h2>10.2\u91d1\u878d\u5957\u5229\u5206\u6790&#xff1a;<\/h2>\n<p>\u80fd\u68c0\u6d4b\u8d1f\u6743\u73af&#xff0c;\u53ef\u53d1\u73b0\u91d1\u878d\u5e02\u573a\u4e2d\u56e0\u6c47\u7387\u5dee\u5f02\u3001\u5229\u7387\u6ce2\u52a8\u7b49\u56e0\u7d20\u5f62\u6210\u7684\u5957\u5229\u673a\u4f1a&#xff0c;\u8f85\u52a9\u6295\u8d44\u8005\u51b3\u7b56\u3002<\/p>\n<h2>10.3\u793e\u4ea4\u7f51\u7edc\u4fe1\u606f\u4f20\u64ad&#xff1a;<\/h2>\n<p>\u5e73\u5747\u590d\u6742\u5ea6\u4f4e&#xff0c;\u9002\u5408\u793e\u4ea4\u7f51\u7edc\u8fd9\u79cd\u7a00\u758f\u56fe&#xff0c;\u53ef\u8ba1\u7b97\u4fe1\u606f\u4ece\u4e00\u4e2a\u7528\u6237\u5230\u5176\u4ed6\u7528\u6237\u7684\u6700\u77ed\u4f20\u64ad\u8def\u5f84&#xff0c;\u52a9\u529b\u7cbe\u51c6\u8425\u9500\u548c\u4fe1\u606f\u6269\u6563\u7b56\u7565\u5236\u5b9a\u3002<\/p>\n<h2>10.4\u7ade\u8d5b\u4e0e\u5b66\u4e60&#xff1a;<\/h2>\n<p>\u5b9e\u73b0\u7b80\u5355&#xff0c;\u4fbf\u4e8e\u521d\u5b66\u8005\u7406\u89e3\u548c\u5feb\u901f\u5b9e\u73b0&#xff0c;\u5e38\u7528\u4e8e\u7b97\u6cd5\u7ade\u8d5b\u548c\u5b66\u4e60\u4e2d\u9a8c\u8bc1\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\u7684\u6b63\u786e\u6027\u3002<\/p>\n<p>\u8fd8\u6709\u5176\u4ed6\u7684\u7528\u9014\u7b49\u7b49&#xff1b;\u8fd9\u91cc\u5c31\u4e0d\u4e00\u4e00\u5217\u4e3e\u4e86\u3002\u00a0<\/p>\n<p>\u4f46\u662f\u8fd8\u6709\u4e9b\u7531\u4e8e\u5b83\u7684\u7f3a\u70b9\u662f\u4e0d\u80fd\u5e94\u7528\u7684&#xff1b;\u5982&#xff1a;<\/p>\n<li>\u5927\u89c4\u6a21\u7a20\u5bc6\u7f51\u7edc&#xff1a;\u5982\u96c6\u6210\u7535\u8def\u5e03\u7ebf\u95ee\u9898&#xff0c;SPFA \u53ef\u80fd\u56e0\u6700\u574f\u590d\u6742\u5ea6\u9ad8\u3001\u4e0d\u9002\u7528\u4e8e\u7a20\u5bc6\u56fe\u800c\u6548\u7387\u4f4e\u4e0b&#xff0c;\u53ef\u8003\u8651\u4f7f\u7528\u9488\u5bf9\u7a20\u5bc6\u56fe\u4f18\u5316\u7684\u7b97\u6cd5&#xff0c;\u5982\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u7684 Dijkstra \u7b97\u6cd5\u53d8\u79cd\u3002<\/li>\n<li>\u8d85\u5927\u89c4\u6a21\u56fe\u6570\u636e&#xff1a;\u5728\u5168\u7403\u4e92\u8054\u7f51\u62d3\u6251\u56fe\u5206\u6790\u4e2d&#xff0c;\u56e0\u7a7a\u95f4\u5f00\u9500\u5927\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u4e0d\u8db3&#xff0c;\u53ef\u8003\u8651\u91c7\u7528\u5206\u5e03\u5f0f\u7b97\u6cd5\u6216\u5185\u5b58\u4f18\u5316\u7b56\u7565\u3002<\/li>\n<h2>\u5341\u4e00\u00b7\u4e2a\u4eba\u5c0f\u603b\u7ed3&#xff1a;<\/h2>\n<p>\u672c\u7bc7\u6211\u4eec\u6240\u4ecb\u7ecd\u7684\u662fSPFA\u7b97\u6cd5\u4e5f\u5c31\u662f\u4f18\u5316\u540e\u7684Bellman-Ford\u7b97\u6cd5&#xff1b;\u4e0b\u9762\u535a\u4e3b\u603b\u7ed3\u4e86\u4e00\u4e9b\u4ee3\u7801\u5b9e\u73b0\u6280\u5de7\u53ca\u5176\u9002\u7528\u7684\u5f52\u7eb3&#xff1b;\u8fd8\u671b\u5bf9\u5927\u5bb6\u5b66\u4e60\u8fd9\u4e2a\u7b97\u6cd5\u6709\u5e2e\u52a9\u3002<\/p>\n<p>\u5b9e\u73b0SPFA&#xff1a;\u901a\u8fc7\u90bb\u63a5\u8868u\u8fde\u7740\u591a\u4e2av\u4ee5\u53caw&#xff08;\u6743\u8fb9&#xff09;&#xff1b;\u53ea\u8981v\u80fd\u501f\u52a9\u8fd9\u4e2au\u5230\u8fbe\u4e14\u66f4\u65b0dist&#xff08;\u6ce8\u610fu\u70b9\u8981\u662f\u6e90\u70b9\u53ef\u8fbe\u70b9&#xff09;\u5e76\u4e14v\u4e0d\u5728\u961f\u5217\u5c31\u5165\u8fdb\u53bb&#xff1b;\u65f6\u523b\u6807\u8bb0\u662f\u5426\u5728\u961f\u5217\u60c5\u51b5\u4ee5\u53ca\u67d0\u70b9\u8fdb\u8fc7\u961f\u5217\u7684\u6b21\u6570cnt\u3002&#xff08;\u9700\u8981\u65f6\u53ef\u7ef4\u62a4\u597dpre\u6570\u7ec4&#xff09; \u8fd9\u4e5f\u5c31\u662f\u666e\u901aSPFA\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p>\u5bf9\u5e94SLF\u548cLLL\u4f18\u5316&#xff1a;\u5176\u5b9e\u4e00\u4e2a\u662f\u63d2\u5165\u961f\u5217\u7684\u65f6\u5019&#xff1a;\u5c0f\u4e8e\u961f\u5934\u5143\u7d20\u5c31\u63d2\u961f\u5934\u5927\u4e8e\u5c31\u63d2\u961f\u5c3e\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u5c31\u662f\u9009\u62e9\u961f\u5217\u4e2d\u5143\u7d20\u7684\u65f6\u5019&#xff0c;\u6211\u4eec\u6bcf\u6b21\u90fd\u8981\u9009\u5bf9\u5934&#xff1b;\u56e0\u6b64\u5982\u679c\u53d1\u73b0\u961f\u5934\u5143\u7d20\u7684dist\u5c0f\u4e8e\u5e73\u5747\u503c\u5c31\u653e\u961f\u5c3e&#xff1b;\u5426\u5219\u9000\u51fa\u5faa\u73af\u9009\u62e9\u5b83&#xff08;\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u6bcf\u6b21\u64cd\u4f5c\u961f\u5217\u4e2d\u5c3d\u53ef\u80fddist\u5c0f\u7684\u70b9&#xff09;<\/p>\n<\/p>\n<p>\u56db\u79cd\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\u5c0f\u603b\u7ed3&#xff1a;<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214958-689bb706ead25.png\" \/><\/p>\n<\/p>\n<p>\u00a0\u8fd9\u7bc7\u6211\u4eec\u7684SPFA\u7b97\u6cd5\u5c31\u5206\u4eab\u5230\u6b64&#xff1b;\u4e0b\u4e00\u7bc7\u535a\u4e3b\u5c06\u5e26\u5927\u5bb6\u5256\u6790\u5176\u4ed6\u56fe\u8bba\u7b97\u6cd5&#xff1b;\u6b22\u8fce\u5927\u5bb6\u8ba2\u9605\u5440&#xff01;&#xff01;&#xff01;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.8k\u6b21\uff0c\u70b9\u8d5e122\u6b21\uff0c\u6536\u85cf78\u6b21\u3002\u5728\u7b97\u6cd5\u7684\u5947\u5999\u5b87\u5b99\u4e2d\uff0cSPFA\uff08Shortest Path Faster Algorithm\uff09\u662f\u4e00\u9897\u72ec\u7279\u7684\u661f\u8fb0\u3002\u5b83\u6253\u7834\u5e38\u89c4\uff0c\u5728\u56fe\u7684\u590d\u6742\u7f51\u7edc\u91cc\uff0c\u4ee5\u7b80\u6d01\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u5feb\u901f\u7a7f\u68ad\u4e8e\u5404\u4e2a\u8282\u70b9\uff0c\u7cbe\u51c6\u5b9a\u4f4d\u6700\u77ed\u8def\u5f84\u3002\u51ed\u501f\u677e\u5f1b\u64cd\u4f5c\u8fd9\u4e00\u795e\u5947 \u201c\u9b54\u6cd5\u201d\uff0c\u6bcf\u6b21\u8fed\u4ee3\u90fd\u5411\u7740\u6700\u4f18\u89e3\u5927\u6b65\u8fc8\u8fdb\uff0c\u8ba9\u590d\u6742\u7684\u8def\u5f84\u89c4\u5212\u96be\u9898\u8fce\u5203\u800c\u89e3 \u3002<\/p>\n","protected":false},"author":2,"featured_media":54412,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,5544,1984,5542,427,5543],"topic":[],"class_list":["post-54426","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-spfa","tag-1984","tag-5542","tag-427","tag-5543"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u72c2\u70ed\u7b97\u6cd5\u7bc7\u3011\u63a2\u5bfb\u56fe\u8bba\u5e7d\u5f84\u4e4bSPFA\u7b97\u6cd5\uff1a\u56fe\u8bba\u8ff7\u5bab\u91cc\u7684\u95ea\u7535\u5bfb\u5f84\u8005(\u901a\u4fd7\u6613\u61c2\u7248) - \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\/54426.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u72c2\u70ed\u7b97\u6cd5\u7bc7\u3011\u63a2\u5bfb\u56fe\u8bba\u5e7d\u5f84\u4e4bSPFA\u7b97\u6cd5\uff1a\u56fe\u8bba\u8ff7\u5bab\u91cc\u7684\u95ea\u7535\u5bfb\u5f84\u8005(\u901a\u4fd7\u6613\u61c2\u7248) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.8k\u6b21\uff0c\u70b9\u8d5e122\u6b21\uff0c\u6536\u85cf78\u6b21\u3002\u5728\u7b97\u6cd5\u7684\u5947\u5999\u5b87\u5b99\u4e2d\uff0cSPFA\uff08Shortest Path Faster Algorithm\uff09\u662f\u4e00\u9897\u72ec\u7279\u7684\u661f\u8fb0\u3002\u5b83\u6253\u7834\u5e38\u89c4\uff0c\u5728\u56fe\u7684\u590d\u6742\u7f51\u7edc\u91cc\uff0c\u4ee5\u7b80\u6d01\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u5feb\u901f\u7a7f\u68ad\u4e8e\u5404\u4e2a\u8282\u70b9\uff0c\u7cbe\u51c6\u5b9a\u4f4d\u6700\u77ed\u8def\u5f84\u3002\u51ed\u501f\u677e\u5f1b\u64cd\u4f5c\u8fd9\u4e00\u795e\u5947 \u201c\u9b54\u6cd5\u201d\uff0c\u6bcf\u6b21\u8fed\u4ee3\u90fd\u5411\u7740\u6700\u4f18\u89e3\u5927\u6b65\u8fc8\u8fdb\uff0c\u8ba9\u590d\u6742\u7684\u8def\u5f84\u89c4\u5212\u96be\u9898\u8fce\u5203\u800c\u89e3 \u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/54426.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-12T21:50:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214955-689bb703a9484.gif\" \/>\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=\"12 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/54426.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/54426.html\",\"name\":\"\u3010\u72c2\u70ed\u7b97\u6cd5\u7bc7\u3011\u63a2\u5bfb\u56fe\u8bba\u5e7d\u5f84\u4e4bSPFA\u7b97\u6cd5\uff1a\u56fe\u8bba\u8ff7\u5bab\u91cc\u7684\u95ea\u7535\u5bfb\u5f84\u8005(\u901a\u4fd7\u6613\u61c2\u7248) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-12T21:50:00+00:00\",\"dateModified\":\"2025-08-12T21:50:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/54426.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/54426.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/54426.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u72c2\u70ed\u7b97\u6cd5\u7bc7\u3011\u63a2\u5bfb\u56fe\u8bba\u5e7d\u5f84\u4e4bSPFA\u7b97\u6cd5\uff1a\u56fe\u8bba\u8ff7\u5bab\u91cc\u7684\u95ea\u7535\u5bfb\u5f84\u8005(\u901a\u4fd7\u6613\u61c2\u7248)\"}]},{\"@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\u72c2\u70ed\u7b97\u6cd5\u7bc7\u3011\u63a2\u5bfb\u56fe\u8bba\u5e7d\u5f84\u4e4bSPFA\u7b97\u6cd5\uff1a\u56fe\u8bba\u8ff7\u5bab\u91cc\u7684\u95ea\u7535\u5bfb\u5f84\u8005(\u901a\u4fd7\u6613\u61c2\u7248) - \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\/54426.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u72c2\u70ed\u7b97\u6cd5\u7bc7\u3011\u63a2\u5bfb\u56fe\u8bba\u5e7d\u5f84\u4e4bSPFA\u7b97\u6cd5\uff1a\u56fe\u8bba\u8ff7\u5bab\u91cc\u7684\u95ea\u7535\u5bfb\u5f84\u8005(\u901a\u4fd7\u6613\u61c2\u7248) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.8k\u6b21\uff0c\u70b9\u8d5e122\u6b21\uff0c\u6536\u85cf78\u6b21\u3002\u5728\u7b97\u6cd5\u7684\u5947\u5999\u5b87\u5b99\u4e2d\uff0cSPFA\uff08Shortest Path Faster Algorithm\uff09\u662f\u4e00\u9897\u72ec\u7279\u7684\u661f\u8fb0\u3002\u5b83\u6253\u7834\u5e38\u89c4\uff0c\u5728\u56fe\u7684\u590d\u6742\u7f51\u7edc\u91cc\uff0c\u4ee5\u7b80\u6d01\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u5feb\u901f\u7a7f\u68ad\u4e8e\u5404\u4e2a\u8282\u70b9\uff0c\u7cbe\u51c6\u5b9a\u4f4d\u6700\u77ed\u8def\u5f84\u3002\u51ed\u501f\u677e\u5f1b\u64cd\u4f5c\u8fd9\u4e00\u795e\u5947 \u201c\u9b54\u6cd5\u201d\uff0c\u6bcf\u6b21\u8fed\u4ee3\u90fd\u5411\u7740\u6700\u4f18\u89e3\u5927\u6b65\u8fc8\u8fdb\uff0c\u8ba9\u590d\u6742\u7684\u8def\u5f84\u89c4\u5212\u96be\u9898\u8fce\u5203\u800c\u89e3 \u3002","og_url":"https:\/\/www.wsisp.com\/helps\/54426.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-12T21:50:00+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250812214955-689bb703a9484.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"12 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/54426.html","url":"https:\/\/www.wsisp.com\/helps\/54426.html","name":"\u3010\u72c2\u70ed\u7b97\u6cd5\u7bc7\u3011\u63a2\u5bfb\u56fe\u8bba\u5e7d\u5f84\u4e4bSPFA\u7b97\u6cd5\uff1a\u56fe\u8bba\u8ff7\u5bab\u91cc\u7684\u95ea\u7535\u5bfb\u5f84\u8005(\u901a\u4fd7\u6613\u61c2\u7248) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-12T21:50:00+00:00","dateModified":"2025-08-12T21:50:00+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/54426.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/54426.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/54426.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u72c2\u70ed\u7b97\u6cd5\u7bc7\u3011\u63a2\u5bfb\u56fe\u8bba\u5e7d\u5f84\u4e4bSPFA\u7b97\u6cd5\uff1a\u56fe\u8bba\u8ff7\u5bab\u91cc\u7684\u95ea\u7535\u5bfb\u5f84\u8005(\u901a\u4fd7\u6613\u61c2\u7248)"}]},{"@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\/54426","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=54426"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/54426\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/54412"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=54426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=54426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=54426"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=54426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}