{"id":56555,"date":"2025-08-14T20:15:44","date_gmt":"2025-08-14T12:15:44","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/56555.html"},"modified":"2025-08-14T20:15:44","modified_gmt":"2025-08-14T12:15:44","slug":"%e3%80%8a%e7%ae%97%e6%b3%95%e5%af%bc%e8%ae%ba%e3%80%8b%e7%ac%ac-24-%e7%ab%a0-%e5%8d%95%e6%ba%90%e6%9c%80%e7%9f%ad%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/56555.html","title":{"rendered":"\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u7b2c 24 \u7ae0 - \u5355\u6e90\u6700\u77ed\u8def\u5f84"},"content":{"rendered":"<h2><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"778\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121534-689dd36677afd.png\" width=\"1054\" \/><\/h2>\n<h3><span style=\"color:#be191c\">\u5f15\u8a00<\/span><\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5355\u6e90\u6700\u77ed\u8def\u5f84\u95ee\u9898\u662f\u56fe\u8bba\u4e2d\u7684\u6838\u5fc3\u95ee\u9898\u4e4b\u4e00&#xff0c;\u65e8\u5728\u6c42\u89e3<span style=\"background-color:#d4e9d5\">\u4ece\u56fe\u4e2d\u4e00\u4e2a\u7279\u5b9a\u6e90\u8282\u70b9\u5230\u5176\u4ed6\u6240\u6709\u8282\u70b9\u7684\u6700\u77ed\u8def\u5f84<\/span>\u3002\u8fd9\u4e00\u95ee\u9898\u5728\u5bfc\u822a\u7cfb\u7edf\u3001\u7f51\u7edc\u8def\u7531\u3001\u4ea4\u901a\u89c4\u5212\u7b49\u9886\u57df\u6709\u7740\u5e7f\u6cdb\u5e94\u7528\u3002\u672c\u7ae0\u5c06\u8be6\u7ec6\u89e3\u6790\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u7b2c 24 \u7ae0\u4ecb\u7ecd\u7684\u56db\u79cd\u7ecf\u5178\u7b97\u6cd5&#xff0c;\u5e76\u63d0\u4f9b\u5b8c\u6574\u53ef\u8fd0\u884c\u7684 C&#043;&#043; \u5b9e\u73b0\u4ee3\u7801\u3002<\/p>\n<h4><\/h4>\n<h3><span style=\"color:#be191c\">24.1 Bellman-Ford \u7b97\u6cd5<\/span><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1248\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121535-689dd3671ab79.png\" width=\"1856\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Bellman-Ford \u7b97\u6cd5\u662f\u6c42\u89e3\u5355\u6e90\u6700\u77ed\u8def\u5f84\u95ee\u9898\u7684\u7ecf\u5178\u7b97\u6cd5&#xff0c;\u5b83\u80fd\u591f\u5904\u7406<span style=\"background-color:#c7e6ea\">\u5305\u542b\u8d1f\u6743\u8fb9\u7684\u56fe&#xff0c;\u5e76\u4e14\u53ef\u4ee5\u68c0\u6d4b\u51fa\u56fe\u4e2d\u662f\u5426\u5b58\u5728\u4ece\u6e90\u8282\u70b9\u53ef\u8fbe\u7684\u8d1f\u6743\u56de\u8def<\/span>\u3002<\/p>\n<h4><span style=\"color:#1a439c\">\u7b97\u6cd5\u601d\u60f3<\/span><\/h4>\n<li>\u521d\u59cb\u5316&#xff1a;\u5c06\u6e90\u8282\u70b9\u5230\u6240\u6709\u5176\u4ed6\u8282\u70b9\u7684\u8ddd\u79bb\u521d\u59cb\u5316\u4e3a\u65e0\u7a77\u5927&#xff0c;\u6e90\u8282\u70b9\u5230\u81ea\u8eab\u7684\u8ddd\u79bb\u521d\u59cb\u5316\u4e3a 0\u3002<\/li>\n<li>\u677e\u5f1b\u64cd\u4f5c&#xff1a;\u5bf9\u56fe\u4e2d\u6240\u6709\u8fb9\u8fdb\u884c V-1 \u6b21\u677e\u5f1b\u64cd\u4f5c&#xff08;V \u4e3a\u8282\u70b9\u6570&#xff09;\u3002\u6bcf\u6b21\u677e\u5f1b\u64cd\u4f5c\u904d\u5386\u6240\u6709\u8fb9 (u, v)&#xff0c;\u82e5distance[v] &gt; distance[u] &#043; weight(u, v)&#xff0c;\u5219\u66f4\u65b0distance[v] &#061; distance[u] &#043; weight(u, v)\u3002<\/li>\n<li>\u68c0\u6d4b\u8d1f\u6743\u56de\u8def&#xff1a;\u5728\u5b8c\u6210 V-1 \u6b21\u677e\u5f1b\u540e&#xff0c;\u518d\u5bf9\u6240\u6709\u8fb9\u8fdb\u884c\u4e00\u6b21\u68c0\u67e5\u3002\u5982\u679c\u4ecd\u80fd\u8fdb\u884c\u677e\u5f1b\u64cd\u4f5c&#xff0c;\u5219\u8bf4\u660e\u56fe\u4e2d\u5b58\u5728\u4ece\u6e90\u8282\u70b9\u53ef\u8fbe\u7684\u8d1f\u6743\u56de\u8def\u3002<\/li>\n<h4><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"745\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121536-689dd36838e01.png\" width=\"1507\" \/><\/h4>\n<h4><span style=\"color:#1a439c\">\u4ee3\u7801\u5b9e\u73b0<\/span><\/h4>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;climits&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u8fb9\u7684\u7ed3\u6784\u4f53<br \/>\nstruct Edge {<br \/>\n    int u;      \/\/ \u8d77\u70b9<br \/>\n    int v;      \/\/ \u7ec8\u70b9<br \/>\n    int weight; \/\/ \u6743\u91cd<br \/>\n    Edge(int u_, int v_, int weight_) : u(u_), v(v_), weight(weight_) {}<br \/>\n};<\/p>\n<p>\/**<br \/>\n * Bellman-Ford\u7b97\u6cd5\u5b9e\u73b0<br \/>\n * &#064;param edges \u8fb9\u7684\u96c6\u5408<br \/>\n * &#064;param V \u8282\u70b9\u6570\u91cf<br \/>\n * &#064;param s \u6e90\u8282\u70b9<br \/>\n * &#064;param distance \u5b58\u50a8\u6700\u77ed\u8ddd\u79bb\u7684\u6570\u7ec4<br \/>\n * &#064;return \u662f\u5426\u5b58\u5728\u8d1f\u6743\u56de\u8def<br \/>\n *\/<br \/>\nbool bellmanFord(const vector&lt;Edge&gt;&amp; edges, int V, int s, vector&lt;int&gt;&amp; distance) {<br \/>\n    \/\/ 1. \u521d\u59cb\u5316\u8ddd\u79bb<br \/>\n    distance.assign(V, INT_MAX);<br \/>\n    distance[s] &#061; 0;<\/p>\n<p>    \/\/ 2. \u8fdb\u884cV-1\u6b21\u677e\u5f1b\u64cd\u4f5c<br \/>\n    for (int i &#061; 1; i &lt;&#061; V &#8211; 1; &#043;&#043;i) {<br \/>\n        bool updated &#061; false;<br \/>\n        for (const Edge&amp; e : edges) {<br \/>\n            if (distance[e.u] !&#061; INT_MAX &amp;&amp; distance[e.v] &gt; distance[e.u] &#043; e.weight) {<br \/>\n                distance[e.v] &#061; distance[e.u] &#043; e.weight;<br \/>\n                updated &#061; true;<br \/>\n            }<br \/>\n        }<br \/>\n        \/\/ \u5982\u679c\u6ca1\u6709\u66f4\u65b0&#xff0c;\u53ef\u4ee5\u63d0\u524d\u9000\u51fa<br \/>\n        if (!updated) break;<br \/>\n    }<\/p>\n<p>    \/\/ 3. \u68c0\u6d4b\u8d1f\u6743\u56de\u8def<br \/>\n    for (const Edge&amp; e : edges) {<br \/>\n        if (distance[e.u] !&#061; INT_MAX &amp;&amp; distance[e.v] &gt; distance[e.u] &#043; e.weight) {<br \/>\n            return true; \/\/ \u5b58\u5728\u8d1f\u6743\u56de\u8def<br \/>\n        }<br \/>\n    }<br \/>\n    return false; \/\/ \u4e0d\u5b58\u5728\u8d1f\u6743\u56de\u8def<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u793a\u4f8b\u56fe: \u5305\u542b5\u4e2a\u8282\u70b9(0-4)<br \/>\n    int V &#061; 5;<br \/>\n    vector&lt;Edge&gt; edges;<\/p>\n<p>    \/\/ \u6dfb\u52a0\u8fb9<br \/>\n    edges.emplace_back(0, 1, -1);<br \/>\n    edges.emplace_back(0, 2, 4);<br \/>\n    edges.emplace_back(1, 2, 3);<br \/>\n    edges.emplace_back(1, 3, 2);<br \/>\n    edges.emplace_back(1, 4, 2);<br \/>\n    edges.emplace_back(3, 1, 1);<br \/>\n    edges.emplace_back(3, 2, 5);<br \/>\n    edges.emplace_back(4, 3, -3);<\/p>\n<p>    int source &#061; 0;<br \/>\n    vector&lt;int&gt; distance;<\/p>\n<p>    bool hasNegativeCycle &#061; bellmanFord(edges, V, source, distance);<\/p>\n<p>    if (hasNegativeCycle) {<br \/>\n        cout &lt;&lt; &#034;\u56fe\u4e2d\u5b58\u5728\u8d1f\u6743\u56de\u8def&#034; &lt;&lt; endl;<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;\u4ece\u6e90\u8282\u70b9 &#034; &lt;&lt; source &lt;&lt; &#034; \u5230\u5404\u8282\u70b9\u7684\u6700\u77ed\u8ddd\u79bb:&#034; &lt;&lt; endl;<br \/>\n        for (int i &#061; 0; i &lt; V; &#043;&#043;i) {<br \/>\n            if (distance[i] &#061;&#061; INT_MAX) {<br \/>\n                cout &lt;&lt; &#034;\u8282\u70b9 &#034; &lt;&lt; i &lt;&lt; &#034;: \u4e0d\u53ef\u8fbe&#034; &lt;&lt; endl;<br \/>\n            } else {<br \/>\n                cout &lt;&lt; &#034;\u8282\u70b9 &#034; &lt;&lt; i &lt;&lt; &#034;: &#034; &lt;&lt; distance[i] &lt;&lt; endl;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1072\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121536-689dd3688e45c.png\" width=\"1813\" \/><\/h4>\n<h4><\/h4>\n<h4><span style=\"color:#1a439c\">\u7efc\u5408\u6848\u4f8b\u53ca\u5e94\u7528<\/span><\/h4>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e0b\u9762\u662f\u4e00\u4e2a\u7269\u6d41\u8def\u5f84\u89c4\u5212\u7684\u6848\u4f8b&#xff0c;\u5c55\u793a\u5982\u4f55\u4f7f\u7528 Bellman-Ford \u7b97\u6cd5\u5904\u7406\u53ef\u80fd\u5305\u542b\u8d1f\u6743\u8fb9&#xff08;\u5982\u6298\u6263\u8def\u7ebf&#xff09;\u7684\u573a\u666f&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;climits&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>struct Edge {<br \/>\n    int from;<br \/>\n    int to;<br \/>\n    int cost; \/\/ \u53ef\u4ee5\u662f\u8d1f\u6570&#xff0c;\u8868\u793a\u6298\u6263<br \/>\n    Edge(int f, int t, int c) : from(f), to(t), cost(c) {}<br \/>\n};<\/p>\n<p>bool bellmanFord(const vector&lt;Edge&gt;&amp; edges, int V, int s, vector&lt;int&gt;&amp; distance, vector&lt;int&gt;&amp; parent) {<br \/>\n    distance.assign(V, INT_MAX);<br \/>\n    parent.assign(V, -1);<br \/>\n    distance[s] &#061; 0;<\/p>\n<p>    for (int i &#061; 1; i &lt;&#061; V &#8211; 1; &#043;&#043;i) {<br \/>\n        for (const Edge&amp; e : edges) {<br \/>\n            if (distance[e.from] !&#061; INT_MAX &amp;&amp; distance[e.to] &gt; distance[e.from] &#043; e.cost) {<br \/>\n                distance[e.to] &#061; distance[e.from] &#043; e.cost;<br \/>\n                parent[e.to] &#061; e.from;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    for (const Edge&amp; e : edges) {<br \/>\n        if (distance[e.from] !&#061; INT_MAX &amp;&amp; distance[e.to] &gt; distance[e.from] &#043; e.cost) {<br \/>\n            return true;<br \/>\n        }<br \/>\n    }<br \/>\n    return false;<br \/>\n}<\/p>\n<p>\/\/ \u6253\u5370\u4ece\u6e90\u8282\u70b9\u5230\u76ee\u6807\u8282\u70b9\u7684\u8def\u5f84<br \/>\nvoid printPath(int target, const vector&lt;int&gt;&amp; parent) {<br \/>\n    if (parent[target] &#061;&#061; -1) {<br \/>\n        cout &lt;&lt; target;<br \/>\n        return;<br \/>\n    }<br \/>\n    printPath(parent[target], parent);<br \/>\n    cout &lt;&lt; &#034; -&gt; &#034; &lt;&lt; target;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u7269\u6d41\u7f51\u7edc: 5\u4e2a\u4ed3\u5e93\u8282\u70b9(0-4)<br \/>\n    int V &#061; 5;<br \/>\n    vector&lt;Edge&gt; edges;<\/p>\n<p>    \/\/ \u6dfb\u52a0\u8fd0\u8f93\u8def\u7ebf\u53ca\u6210\u672c(\u8d1f\u6570\u8868\u793a\u6298\u6263)<br \/>\n    edges.emplace_back(0, 1, 10);   \/\/ \u4ed3\u5e930\u52301&#xff0c;\u6210\u672c10<br \/>\n    edges.emplace_back(0, 2, 30);   \/\/ \u4ed3\u5e930\u52302&#xff0c;\u6210\u672c30<br \/>\n    edges.emplace_back(0, 3, 100);  \/\/ \u4ed3\u5e930\u52303&#xff0c;\u6210\u672c100<br \/>\n    edges.emplace_back(1, 2, 5);    \/\/ \u4ed3\u5e931\u52302&#xff0c;\u6210\u672c5<br \/>\n    edges.emplace_back(2, 3, 20);   \/\/ \u4ed3\u5e932\u52303&#xff0c;\u6210\u672c20<br \/>\n    edges.emplace_back(3, 4, 10);   \/\/ \u4ed3\u5e933\u52304&#xff0c;\u6210\u672c10<br \/>\n    edges.emplace_back(2, 4, 60);   \/\/ \u4ed3\u5e932\u52304&#xff0c;\u6210\u672c60<br \/>\n    edges.emplace_back(1, 4, -50);  \/\/ \u4ed3\u5e931\u52304&#xff0c;\u6298\u6263\u8def\u7ebf&#xff0c;\u6210\u672c-50(\u5b9e\u9645\u662f\u6536\u76ca)<\/p>\n<p>    int source &#061; 0;<br \/>\n    vector&lt;int&gt; distance, parent;<\/p>\n<p>    bool hasNegativeCycle &#061; bellmanFord(edges, V, source, distance, parent);<\/p>\n<p>    if (hasNegativeCycle) {<br \/>\n        cout &lt;&lt; &#034;\u7269\u6d41\u7f51\u7edc\u4e2d\u5b58\u5728\u5f02\u5e38\u6298\u6263\u5faa\u73af&#xff0c;\u53ef\u80fd\u5bfc\u81f4\u6210\u672c\u65e0\u9650\u964d\u4f4e&#xff01;&#034; &lt;&lt; endl;<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;\u4ece\u4ed3\u5e93 &#034; &lt;&lt; source &lt;&lt; &#034; \u5230\u5404\u4ed3\u5e93\u7684\u6700\u4f4e\u6210\u672c:&#034; &lt;&lt; endl;<br \/>\n        for (int i &#061; 0; i &lt; V; &#043;&#043;i) {<br \/>\n            if (distance[i] &#061;&#061; INT_MAX) {<br \/>\n                cout &lt;&lt; &#034;\u4ed3\u5e93 &#034; &lt;&lt; i &lt;&lt; &#034;: \u4e0d\u53ef\u8fbe&#034; &lt;&lt; endl;<br \/>\n            } else {<br \/>\n                cout &lt;&lt; &#034;\u4ed3\u5e93 &#034; &lt;&lt; i &lt;&lt; &#034;: \u6210\u672c &#034; &lt;&lt; distance[i] &lt;&lt; &#034;, \u8def\u5f84: &#034;;<br \/>\n                printPath(i, parent);<br \/>\n                cout &lt;&lt; endl;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1045\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121537-689dd3694d20b.png\" width=\"1765\" \/><\/h3>\n<h4><\/h4>\n<h3><span style=\"color:#be191c\">24.2 \u6709\u5411\u65e0\u73af\u56fe\u4e2d\u7684\u5355\u6e90\u6700\u77ed\u8def\u5f84\u95ee\u9898<\/span><\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6709\u5411\u65e0\u73af\u56fe&#xff08;DAG&#xff09;\u7684\u5355\u6e90\u6700\u77ed\u8def\u5f84\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7<span style=\"background-color:#d4e9d5\">\u62d3\u6251\u6392\u5e8f\u7ed3\u5408\u677e\u5f1b\u64cd\u4f5c\u9ad8\u6548\u89e3\u51b3<\/span>&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O (V&#043;E)&#xff0c;\u4f18\u4e8e Bellman-Ford \u7b97\u6cd5\u3002<\/p>\n<h4><span style=\"color:#1a439c\">\u7b97\u6cd5\u601d\u60f3<\/span><\/h4>\n<li>\u5bf9 DAG \u8fdb\u884c\u62d3\u6251\u6392\u5e8f&#xff0c;\u5f97\u5230\u4e00\u4e2a\u7ebf\u6027\u6392\u5217\u7684\u8282\u70b9\u5e8f\u5217\u3002<\/li>\n<li>\u521d\u59cb\u5316\u8ddd\u79bb\u6570\u7ec4&#xff1a;\u6e90\u8282\u70b9\u8ddd\u79bb\u4e3a 0&#xff0c;\u5176\u4ed6\u8282\u70b9\u4e3a\u65e0\u7a77\u5927\u3002<\/li>\n<li>\u6309\u7167\u62d3\u6251\u6392\u5e8f\u7684\u987a\u5e8f&#xff0c;\u5bf9\u6bcf\u4e2a\u8282\u70b9\u7684\u6240\u6709\u51fa\u8fb9\u8fdb\u884c\u677e\u5f1b\u64cd\u4f5c\u3002<\/li>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7531\u4e8e DAG \u4e2d\u6ca1\u6709\u73af&#xff0c;\u4e14\u6211\u4eec\u6309\u7167\u62d3\u6251\u987a\u5e8f\u5904\u7406\u8282\u70b9&#xff0c;\u6bcf\u4e2a\u8282\u70b9\u53ea\u4f1a\u88ab\u5904\u7406\u4e00\u6b21&#xff0c;\u56e0\u6b64\u7b97\u6cd5\u6548\u7387\u5f88\u9ad8\u3002\u8be5\u7b97\u6cd5\u53ef\u4ee5\u5904\u7406\u8d1f\u6743\u8fb9&#xff0c;\u4f46\u4e0d\u80fd\u5904\u7406\u6709\u73af\u7684\u56fe\u3002<\/p>\n<h4><\/h4>\n<h4><span style=\"color:#1a439c\">\u4ee3\u7801\u5b9e\u73b0<\/span><\/h4>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;stack&gt;<br \/>\n#include &lt;climits&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/**<br \/>\n * \u5bf9DAG\u8fdb\u884c\u62d3\u6251\u6392\u5e8f<br \/>\n * &#064;param adj \u90bb\u63a5\u8868\u8868\u793a\u7684\u56fe<br \/>\n * &#064;param V \u8282\u70b9\u6570\u91cf<br \/>\n * &#064;return \u62d3\u6251\u6392\u5e8f\u7ed3\u679c<br \/>\n *\/<br \/>\nvector&lt;int&gt; topologicalSort(const vector&lt;vector&lt;pair&lt;int, int&gt;&gt;&gt;&amp; adj, int V) {<br \/>\n    vector&lt;int&gt; inDegree(V, 0);<br \/>\n    \/\/ \u8ba1\u7b97\u5165\u5ea6<br \/>\n    for (int u &#061; 0; u &lt; V; &#043;&#043;u) {<br \/>\n        for (const auto&amp; edge : adj[u]) {<br \/>\n            int v &#061; edge.first;<br \/>\n            inDegree[v]&#043;&#043;;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u4f7f\u7528\u6808\u5b58\u50a8\u5165\u5ea6\u4e3a0\u7684\u8282\u70b9<br \/>\n    stack&lt;int&gt; s;<br \/>\n    for (int u &#061; 0; u &lt; V; &#043;&#043;u) {<br \/>\n        if (inDegree[u] &#061;&#061; 0) {<br \/>\n            s.push(u);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    vector&lt;int&gt; topoOrder;<br \/>\n    while (!s.empty()) {<br \/>\n        int u &#061; s.top();<br \/>\n        s.pop();<br \/>\n        topoOrder.push_back(u);<\/p>\n<p>        \/\/ \u51cf\u5c11\u76f8\u90bb\u8282\u70b9\u7684\u5165\u5ea6<br \/>\n        for (const auto&amp; edge : adj[u]) {<br \/>\n            int v &#061; edge.first;<br \/>\n            inDegree[v]&#8211;;<br \/>\n            if (inDegree[v] &#061;&#061; 0) {<br \/>\n                s.push(v);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return topoOrder;<br \/>\n}<\/p>\n<p>\/**<br \/>\n * DAG\u4e2d\u7684\u5355\u6e90\u6700\u77ed\u8def\u5f84\u7b97\u6cd5<br \/>\n * &#064;param adj \u90bb\u63a5\u8868\u8868\u793a\u7684\u56fe<br \/>\n * &#064;param V \u8282\u70b9\u6570\u91cf<br \/>\n * &#064;param s \u6e90\u8282\u70b9<br \/>\n * &#064;param distance \u5b58\u50a8\u6700\u77ed\u8ddd\u79bb\u7684\u6570\u7ec4<br \/>\n * &#064;param parent \u5b58\u50a8\u524d\u9a71\u8282\u70b9\u7684\u6570\u7ec4<br \/>\n *\/<br \/>\nvoid dagShortestPath(const vector&lt;vector&lt;pair&lt;int, int&gt;&gt;&gt;&amp; adj, int V, int s,<br \/>\n                    vector&lt;int&gt;&amp; distance, vector&lt;int&gt;&amp; parent) {<br \/>\n    \/\/ 1. \u62d3\u6251\u6392\u5e8f<br \/>\n    vector&lt;int&gt; topoOrder &#061; topologicalSort(adj, V);<\/p>\n<p>    \/\/ 2. \u521d\u59cb\u5316<br \/>\n    distance.assign(V, INT_MAX);<br \/>\n    parent.assign(V, -1);<br \/>\n    distance[s] &#061; 0;<\/p>\n<p>    \/\/ 3. \u6309\u62d3\u6251\u987a\u5e8f\u677e\u5f1b\u8fb9<br \/>\n    for (int u : topoOrder) {<br \/>\n        if (distance[u] !&#061; INT_MAX) {  \/\/ \u4ec5\u5904\u7406\u53ef\u8fbe\u8282\u70b9<br \/>\n            for (const auto&amp; edge : adj[u]) {<br \/>\n                int v &#061; edge.first;<br \/>\n                int weight &#061; edge.second;<br \/>\n                \/\/ \u677e\u5f1b\u64cd\u4f5c<br \/>\n                if (distance[v] &gt; distance[u] &#043; weight) {<br \/>\n                    distance[v] &#061; distance[u] &#043; weight;<br \/>\n                    parent[v] &#061; u;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6253\u5370\u8def\u5f84<br \/>\nvoid printPath(int v, const vector&lt;int&gt;&amp; parent) {<br \/>\n    if (parent[v] &#061;&#061; -1) {<br \/>\n        cout &lt;&lt; v;<br \/>\n        return;<br \/>\n    }<br \/>\n    printPath(parent[v], parent);<br \/>\n    cout &lt;&lt; &#034; -&gt; &#034; &lt;&lt; v;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u793a\u4f8bDAG: 5\u4e2a\u8282\u70b9(0-4)<br \/>\n    int V &#061; 5;<br \/>\n    vector&lt;vector&lt;pair&lt;int, int&gt;&gt;&gt; adj(V);<\/p>\n<p>    \/\/ \u6dfb\u52a0\u8fb9 (u, v, weight)<br \/>\n    adj[0].emplace_back(1, 5);<br \/>\n    adj[0].emplace_back(2, 3);<br \/>\n    adj[1].emplace_back(2, 2);<br \/>\n    adj[1].emplace_back(3, 6);<br \/>\n    adj[2].emplace_back(3, 7);<br \/>\n    adj[2].emplace_back(4, 4);<br \/>\n    adj[2].emplace_back(1, 1);<br \/>\n    adj[3].emplace_back(4, -1);<br \/>\n    adj[4].emplace_back(3, -2);<\/p>\n<p>    int source &#061; 1;<br \/>\n    vector&lt;int&gt; distance, parent;<\/p>\n<p>    dagShortestPath(adj, V, source, distance, parent);<\/p>\n<p>    cout &lt;&lt; &#034;\u4ece\u6e90\u8282\u70b9 &#034; &lt;&lt; source &lt;&lt; &#034; \u5230\u5404\u8282\u70b9\u7684\u6700\u77ed\u8ddd\u79bb:&#034; &lt;&lt; endl;<br \/>\n    for (int i &#061; 0; i &lt; V; &#043;&#043;i) {<br \/>\n        if (distance[i] &#061;&#061; INT_MAX) {<br \/>\n            cout &lt;&lt; &#034;\u8282\u70b9 &#034; &lt;&lt; i &lt;&lt; &#034;: \u4e0d\u53ef\u8fbe&#034; &lt;&lt; endl;<br \/>\n        } else {<br \/>\n            cout &lt;&lt; &#034;\u8282\u70b9 &#034; &lt;&lt; i &lt;&lt; &#034;: &#034; &lt;&lt; distance[i] &lt;&lt; &#034;, \u8def\u5f84: &#034;;<br \/>\n            printPath(i, parent);<br \/>\n            cout &lt;&lt; endl;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1072\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121538-689dd36a0cbf0.png\" width=\"1782\" \/><\/h4>\n<h4><\/h4>\n<h4><span style=\"color:#1a439c\">\u7efc\u5408\u6848\u4f8b\u53ca\u5e94\u7528<\/span><\/h4>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u9879\u76ee\u8c03\u5ea6\u7684\u6848\u4f8b&#xff0c;\u5c55\u793a\u5982\u4f55\u4f7f\u7528 DAG \u6700\u77ed\u8def\u5f84\u7b97\u6cd5\u89e3\u51b3\u5173\u952e\u8def\u5f84\u95ee\u9898&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;stack&gt;<br \/>\n#include &lt;climits&gt;<br \/>\n#include &lt;algorithm&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u5bf9DAG\u8fdb\u884c\u62d3\u6251\u6392\u5e8f<br \/>\nvector&lt;int&gt; topologicalSort(const vector&lt;vector&lt;pair&lt;int, int&gt;&gt;&gt;&amp; adj, int V) {<br \/>\n    vector&lt;int&gt; inDegree(V, 0);<br \/>\n    for (int u &#061; 0; u &lt; V; &#043;&#043;u) {<br \/>\n        for (const auto&amp; edge : adj[u]) {<br \/>\n            int v &#061; edge.first;<br \/>\n            inDegree[v]&#043;&#043;;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    stack&lt;int&gt; s;<br \/>\n    for (int u &#061; 0; u &lt; V; &#043;&#043;u) {<br \/>\n        if (inDegree[u] &#061;&#061; 0) {<br \/>\n            s.push(u);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    vector&lt;int&gt; topoOrder;<br \/>\n    while (!s.empty()) {<br \/>\n        int u &#061; s.top();<br \/>\n        s.pop();<br \/>\n        topoOrder.push_back(u);<\/p>\n<p>        for (const auto&amp; edge : adj[u]) {<br \/>\n            int v &#061; edge.first;<br \/>\n            inDegree[v]&#8211;;<br \/>\n            if (inDegree[v] &#061;&#061; 0) {<br \/>\n                s.push(v);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return topoOrder;<br \/>\n}<\/p>\n<p>\/\/ \u8ba1\u7b97\u6700\u957f\u8def\u5f84&#xff08;\u5173\u952e\u8def\u5f84&#xff09;<br \/>\nvoid criticalPathAnalysis(const vector&lt;vector&lt;pair&lt;int, int&gt;&gt;&gt;&amp; adj, int V,<br \/>\n                         vector&lt;int&gt;&amp; earliest, vector&lt;int&gt;&amp; latest, vector&lt;int&gt;&amp; parent) {<br \/>\n    vector&lt;int&gt; topoOrder &#061; topologicalSort(adj, V);<\/p>\n<p>    \/\/ \u8ba1\u7b97\u6700\u65e9\u5f00\u59cb\u65f6\u95f4&#xff08;\u6700\u957f\u8def\u5f84&#xff09;<br \/>\n    earliest.assign(V, 0);<br \/>\n    parent.assign(V, -1);<\/p>\n<p>    for (int u : topoOrder) {<br \/>\n        for (const auto&amp; edge : adj[u]) {<br \/>\n            int v &#061; edge.first;<br \/>\n            int weight &#061; edge.second;<br \/>\n            if (earliest[v] &lt; earliest[u] &#043; weight) {<br \/>\n                earliest[v] &#061; earliest[u] &#043; weight;<br \/>\n                parent[v] &#061; u;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u8ba1\u7b97\u6700\u665a\u5f00\u59cb\u65f6\u95f4<br \/>\n    latest.assign(V, earliest.back());  \/\/ \u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u6700\u665a\u65f6\u95f4\u7b49\u4e8e\u6700\u65e9\u65f6\u95f4<\/p>\n<p>    \/\/ \u53cd\u8f6c\u62d3\u6251\u6392\u5e8f<br \/>\n    reverse(topoOrder.begin(), topoOrder.end());<\/p>\n<p>    for (int u : topoOrder) {<br \/>\n        for (const auto&amp; edge : adj[u]) {<br \/>\n            int v &#061; edge.first;<br \/>\n            int weight &#061; edge.second;<br \/>\n            if (latest[u] &gt; latest[v] &#8211; weight) {<br \/>\n                latest[u] &#061; latest[v] &#8211; weight;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6253\u5370\u5173\u952e\u8def\u5f84<br \/>\nvoid printCriticalPath(int v, const vector&lt;int&gt;&amp; parent) {<br \/>\n    if (parent[v] &#061;&#061; -1) {<br \/>\n        cout &lt;&lt; v;<br \/>\n        return;<br \/>\n    }<br \/>\n    printCriticalPath(parent[v], parent);<br \/>\n    cout &lt;&lt; &#034; -&gt; &#034; &lt;&lt; v;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u9879\u76ee\u4efb\u52a1\u8282\u70b9: 0(\u5f00\u59cb), 1-6(\u4efb\u52a1), 7(\u7ed3\u675f)<br \/>\n    int V &#061; 8;<br \/>\n    vector&lt;vector&lt;pair&lt;int, int&gt;&gt;&gt; adj(V);<\/p>\n<p>    \/\/ \u6dfb\u52a0\u4efb\u52a1\u4f9d\u8d56\u548c\u6301\u7eed\u65f6\u95f4 (u, v, \u6301\u7eed\u65f6\u95f4)<br \/>\n    adj[0].emplace_back(1, 3);   \/\/ \u5f00\u59cb -&gt; \u4efb\u52a11, 3\u5929<br \/>\n    adj[0].emplace_back(2, 2);   \/\/ \u5f00\u59cb -&gt; \u4efb\u52a12, 2\u5929<br \/>\n    adj[1].emplace_back(3, 4);   \/\/ \u4efb\u52a11 -&gt; \u4efb\u52a13, 4\u5929<br \/>\n    adj[1].emplace_back(4, 3);   \/\/ \u4efb\u52a11 -&gt; \u4efb\u52a14, 3\u5929<br \/>\n    adj[2].emplace_back(4, 2);   \/\/ \u4efb\u52a12 -&gt; \u4efb\u52a14, 2\u5929<br \/>\n    adj[3].emplace_back(5, 1);   \/\/ \u4efb\u52a13 -&gt; \u4efb\u52a15, 1\u5929<br \/>\n    adj[4].emplace_back(5, 2);   \/\/ \u4efb\u52a14 -&gt; \u4efb\u52a15, 2\u5929<br \/>\n    adj[4].emplace_back(6, 3);   \/\/ \u4efb\u52a14 -&gt; \u4efb\u52a16, 3\u5929<br \/>\n    adj[5].emplace_back(7, 3);   \/\/ \u4efb\u52a15 -&gt; \u7ed3\u675f, 3\u5929<br \/>\n    adj[6].emplace_back(7, 2);   \/\/ \u4efb\u52a16 -&gt; \u7ed3\u675f, 2\u5929<\/p>\n<p>    vector&lt;int&gt; earliest, latest, parent;<br \/>\n    criticalPathAnalysis(adj, V, earliest, latest, parent);<\/p>\n<p>    cout &lt;&lt; &#034;\u9879\u76ee\u8ba1\u5212\u5206\u6790\u7ed3\u679c:&#034; &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;\u9879\u76ee\u6700\u77ed\u5b8c\u6210\u65f6\u95f4: &#034; &lt;&lt; earliest.back() &lt;&lt; &#034;\u5929&#034; &lt;&lt; endl;<br \/>\n    cout &lt;&lt; endl;<\/p>\n<p>    cout &lt;&lt; &#034;\u5404\u4efb\u52a1\u65f6\u95f4\u5206\u6790:&#034; &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;\u4efb\u52a1ID\\\\t\u6700\u65e9\u5f00\u59cb\\\\t\u6700\u665a\u5f00\u59cb\\\\t\u6d6e\u52a8\u65f6\u95f4&#034; &lt;&lt; endl;<br \/>\n    for (int i &#061; 1; i &lt; V &#8211; 1; &#043;&#043;i) {  \/\/ \u8df3\u8fc7\u5f00\u59cb(0)\u548c\u7ed3\u675f(V-1)\u8282\u70b9<br \/>\n        int slack &#061; latest[i] &#8211; earliest[i];<br \/>\n        cout &lt;&lt; i &lt;&lt; &#034;\\\\t&#034; &lt;&lt; earliest[i] &lt;&lt; &#034;\\\\t\\\\t&#034; &lt;&lt; latest[i] &lt;&lt; &#034;\\\\t\\\\t&#034; &lt;&lt; slack &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    cout &lt;&lt; endl &lt;&lt; &#034;\u5173\u952e\u8def\u5f84(\u65e0\u6d6e\u52a8\u65f6\u95f4\u7684\u4efb\u52a1\u5e8f\u5217): &#034;;<br \/>\n    printCriticalPath(V &#8211; 1, parent);<br \/>\n    cout &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1065\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121538-689dd36ac476d.png\" width=\"1773\" \/><\/h3>\n<h4><\/h4>\n<h3><span style=\"color:#be191c\">24.3 Dijkstra \u7b97\u6cd5<\/span><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"720\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121539-689dd36b85009.png\" width=\"1280\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Dijkstra \u7b97\u6cd5\u662f\u4e00\u79cd\u8d2a\u5fc3\u7b97\u6cd5&#xff0c;\u7528\u4e8e<span style=\"background-color:#d4e9d5\">\u6c42\u89e3\u975e\u8d1f\u6743\u56fe\u7684\u5355\u6e90\u6700\u77ed\u8def\u5f84\u95ee\u9898<\/span>\u3002\u5b83\u7684\u6548\u7387\u9ad8\u4e8e Bellman-Ford \u7b97\u6cd5&#xff0c;\u5c24\u5176\u5728\u4f7f\u7528<span style=\"background-color:#ffd7b9\">\u4f18\u5148\u961f\u5217<\/span>\u4f18\u5316\u540e&#xff0c;\u6027\u80fd\u8868\u73b0\u4f18\u5f02\u3002<\/p>\n<h4><span style=\"color:#1a439c\">\u7b97\u6cd5\u601d\u60f3<\/span><\/h4>\n<li>\u521d\u59cb\u5316&#xff1a;\u6e90\u8282\u70b9\u8ddd\u79bb\u4e3a 0&#xff0c;\u5176\u4ed6\u8282\u70b9\u8ddd\u79bb\u4e3a\u65e0\u7a77\u5927\u3002\u7ef4\u62a4\u4e00\u4e2a\u4f18\u5148\u961f\u5217&#xff08;\u6700\u5c0f\u5806&#xff09;&#xff0c;\u7528\u4e8e\u9009\u62e9\u5f53\u524d\u8ddd\u79bb\u6700\u5c0f\u7684\u8282\u70b9\u3002<\/li>\n<li>\u9009\u62e9\u8282\u70b9&#xff1a;\u6bcf\u6b21\u4ece\u4f18\u5148\u961f\u5217\u4e2d\u53d6\u51fa\u8ddd\u79bb\u6700\u5c0f\u7684\u8282\u70b9 u\u3002<\/li>\n<li>\u677e\u5f1b\u64cd\u4f5c&#xff1a;\u5bf9\u8282\u70b9 u \u7684\u6240\u6709\u51fa\u8fb9\u8fdb\u884c\u677e\u5f1b\u64cd\u4f5c\u3002\u5982\u679c\u901a\u8fc7 u \u5230\u8fbe v \u7684\u8def\u5f84\u6bd4\u5f53\u524d\u5df2\u77e5\u8def\u5f84\u66f4\u77ed&#xff0c;\u5219\u66f4\u65b0 v \u7684\u8ddd\u79bb&#xff0c;\u5e76\u5c06 v \u52a0\u5165\u4f18\u5148\u961f\u5217\u3002<\/li>\n<li>\u6807\u8bb0\u5df2\u5904\u7406&#xff1a;\u4e00\u65e6\u8282\u70b9 u \u88ab\u53d6\u51fa\u5e76\u5904\u7406&#xff0c;\u5c31\u6807\u8bb0\u4e3a\u5df2\u5904\u7406&#xff0c;\u4e0d\u518d\u91cd\u65b0\u5904\u7406\u3002<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"584\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121539-689dd36bedf3c.png\" width=\"1027\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7531\u4e8e\u56fe\u4e2d\u6240\u6709\u8fb9\u7684\u6743\u91cd\u90fd\u662f\u975e\u8d1f\u6570&#xff0c;\u4e00\u65e6\u67d0\u4e2a\u8282\u70b9\u88ab\u6807\u8bb0\u4e3a\u5df2\u5904\u7406&#xff0c;\u6211\u4eec\u5c31\u627e\u5230\u4e86\u4ece\u6e90\u8282\u70b9\u5230\u8be5\u8282\u70b9\u7684\u6700\u77ed\u8def\u5f84&#xff0c;\u65e0\u9700\u518d\u6b21\u8003\u8651\u3002<\/p>\n<h4><\/h4>\n<h4><span style=\"color:#1a439c\">\u4ee3\u7801\u5b9e\u73b0<\/span><\/h4>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;queue&gt;<br \/>\n#include &lt;climits&gt;<br \/>\n#include &lt;utility&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/**<br \/>\n * Dijkstra\u7b97\u6cd5\u5b9e\u73b0(\u4f7f\u7528\u4f18\u5148\u961f\u5217\u4f18\u5316)<br \/>\n * &#064;param adj \u90bb\u63a5\u8868\u8868\u793a\u7684\u56fe<br \/>\n * &#064;param V \u8282\u70b9\u6570\u91cf<br \/>\n * &#064;param s \u6e90\u8282\u70b9<br \/>\n * &#064;param distance \u5b58\u50a8\u6700\u77ed\u8ddd\u79bb\u7684\u6570\u7ec4<br \/>\n * &#064;param parent \u5b58\u50a8\u524d\u9a71\u8282\u70b9\u7684\u6570\u7ec4<br \/>\n *\/<br \/>\nvoid dijkstra(const vector&lt;vector&lt;pair&lt;int, int&gt;&gt;&gt;&amp; adj, int V, int s,<br \/>\n             vector&lt;int&gt;&amp; distance, vector&lt;int&gt;&amp; parent) {<br \/>\n    \/\/ \u521d\u59cb\u5316<br \/>\n    distance.assign(V, INT_MAX);<br \/>\n    parent.assign(V, -1);<br \/>\n    distance[s] &#061; 0;<\/p>\n<p>    \/\/ \u4f18\u5148\u961f\u5217: (\u8ddd\u79bb, \u8282\u70b9), \u4f7f\u7528\u6700\u5c0f\u5806<br \/>\n    priority_queue&lt;pair&lt;int, int&gt;, vector&lt;pair&lt;int, int&gt;&gt;, greater&lt;pair&lt;int, int&gt;&gt;&gt; pq;<br \/>\n    pq.push({0, s});<\/p>\n<p>    \/\/ \u8bb0\u5f55\u5df2\u5904\u7406\u7684\u8282\u70b9<br \/>\n    vector&lt;bool&gt; processed(V, false);<\/p>\n<p>    while (!pq.empty()) {<br \/>\n        \/\/ \u63d0\u53d6\u8ddd\u79bb\u6700\u5c0f\u7684\u8282\u70b9<br \/>\n        int u &#061; pq.top().second;<br \/>\n        pq.pop();<\/p>\n<p>        \/\/ \u5982\u679c\u5df2\u5904\u7406&#xff0c;\u8df3\u8fc7<br \/>\n        if (processed[u]) continue;<br \/>\n        processed[u] &#061; true;<\/p>\n<p>        \/\/ \u677e\u5f1b\u6240\u6709\u51fa\u8fb9<br \/>\n        for (const auto&amp; edge : adj[u]) {<br \/>\n            int v &#061; edge.first;<br \/>\n            int weight &#061; edge.second;<\/p>\n<p>            \/\/ \u677e\u5f1b\u64cd\u4f5c<br \/>\n            if (distance[v] &gt; distance[u] &#043; weight) {<br \/>\n                distance[v] &#061; distance[u] &#043; weight;<br \/>\n                parent[v] &#061; u;<br \/>\n                pq.push({distance[v], v});<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6253\u5370\u8def\u5f84<br \/>\nvoid printPath(int v, const vector&lt;int&gt;&amp; parent) {<br \/>\n    if (parent[v] &#061;&#061; -1) {<br \/>\n        cout &lt;&lt; v;<br \/>\n        return;<br \/>\n    }<br \/>\n    printPath(parent[v], parent);<br \/>\n    cout &lt;&lt; &#034; -&gt; &#034; &lt;&lt; v;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u793a\u4f8b\u56fe: 6\u4e2a\u8282\u70b9(0-5)<br \/>\n    int V &#061; 6;<br \/>\n    vector&lt;vector&lt;pair&lt;int, int&gt;&gt;&gt; adj(V);<\/p>\n<p>    \/\/ \u6dfb\u52a0\u8fb9 (u, v, weight)<br \/>\n    adj[0].emplace_back(1, 7);<br \/>\n    adj[0].emplace_back(2, 9);<br \/>\n    adj[0].emplace_back(5, 14);<br \/>\n    adj[1].emplace_back(0, 7);<br \/>\n    adj[1].emplace_back(2, 10);<br \/>\n    adj[1].emplace_back(3, 15);<br \/>\n    adj[2].emplace_back(0, 9);<br \/>\n    adj[2].emplace_back(1, 10);<br \/>\n    adj[2].emplace_back(3, 11);<br \/>\n    adj[2].emplace_back(5, 2);<br \/>\n    adj[3].emplace_back(1, 15);<br \/>\n    adj[3].emplace_back(2, 11);<br \/>\n    adj[3].emplace_back(4, 6);<br \/>\n    adj[4].emplace_back(3, 6);<br \/>\n    adj[4].emplace_back(5, 9);<br \/>\n    adj[5].emplace_back(0, 14);<br \/>\n    adj[5].emplace_back(2, 2);<br \/>\n    adj[5].emplace_back(4, 9);<\/p>\n<p>    int source &#061; 0;<br \/>\n    vector&lt;int&gt; distance, parent;<\/p>\n<p>    dijkstra(adj, V, source, distance, parent);<\/p>\n<p>    cout &lt;&lt; &#034;\u4ece\u6e90\u8282\u70b9 &#034; &lt;&lt; source &lt;&lt; &#034; \u5230\u5404\u8282\u70b9\u7684\u6700\u77ed\u8ddd\u79bb:&#034; &lt;&lt; endl;<br \/>\n    for (int i &#061; 0; i &lt; V; &#043;&#043;i) {<br \/>\n        if (distance[i] &#061;&#061; INT_MAX) {<br \/>\n            cout &lt;&lt; &#034;\u8282\u70b9 &#034; &lt;&lt; i &lt;&lt; &#034;: \u4e0d\u53ef\u8fbe&#034; &lt;&lt; endl;<br \/>\n        } else {<br \/>\n            cout &lt;&lt; &#034;\u8282\u70b9 &#034; &lt;&lt; i &lt;&lt; &#034;: &#034; &lt;&lt; distance[i] &lt;&lt; &#034;, \u8def\u5f84: &#034;;<br \/>\n            printPath(i, parent);<br \/>\n            cout &lt;&lt; endl;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"867\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121540-689dd36c42a92.png\" width=\"1776\" \/><\/h4>\n<h4><\/h4>\n<h4><span style=\"color:#1a439c\">\u7efc\u5408\u6848\u4f8b\u53ca\u5e94\u7528<\/span><\/h4>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u57ce\u5e02\u4ea4\u901a\u5bfc\u822a\u7cfb\u7edf\u7684\u6848\u4f8b&#xff0c;\u5c55\u793a\u5982\u4f55\u4f7f\u7528 Dijkstra \u7b97\u6cd5\u89c4\u5212\u6700\u77ed\u8def\u5f84&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;queue&gt;<br \/>\n#include &lt;climits&gt;<br \/>\n#include &lt;utility&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u57ce\u5e02\u8282\u70b9\u7c7b<br \/>\nclass CityGraph {<br \/>\nprivate:<br \/>\n    vector&lt;vector&lt;pair&lt;int, int&gt;&gt;&gt; adj;  \/\/ \u90bb\u63a5\u8868: (\u57ce\u5e02ID, \u8ddd\u79bb)<br \/>\n    vector&lt;string&gt; cityNames;            \/\/ \u57ce\u5e02\u540d\u79f0<\/p>\n<p>public:<br \/>\n    \/\/ \u6784\u9020\u51fd\u6570<br \/>\n    CityGraph(int n) : adj(n) {}<\/p>\n<p>    \/\/ \u6dfb\u52a0\u57ce\u5e02\u540d\u79f0<br \/>\n    void addCityName(int id, const string&amp; name) {<br \/>\n        if (id &gt;&#061; cityNames.size()) {<br \/>\n            cityNames.resize(id &#043; 1);<br \/>\n        }<br \/>\n        cityNames[id] &#061; name;<br \/>\n    }<\/p>\n<p>    \/\/ \u6dfb\u52a0\u9053\u8def<br \/>\n    void addRoad(int from, int to, int distance) {<br \/>\n        adj[from].emplace_back(to, distance);<br \/>\n        adj[to].emplace_back(from, distance);  \/\/ \u5047\u8bbe\u9053\u8def\u662f\u53cc\u5411\u7684<br \/>\n    }<\/p>\n<p>    \/\/ \u67e5\u627e\u6700\u77ed\u8def\u5f84<br \/>\n    void findShortestPath(int start, int end, vector&lt;int&gt;&amp; distance, vector&lt;int&gt;&amp; parent) {<br \/>\n        int V &#061; adj.size();<br \/>\n        distance.assign(V, INT_MAX);<br \/>\n        parent.assign(V, -1);<br \/>\n        distance[start] &#061; 0;<\/p>\n<p>        priority_queue&lt;pair&lt;int, int&gt;, vector&lt;pair&lt;int, int&gt;&gt;, greater&lt;pair&lt;int, int&gt;&gt;&gt; pq;<br \/>\n        pq.push({0, start});<\/p>\n<p>        vector&lt;bool&gt; processed(V, false);<\/p>\n<p>        while (!pq.empty()) {<br \/>\n            int u &#061; pq.top().second;<br \/>\n            pq.pop();<\/p>\n<p>            if (u &#061;&#061; end) break;  \/\/ \u63d0\u524d\u7ed3\u675f&#xff0c;\u627e\u5230\u76ee\u6807\u57ce\u5e02<br \/>\n            if (processed[u]) continue;<br \/>\n            processed[u] &#061; true;<\/p>\n<p>            for (const auto&amp; edge : adj[u]) {<br \/>\n                int v &#061; edge.first;<br \/>\n                int dist &#061; edge.second;<\/p>\n<p>                if (distance[v] &gt; distance[u] &#043; dist) {<br \/>\n                    distance[v] &#061; distance[u] &#043; dist;<br \/>\n                    parent[v] &#061; u;<br \/>\n                    pq.push({distance[v], v});<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u6253\u5370\u8def\u5f84<br \/>\n    void printPath(int v, const vector&lt;int&gt;&amp; parent) {<br \/>\n        if (parent[v] &#061;&#061; -1) {<br \/>\n            cout &lt;&lt; cityNames[v];<br \/>\n            return;<br \/>\n        }<br \/>\n        printPath(parent[v], parent);<br \/>\n        cout &lt;&lt; &#034; -&gt; &#034; &lt;&lt; cityNames[v];<br \/>\n    }<\/p>\n<p>    \/\/ \u83b7\u53d6\u57ce\u5e02\u540d\u79f0<br \/>\n    string getCityName(int id) const {<br \/>\n        if (id &gt;&#061; 0 &amp;&amp; id &lt; cityNames.size()) {<br \/>\n            return cityNames[id];<br \/>\n        }<br \/>\n        return &#034;\u672a\u77e5\u57ce\u5e02&#034;;<br \/>\n    }<br \/>\n};<\/p>\n<p>int main() {<br \/>\n    \/\/ \u521b\u5efa\u57ce\u5e02\u56fe(8\u4e2a\u57ce\u5e02)<br \/>\n    CityGraph graph(8);<\/p>\n<p>    \/\/ \u6dfb\u52a0\u57ce\u5e02\u540d\u79f0<br \/>\n    graph.addCityName(0, &#034;\u5317\u4eac&#034;);<br \/>\n    graph.addCityName(1, &#034;\u5929\u6d25&#034;);<br \/>\n    graph.addCityName(2, &#034;\u77f3\u5bb6\u5e84&#034;);<br \/>\n    graph.addCityName(3, &#034;\u592a\u539f&#034;);<br \/>\n    graph.addCityName(4, &#034;\u6d4e\u5357&#034;);<br \/>\n    graph.addCityName(5, &#034;\u90d1\u5dde&#034;);<br \/>\n    graph.addCityName(6, &#034;\u897f\u5b89&#034;);<br \/>\n    graph.addCityName(7, &#034;\u6b66\u6c49&#034;);<\/p>\n<p>    \/\/ \u6dfb\u52a0\u57ce\u5e02\u95f4\u9053\u8def(\u8ddd\u79bb\u5355\u4f4d: \u516c\u91cc)<br \/>\n    graph.addRoad(0, 1, 137);   \/\/ \u5317\u4eac-\u5929\u6d25<br \/>\n    graph.addRoad(0, 2, 283);   \/\/ \u5317\u4eac-\u77f3\u5bb6\u5e84<br \/>\n    graph.addRoad(0, 4, 497);   \/\/ \u5317\u4eac-\u6d4e\u5357<br \/>\n    graph.addRoad(1, 4, 357);   \/\/ \u5929\u6d25-\u6d4e\u5357<br \/>\n    graph.addRoad(2, 3, 231);   \/\/ \u77f3\u5bb6\u5e84-\u592a\u539f<br \/>\n    graph.addRoad(2, 5, 412);   \/\/ \u77f3\u5bb6\u5e84-\u90d1\u5dde<br \/>\n    graph.addRoad(3, 6, 511);   \/\/ \u592a\u539f-\u897f\u5b89<br \/>\n    graph.addRoad(4, 5, 374);   \/\/ \u6d4e\u5357-\u90d1\u5dde<br \/>\n    graph.addRoad(5, 6, 480);   \/\/ \u90d1\u5dde-\u897f\u5b89<br \/>\n    graph.addRoad(5, 7, 536);   \/\/ \u90d1\u5dde-\u6b66\u6c49<br \/>\n    graph.addRoad(6, 7, 691);   \/\/ \u897f\u5b89-\u6b66\u6c49<\/p>\n<p>    int start &#061; 0;  \/\/ \u5317\u4eac<br \/>\n    int end &#061; 7;    \/\/ \u6b66\u6c49<br \/>\n    vector&lt;int&gt; distance, parent;<\/p>\n<p>    graph.findShortestPath(start, end, distance, parent);<\/p>\n<p>    cout &lt;&lt; &#034;\u57ce\u5e02\u5bfc\u822a\u7ed3\u679c:&#034; &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;\u4ece &#034; &lt;&lt; graph.getCityName(start) &lt;&lt; &#034; \u5230 &#034; &lt;&lt; graph.getCityName(end)<br \/>\n         &lt;&lt; &#034; \u7684\u6700\u77ed\u8ddd\u79bb\u4e3a: &#034; &lt;&lt; distance[end] &lt;&lt; &#034; \u516c\u91cc&#034; &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;\u63a8\u8350\u8def\u7ebf: &#034;;<br \/>\n    graph.printPath(end, parent);<br \/>\n    cout &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"757\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121540-689dd36cd5ee0.png\" width=\"1759\" \/><\/h3>\n<h4><\/h4>\n<h3><span style=\"color:#be191c\">24.4 \u5dee\u5206\u7ea6\u675f\u548c\u6700\u77ed\u8def\u5f84<\/span><\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf\u662f\u4e00\u7c7b\u7279\u6b8a\u7684\u7ebf\u6027\u89c4\u5212\u95ee\u9898&#xff0c;\u5b83\u53ef\u4ee5\u8f6c\u5316\u4e3a\u5355\u6e90\u6700\u77ed\u8def\u5f84\u95ee\u9898\u6765\u6c42\u89e3\u3002\u901a\u8fc7\u5c06\u6bcf\u4e2a\u7ea6\u675f\u6761\u4ef6\u8f6c\u5316\u4e3a\u56fe\u4e2d\u7684\u4e00\u6761\u8fb9&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\u627e\u5230\u6ee1\u8db3\u6240\u6709\u7ea6\u675f\u7684\u89e3\u3002<\/p>\n<h4><span style=\"color:#511b78\">\u7b97\u6cd5\u601d\u60f3<\/span><\/h4>\n<li>\u5efa\u6a21&#xff1a;\u5c06\u6bcf\u4e2a\u53d8\u91cf(x_i)\u8868\u793a\u4e3a\u56fe\u4e2d\u7684\u4e00\u4e2a\u8282\u70b9i\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u7ea6\u675f\u6761\u4ef6(x_j &#8211; x_i \\\\leq w)&#xff0c;\u521b\u5efa\u4e00\u6761\u4ece\u8282\u70b9i\u5230\u8282\u70b9j\u3001\u6743\u91cd\u4e3aw\u7684\u8fb9\u3002<\/li>\n<li>\u5f15\u5165\u8d85\u7ea7\u6e90\u8282\u70b9&#xff1a;\u4e3a\u4e86\u4fdd\u8bc1\u56fe\u7684\u8fde\u901a\u6027&#xff0c;\u5f15\u5165\u4e00\u4e2a\u8d85\u7ea7\u6e90\u8282\u70b9 0&#xff0c;\u5e76\u6dfb\u52a0\u4ece 0 \u5230\u6240\u6709\u5176\u4ed6\u8282\u70b9\u7684\u8fb9&#xff0c;\u6743\u91cd\u4e3a 0\u3002<\/li>\n<li>\u6c42\u89e3\u6700\u77ed\u8def\u5f84&#xff1a;\u4f7f\u7528 Bellman-Ford \u7b97\u6cd5\u8ba1\u7b97\u4ece\u8d85\u7ea7\u6e90\u8282\u70b9\u5230\u6240\u6709\u5176\u4ed6\u8282\u70b9\u7684\u6700\u77ed\u8def\u5f84\u3002\u5982\u679c\u56fe\u4e2d\u5b58\u5728\u8d1f\u6743\u56de\u8def&#xff0c;\u5219\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf\u65e0\u89e3&#xff1b;\u5426\u5219&#xff0c;\u6700\u77ed\u8def\u5f84\u8ddd\u79bb\u5c31\u662f\u7cfb\u7edf\u7684\u4e00\u4e2a\u53ef\u884c\u89e3\u3002<\/li>\n<h4><span style=\"color:#1a439c\">\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf\u5efa\u6a21\u793a\u610f\u56fe<\/span><\/h4>\n<p>\u7ea6\u675f\u6761\u4ef6:<br \/>\nx1 &#8211; x0 \u2264 3<br \/>\nx2 &#8211; x0 \u2264 5<br \/>\nx3 &#8211; x1 \u2264 2<br \/>\nx2 &#8211; x1 \u2264 2<br \/>\nx3 &#8211; x2 \u2264 -1<\/p>\n<p>\u8f6c\u5316\u4e3a\u56fe:<br \/>\n0 -&gt; 1 (\u6743\u91cd3)<br \/>\n0 -&gt; 2 (\u6743\u91cd5)<br \/>\n1 -&gt; 3 (\u6743\u91cd2)<br \/>\n1 -&gt; 2 (\u6743\u91cd2)<br \/>\n2 -&gt; 3 (\u6743\u91cd-1)<\/p>\n<h4><span style=\"color:#1a439c\">\u4ee3\u7801\u5b9e\u73b0<\/span><\/h4>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;climits&gt;<br \/>\nusing namespace std;<\/p>\n<p>struct Edge {<br \/>\n    int u, v, weight;<br \/>\n    Edge(int u_, int v_, int w_) : u(u_), v(v_), weight(w_) {}<br \/>\n};<\/p>\n<p>\/**<br \/>\n * \u6c42\u89e3\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf<br \/>\n * &#064;param edges \u7ea6\u675f\u6761\u4ef6\u8f6c\u5316\u7684\u8fb9<br \/>\n * &#064;param n \u53d8\u91cf\u6570\u91cf<br \/>\n * &#064;param x \u5b58\u50a8\u89e3\u7684\u6570\u7ec4<br \/>\n * &#064;return \u662f\u5426\u6709\u89e3<br \/>\n *\/<br \/>\nbool solveDifferenceConstraints(const vector&lt;Edge&gt;&amp; edges, int n, vector&lt;int&gt;&amp; x) {<br \/>\n    \/\/ \u6dfb\u52a0\u8d85\u7ea7\u6e90\u8282\u70b90&#xff0c;\u8fde\u63a5\u5230\u6240\u6709\u5176\u4ed6\u8282\u70b9<br \/>\n    vector&lt;Edge&gt; newEdges &#061; edges;<br \/>\n    for (int i &#061; 1; i &lt;&#061; n; &#043;&#043;i) {<br \/>\n        newEdges.emplace_back(0, i, 0);<br \/>\n    }<\/p>\n<p>    \/\/ \u4f7f\u7528Bellman-Ford\u7b97\u6cd5\u6c42\u89e3<br \/>\n    x.assign(n &#043; 1, INT_MAX);  \/\/ x[0]\u5230x[n]<br \/>\n    x[0] &#061; 0;<\/p>\n<p>    \/\/ \u677e\u5f1bn\u6b21<br \/>\n    for (int i &#061; 0; i &lt; n; &#043;&#043;i) {<br \/>\n        for (const Edge&amp; e : newEdges) {<br \/>\n            if (x[e.u] !&#061; INT_MAX &amp;&amp; x[e.v] &gt; x[e.u] &#043; e.weight) {<br \/>\n                x[e.v] &#061; x[e.u] &#043; e.weight;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u68c0\u6d4b\u8d1f\u6743\u56de\u8def<br \/>\n    for (const Edge&amp; e : newEdges) {<br \/>\n        if (x[e.u] !&#061; INT_MAX &amp;&amp; x[e.v] &gt; x[e.u] &#043; e.weight) {<br \/>\n            return false;  \/\/ \u5b58\u5728\u8d1f\u6743\u56de\u8def&#xff0c;\u65e0\u89e3<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return true;  \/\/ \u6709\u89e3<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u793a\u4f8b: \u6c42\u89e3\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf<br \/>\n    \/\/ \u53d8\u91cf: x1, x2, x3<br \/>\n    \/\/ \u7ea6\u675f:<br \/>\n    \/\/ x1 &#8211; x2 &lt;&#061; 1<br \/>\n    \/\/ x1 &#8211; x3 &lt;&#061; 3<br \/>\n    \/\/ x2 &#8211; x3 &lt;&#061; 2<br \/>\n    \/\/ x3 &#8211; x2 &lt;&#061; -1<br \/>\n    \/\/ x2 &#8211; x1 &lt;&#061; -2<\/p>\n<p>    int n &#061; 3;  \/\/ \u53d8\u91cf\u6570\u91cf<br \/>\n    vector&lt;Edge&gt; edges;<\/p>\n<p>    \/\/ \u6dfb\u52a0\u7ea6\u675f\u5bf9\u5e94\u7684\u8fb9<br \/>\n    edges.emplace_back(2, 1, 1);   \/\/ x1 &#8211; x2 &lt;&#061; 1<br \/>\n    edges.emplace_back(3, 1, 3);   \/\/ x1 &#8211; x3 &lt;&#061; 3<br \/>\n    edges.emplace_back(3, 2, 2);   \/\/ x2 &#8211; x3 &lt;&#061; 2<br \/>\n    edges.emplace_back(2, 3, -1);  \/\/ x3 &#8211; x2 &lt;&#061; -1<br \/>\n    edges.emplace_back(1, 2, -2);  \/\/ x2 &#8211; x1 &lt;&#061; -2<\/p>\n<p>    vector&lt;int&gt; x;<br \/>\n    bool hasSolution &#061; solveDifferenceConstraints(edges, n, x);<\/p>\n<p>    if (hasSolution) {<br \/>\n        cout &lt;&lt; &#034;\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf\u7684\u4e00\u4e2a\u89e3\u4e3a:&#034; &lt;&lt; endl;<br \/>\n        for (int i &#061; 1; i &lt;&#061; n; &#043;&#043;i) {<br \/>\n            cout &lt;&lt; &#034;x&#034; &lt;&lt; i &lt;&lt; &#034; &#061; &#034; &lt;&lt; x[i] &lt;&lt; endl;<br \/>\n        }<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf\u65e0\u89e3&#034; &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"930\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121541-689dd36d7f395.png\" width=\"1762\" \/><\/h4>\n<h4><\/h4>\n<h4><span style=\"color:#1a439c\">\u7efc\u5408\u6848\u4f8b\u53ca\u5e94\u7528<\/span><\/h4>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u8bfe\u7a0b\u5b89\u6392\u7684\u6848\u4f8b&#xff0c;\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf\u89e3\u51b3\u5b9e\u9645\u95ee\u9898&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;climits&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>struct Edge {<br \/>\n    int u, v, weight;<br \/>\n    Edge(int u_, int v_, int w_) : u(u_), v(v_), weight(w_) {}<br \/>\n};<\/p>\n<p>bool solveCourseScheduling(const vector&lt;Edge&gt;&amp; edges, int n, vector&lt;int&gt;&amp; schedule) {<br \/>\n    vector&lt;Edge&gt; newEdges &#061; edges;<br \/>\n    \/\/ \u6dfb\u52a0\u8d85\u7ea7\u6e90\u8282\u70b90<br \/>\n    for (int i &#061; 1; i &lt;&#061; n; &#043;&#043;i) {<br \/>\n        newEdges.emplace_back(0, i, 0);<br \/>\n    }<\/p>\n<p>    schedule.assign(n &#043; 1, INT_MAX);<br \/>\n    schedule[0] &#061; 0;<\/p>\n<p>    \/\/ \u677e\u5f1b\u64cd\u4f5c<br \/>\n    for (int i &#061; 0; i &lt; n; &#043;&#043;i) {<br \/>\n        for (const Edge&amp; e : newEdges) {<br \/>\n            if (schedule[e.u] !&#061; INT_MAX &amp;&amp; schedule[e.v] &gt; schedule[e.u] &#043; e.weight) {<br \/>\n                schedule[e.v] &#061; schedule[e.u] &#043; e.weight;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u68c0\u6d4b\u8d1f\u6743\u56de\u8def<br \/>\n    for (const Edge&amp; e : newEdges) {<br \/>\n        if (schedule[e.u] !&#061; INT_MAX &amp;&amp; schedule[e.v] &gt; schedule[e.u] &#043; e.weight) {<br \/>\n            return false;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return true;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u8bfe\u7a0b\u5b89\u6392\u95ee\u9898<br \/>\n    \/\/ \u8bfe\u7a0b: 1(\u7b97\u6cd5), 2(\u6570\u636e\u7ed3\u6784), 3(\u7a0b\u5e8f\u8bbe\u8ba1), 4(\u8ba1\u7b97\u673a\u7f51\u7edc), 5(\u64cd\u4f5c\u7cfb\u7edf)<br \/>\n    \/\/ \u7ea6\u675f\u6761\u4ef6:<br \/>\n    \/\/ 1. \u6570\u636e\u7ed3\u6784\u5fc5\u987b\u5728\u7b97\u6cd5\u4e4b\u540e\u81f3\u5c111\u5468: x1 &#043; 1 &lt;&#061; x2 \u2192 x2 &#8211; x1 &gt;&#061; 1 \u2192 x1 &#8211; x2 &lt;&#061; -1<br \/>\n    \/\/ 2. \u7b97\u6cd5\u5fc5\u987b\u5728\u7a0b\u5e8f\u8bbe\u8ba1\u4e4b\u540e: x3 &lt;&#061; x1 \u2192 x1 &#8211; x3 &gt;&#061; 0 \u2192 x3 &#8211; x1 &lt;&#061; 0<br \/>\n    \/\/ 3. \u8ba1\u7b97\u673a\u7f51\u7edc\u5fc5\u987b\u5728\u6570\u636e\u7ed3\u6784\u4e4b\u540e\u81f3\u5c112\u5468: x2 &#043; 2 &lt;&#061; x4 \u2192 x4 &#8211; x2 &gt;&#061; 2 \u2192 x2 &#8211; x4 &lt;&#061; -2<br \/>\n    \/\/ 4. \u64cd\u4f5c\u7cfb\u7edf\u5fc5\u987b\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u4e4b\u540e: x4 &lt;&#061; x5 \u2192 x5 &#8211; x4 &gt;&#061; 0 \u2192 x4 &#8211; x5 &lt;&#061; 0<br \/>\n    \/\/ 5. \u64cd\u4f5c\u7cfb\u7edf\u5fc5\u987b\u5728\u6570\u636e\u7ed3\u6784\u4e4b\u540e\u81f3\u5c113\u5468: x2 &#043; 3 &lt;&#061; x5 \u2192 x5 &#8211; x2 &gt;&#061; 3 \u2192 x2 &#8211; x5 &lt;&#061; -3<br \/>\n    \/\/ 6. \u6240\u6709\u8bfe\u7a0b\u5fc5\u987b\u572810\u5468\u5185\u5b8c\u6210: xi &lt;&#061; 10 \u2192 xi &#8211; x0 &lt;&#061; 10 (x0&#061;0)<\/p>\n<p>    int n &#061; 5;  \/\/ 5\u95e8\u8bfe\u7a0b<br \/>\n    vector&lt;Edge&gt; edges;<br \/>\n    vector&lt;string&gt; courseNames &#061; {&#034;&#034;, &#034;\u7b97\u6cd5&#034;, &#034;\u6570\u636e\u7ed3\u6784&#034;, &#034;\u7a0b\u5e8f\u8bbe\u8ba1&#034;, &#034;\u8ba1\u7b97\u673a\u7f51\u7edc&#034;, &#034;\u64cd\u4f5c\u7cfb\u7edf&#034;};<\/p>\n<p>    \/\/ \u6dfb\u52a0\u7ea6\u675f\u5bf9\u5e94\u7684\u8fb9<br \/>\n    edges.emplace_back(2, 1, -1);  \/\/ \u7ea6\u675f1<br \/>\n    edges.emplace_back(1, 3, 0);   \/\/ \u7ea6\u675f2<br \/>\n    edges.emplace_back(4, 2, -2);  \/\/ \u7ea6\u675f3<br \/>\n    edges.emplace_back(5, 4, 0);   \/\/ \u7ea6\u675f4<br \/>\n    edges.emplace_back(5, 2, -3);  \/\/ \u7ea6\u675f5<\/p>\n<p>    \/\/ \u6240\u6709\u8bfe\u7a0b\u5fc5\u987b\u572810\u5468\u5185\u5b8c\u6210<br \/>\n    for (int i &#061; 1; i &lt;&#061; n; &#043;&#043;i) {<br \/>\n        edges.emplace_back(i, 0, 10);  \/\/ xi &#8211; x0 &lt;&#061; 10<br \/>\n    }<\/p>\n<p>    vector&lt;int&gt; schedule;<br \/>\n    bool possible &#061; solveCourseScheduling(edges, n, schedule);<\/p>\n<p>    if (possible) {<br \/>\n        cout &lt;&lt; &#034;\u8bfe\u7a0b\u5b89\u6392\u65b9\u6848:&#034; &lt;&lt; endl;<br \/>\n        for (int i &#061; 1; i &lt;&#061; n; &#043;&#043;i) {<br \/>\n            cout &lt;&lt; courseNames[i] &lt;&lt; &#034; \u5b89\u6392\u5728\u7b2c &#034; &lt;&lt; schedule[i] &lt;&lt; &#034; \u5468&#034; &lt;&lt; endl;<br \/>\n        }<br \/>\n        cout &lt;&lt; &#034;\u6240\u6709\u8bfe\u7a0b\u5c06\u5728\u7b2c &#034; &lt;&lt; schedule[5] &lt;&lt; &#034; \u5468\u524d\u5b8c\u6210&#034; &lt;&lt; endl;<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;\u65e0\u6cd5\u5b89\u6392\u8fd9\u4e9b\u8bfe\u7a0b&#xff0c;\u5b58\u5728\u51b2\u7a81\u7684\u5148\u4fee\u5173\u7cfb&#034; &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1000\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121542-689dd36e48d59.png\" width=\"1702\" \/><\/h3>\n<h4><\/h4>\n<h3><span style=\"color:#be191c\">\u601d\u8003\u9898<\/span><\/h3>\n<li>\n<p>\u6bd4\u8f83\u4e0d\u540c\u7b97\u6cd5&#xff1a;\u5bf9\u4e8e\u4e00\u4e2a\u6709 1000 \u4e2a\u8282\u70b9\u548c 10000 \u6761\u8fb9\u7684\u56fe&#xff0c;\u5206\u522b\u8ba8\u8bba\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u9009\u62e9 Bellman-Ford\u3001DAG \u6700\u77ed\u8def\u5f84\u7b97\u6cd5\u6216 Dijkstra \u7b97\u6cd5\u66f4\u5408\u9002\u3002<\/p>\n<\/li>\n<li>\n<p>\u8d1f\u6743\u8fb9\u5904\u7406&#xff1a;\u5982\u679c\u56fe\u4e2d\u5b58\u5728\u8d1f\u6743\u8fb9\u4f46\u4e0d\u5b58\u5728\u8d1f\u6743\u56de\u8def&#xff0c;Dijkstra \u7b97\u6cd5\u662f\u5426\u4ecd\u7136\u9002\u7528&#xff1f;\u4e3a\u4ec0\u4e48&#xff1f;\u8bbe\u8ba1\u4e00\u4e2a\u53cd\u4f8b\u6765\u8bf4\u660e\u4f60\u7684\u7ed3\u8bba\u3002<\/p>\n<\/li>\n<li>\n<p>\u7b97\u6cd5\u4f18\u5316&#xff1a;\u5982\u4f55\u4f18\u5316 Dijkstra \u7b97\u6cd5\u4f7f\u5176\u5728\u7a00\u758f\u56fe\u4e0a\u7684\u6027\u80fd\u66f4\u597d&#xff1f;\u9664\u4e86\u4f18\u5148\u961f\u5217&#xff0c;\u8fd8\u6709\u5176\u4ed6\u53ef\u80fd\u7684\u4f18\u5316\u65b9\u6cd5\u5417&#xff1f;<\/p>\n<\/li>\n<li>\n<p>\u5dee\u5206\u7ea6\u675f\u6269\u5c55&#xff1a;\u5982\u4f55\u5c06\u4e0d\u7b49\u5f0f\u00a0(x_j &#8211; x_i \\\\geq w)\u00a0\u8f6c\u5316\u4e3a\u9002\u5408\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf\u7684\u5f62\u5f0f&#xff1f;\u8fd9\u5bf9\u5e94\u56fe\u4e2d\u7684\u4ec0\u4e48\u8fb9&#xff1f;<\/p>\n<\/li>\n<li>\n<p>\u6700\u77ed\u8def\u5f84\u6811&#xff1a;\u8bc1\u660e\u6700\u77ed\u8def\u5f84\u6811\u662f\u552f\u4e00\u7684\u5f53\u4e14\u4ec5\u5f53\u5bf9\u4e8e\u6bcf\u4e2a\u8282\u70b9 v&#xff0c;\u4ece\u6e90\u8282\u70b9 s \u5230 v \u7684\u6700\u77ed\u8def\u5f84\u662f\u552f\u4e00\u7684\u3002<\/p>\n<\/li>\n<h3 style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121543-689dd36f0ecd4.jpg\" \/><\/h3>\n<h3><span style=\"color:#be191c\">\u672c\u7ae0\u6ce8\u8bb0<\/span><\/h3>\n<p>\u5355\u6e90\u6700\u77ed\u8def\u5f84\u95ee\u9898\u662f\u56fe\u8bba\u4e2d\u7684\u57fa\u7840\u95ee\u9898&#xff0c;\u672c\u7ae0\u4ecb\u7ecd\u7684\u7b97\u6cd5\u5404\u6709\u5176\u9002\u7528\u573a\u666f&#xff1a;<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121543-689dd36f39bc4.jpg\" \/><\/p>\n<ul>\n<li>\n<p>Bellman-Ford \u7b97\u6cd5&#xff1a;\u9002\u7528\u4e8e\u5b58\u5728\u8d1f\u6743\u8fb9\u7684\u56fe&#xff0c;\u80fd\u591f\u68c0\u6d4b\u8d1f\u6743\u56de\u8def&#xff0c;\u4f46\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8&#xff08;O (VE)&#xff09;\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d&#xff0c;\u5176\u961f\u5217\u4f18\u5316\u7248\u672c&#xff08;SPFA \u7b97\u6cd5&#xff09;\u66f4\u4e3a\u5e38\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>DAG \u6700\u77ed\u8def\u5f84\u7b97\u6cd5&#xff1a;\u662f\u5904\u7406\u6709\u5411\u65e0\u73af\u56fe\u7684\u9ad8\u6548\u7b97\u6cd5&#xff08;O (V&#043;E)&#xff09;&#xff0c;\u53ef\u4ee5\u5904\u7406\u8d1f\u6743\u8fb9&#xff0c;\u7279\u522b\u9002\u5408\u4efb\u52a1\u8c03\u5ea6\u3001\u4f9d\u8d56\u5206\u6790\u7b49\u573a\u666f\u3002<\/p>\n<\/li>\n<li>\n<p>Dijkstra \u7b97\u6cd5&#xff1a;\u662f\u5904\u7406\u975e\u8d1f\u6743\u56fe\u7684\u9ad8\u6548\u7b97\u6cd5&#xff0c;\u4f7f\u7528\u4f18\u5148\u961f\u5217\u4f18\u5316\u540e\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O (ElogV)&#xff0c;\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5bfc\u822a\u7cfb\u7edf\u3001\u7f51\u7edc\u8def\u7531\u7b49\u9886\u57df\u3002<\/p>\n<\/li>\n<li>\n<p>\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf&#xff1a;\u5c55\u793a\u4e86\u56fe\u8bba\u4e0e\u7ebf\u6027\u89c4\u5212\u7684\u6df1\u523b\u8054\u7cfb&#xff0c;\u63d0\u4f9b\u4e86\u4e00\u79cd\u6c42\u89e3\u7279\u5b9a\u7c7b\u578b\u7ea6\u675f\u95ee\u9898\u7684\u6709\u6548\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<\/ul>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121543-689dd36f566c9.webp\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\u7684\u7814\u7a76\u4ecd\u5728\u7ee7\u7eed&#xff0c;\u65b0\u7684\u4f18\u5316\u65b9\u6cd5\u548c\u53d8\u4f53\u4e0d\u65ad\u6d8c\u73b0&#xff0c;\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u548c\u6027\u80fd\u9700\u6c42\u3002\u4f8b\u5982&#xff0c;\u9488\u5bf9\u5927\u89c4\u6a21\u56fe\u7684\u5206\u5e03\u5f0f\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\u3001\u8003\u8651\u65f6\u6548\u6027\u7684\u52a8\u6001\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\u7b49&#xff0c;\u90fd\u662f\u5f53\u524d\u7814\u7a76\u7684\u70ed\u70b9\u65b9\u5411\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121543-689dd36f719de.jpg\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7406\u89e3\u8fd9\u4e9b\u7b97\u6cd5\u7684\u539f\u7406\u548c\u6027\u8d28&#xff0c;\u4e0d\u4ec5\u6709\u52a9\u4e8e\u6211\u4eec\u5728\u5b9e\u9645\u95ee\u9898\u4e2d\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5&#xff0c;\u4e5f\u4e3a\u5b66\u4e60\u66f4\u590d\u6742\u7684\u56fe\u7b97\u6cd5\u5960\u5b9a\u4e86\u57fa\u7840\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb16\u6b21\u3002\u672c\u6587\u7cfb\u7edf\u4ecb\u7ecd\u4e86\u5355\u6e90\u6700\u77ed\u8def\u5f84\u95ee\u9898\u7684\u56db\u79cd\u7ecf\u5178\u7b97\u6cd5\u53ca\u5176C++\u5b9e\u73b0\u3002\u9996\u5148\u8be6\u7ec6\u8bb2\u89e3\u4e86Bellman-Ford\u7b97\u6cd5\u7684\u539f\u7406\u548c\u4ee3\u7801\u5b9e\u73b0\uff0c\u5305\u62ec\u8d1f\u6743\u8fb9\u5904\u7406\u548c\u8d1f\u6743\u56de\u8def\u68c0\u6d4b\uff1b\u5176\u6b21\u9610\u8ff0\u4e86DAG\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\uff0c\u5c55\u793a\u5176\u5728\u62d3\u6251\u6392\u5e8f\u540e\u7684\u9ad8\u6548\u8ba1\u7b97\uff1b\u7136\u540e\u91cd\u70b9\u4ecb\u7ecd\u4e86Dijkstra\u7b97\u6cd5\u53ca\u5176\u4f18\u5148\u961f\u5217\u4f18\u5316\uff1b\u6700\u540e\u63a2\u8ba8\u4e86\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf\u4e0e\u6700\u77ed\u8def\u5f84\u7684\u5173\u7cfb\u3002\u6587\u7ae0\u901a\u8fc7\u7269\u6d41\u89c4\u5212\u3001\u4ea4\u901a\u5bfc\u822a\u3001\u8bfe\u7a0b\u5b89\u6392\u7b49\u5b9e\u9645\u6848\u4f8b\uff0c\u5c55\u793a\u4e86\u8fd9\u4e9b\u7b97\u6cd5\u5728\u5de5\u7a0b\u5b9e\u8df5\u4e2d\u7684\u5e94\u7528\u573a\u666f\u3002\u56db\u79cd\u7b97\u6cd5\u5404\u6709\u9002\u7528\u573a\u666f\uff1aBellman-Ford\u9002\u7528\u4e8e\u542b\u8d1f\u6743\u8fb9\u7684\u56fe\uff08O(VE)\uff09\uff0cDAG\u7b97\u6cd5\u9488\u5bf9\u65e0\u73af\u56fe\uff08O(V+E)\uff09\uff0cDij<\/p>\n","protected":false},"author":2,"featured_media":56538,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,230,190,1813,427],"topic":[],"class_list":["post-56555","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-php","tag-190","tag-1813","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u7b2c 24 \u7ae0 - \u5355\u6e90\u6700\u77ed\u8def\u5f84 - \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\/56555.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u7b2c 24 \u7ae0 - \u5355\u6e90\u6700\u77ed\u8def\u5f84 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb16\u6b21\u3002\u672c\u6587\u7cfb\u7edf\u4ecb\u7ecd\u4e86\u5355\u6e90\u6700\u77ed\u8def\u5f84\u95ee\u9898\u7684\u56db\u79cd\u7ecf\u5178\u7b97\u6cd5\u53ca\u5176C++\u5b9e\u73b0\u3002\u9996\u5148\u8be6\u7ec6\u8bb2\u89e3\u4e86Bellman-Ford\u7b97\u6cd5\u7684\u539f\u7406\u548c\u4ee3\u7801\u5b9e\u73b0\uff0c\u5305\u62ec\u8d1f\u6743\u8fb9\u5904\u7406\u548c\u8d1f\u6743\u56de\u8def\u68c0\u6d4b\uff1b\u5176\u6b21\u9610\u8ff0\u4e86DAG\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\uff0c\u5c55\u793a\u5176\u5728\u62d3\u6251\u6392\u5e8f\u540e\u7684\u9ad8\u6548\u8ba1\u7b97\uff1b\u7136\u540e\u91cd\u70b9\u4ecb\u7ecd\u4e86Dijkstra\u7b97\u6cd5\u53ca\u5176\u4f18\u5148\u961f\u5217\u4f18\u5316\uff1b\u6700\u540e\u63a2\u8ba8\u4e86\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf\u4e0e\u6700\u77ed\u8def\u5f84\u7684\u5173\u7cfb\u3002\u6587\u7ae0\u901a\u8fc7\u7269\u6d41\u89c4\u5212\u3001\u4ea4\u901a\u5bfc\u822a\u3001\u8bfe\u7a0b\u5b89\u6392\u7b49\u5b9e\u9645\u6848\u4f8b\uff0c\u5c55\u793a\u4e86\u8fd9\u4e9b\u7b97\u6cd5\u5728\u5de5\u7a0b\u5b9e\u8df5\u4e2d\u7684\u5e94\u7528\u573a\u666f\u3002\u56db\u79cd\u7b97\u6cd5\u5404\u6709\u9002\u7528\u573a\u666f\uff1aBellman-Ford\u9002\u7528\u4e8e\u542b\u8d1f\u6743\u8fb9\u7684\u56fe\uff08O(VE)\uff09\uff0cDAG\u7b97\u6cd5\u9488\u5bf9\u65e0\u73af\u56fe\uff08O(V+E)\uff09\uff0cDij\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/56555.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-14T12:15:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121534-689dd36677afd.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=\"15 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/56555.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/56555.html\",\"name\":\"\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u7b2c 24 \u7ae0 - \u5355\u6e90\u6700\u77ed\u8def\u5f84 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-14T12:15:44+00:00\",\"dateModified\":\"2025-08-14T12:15:44+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/56555.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/56555.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/56555.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u7b2c 24 \u7ae0 - \u5355\u6e90\u6700\u77ed\u8def\u5f84\"}]},{\"@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":"\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u7b2c 24 \u7ae0 - \u5355\u6e90\u6700\u77ed\u8def\u5f84 - \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\/56555.html","og_locale":"zh_CN","og_type":"article","og_title":"\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u7b2c 24 \u7ae0 - \u5355\u6e90\u6700\u77ed\u8def\u5f84 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb16\u6b21\u3002\u672c\u6587\u7cfb\u7edf\u4ecb\u7ecd\u4e86\u5355\u6e90\u6700\u77ed\u8def\u5f84\u95ee\u9898\u7684\u56db\u79cd\u7ecf\u5178\u7b97\u6cd5\u53ca\u5176C++\u5b9e\u73b0\u3002\u9996\u5148\u8be6\u7ec6\u8bb2\u89e3\u4e86Bellman-Ford\u7b97\u6cd5\u7684\u539f\u7406\u548c\u4ee3\u7801\u5b9e\u73b0\uff0c\u5305\u62ec\u8d1f\u6743\u8fb9\u5904\u7406\u548c\u8d1f\u6743\u56de\u8def\u68c0\u6d4b\uff1b\u5176\u6b21\u9610\u8ff0\u4e86DAG\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\uff0c\u5c55\u793a\u5176\u5728\u62d3\u6251\u6392\u5e8f\u540e\u7684\u9ad8\u6548\u8ba1\u7b97\uff1b\u7136\u540e\u91cd\u70b9\u4ecb\u7ecd\u4e86Dijkstra\u7b97\u6cd5\u53ca\u5176\u4f18\u5148\u961f\u5217\u4f18\u5316\uff1b\u6700\u540e\u63a2\u8ba8\u4e86\u5dee\u5206\u7ea6\u675f\u7cfb\u7edf\u4e0e\u6700\u77ed\u8def\u5f84\u7684\u5173\u7cfb\u3002\u6587\u7ae0\u901a\u8fc7\u7269\u6d41\u89c4\u5212\u3001\u4ea4\u901a\u5bfc\u822a\u3001\u8bfe\u7a0b\u5b89\u6392\u7b49\u5b9e\u9645\u6848\u4f8b\uff0c\u5c55\u793a\u4e86\u8fd9\u4e9b\u7b97\u6cd5\u5728\u5de5\u7a0b\u5b9e\u8df5\u4e2d\u7684\u5e94\u7528\u573a\u666f\u3002\u56db\u79cd\u7b97\u6cd5\u5404\u6709\u9002\u7528\u573a\u666f\uff1aBellman-Ford\u9002\u7528\u4e8e\u542b\u8d1f\u6743\u8fb9\u7684\u56fe\uff08O(VE)\uff09\uff0cDAG\u7b97\u6cd5\u9488\u5bf9\u65e0\u73af\u56fe\uff08O(V+E)\uff09\uff0cDij","og_url":"https:\/\/www.wsisp.com\/helps\/56555.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-14T12:15:44+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814121534-689dd36677afd.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"15 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/56555.html","url":"https:\/\/www.wsisp.com\/helps\/56555.html","name":"\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u7b2c 24 \u7ae0 - \u5355\u6e90\u6700\u77ed\u8def\u5f84 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-14T12:15:44+00:00","dateModified":"2025-08-14T12:15:44+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/56555.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/56555.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/56555.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u7b2c 24 \u7ae0 - \u5355\u6e90\u6700\u77ed\u8def\u5f84"}]},{"@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\/56555","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=56555"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/56555\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/56538"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=56555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=56555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=56555"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=56555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}