{"id":435,"date":"2020-05-08T15:25:27","date_gmt":"2020-05-08T07:25:27","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=435"},"modified":"2020-05-15T23:56:52","modified_gmt":"2020-05-15T15:56:52","slug":"spfa","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/05\/08\/spfa\/","title":{"rendered":"SPFA"},"content":{"rendered":"<h3>SPFA<\/h3>\n<p>Shortest Path Faster Algorithm\uff0c\u662f\u4e00\u4e2a\u7528\u4e8e\u6c42\u6709\u5411\u5e26\u6743\u56fe\u5355\u6e90\u6700\u77ed\u8def\u5f84\u7684\u7b97\u6cd5\uff0c\u4e14\u9002\u7528\u4e8e\u5e26\u8d1f\u6743\u7684\u56fe\uff0c\u80fd\u591f\u5224\u65ad\u8d1f\u73af\u3002<\/p>\n<p>SPFA\u7684\u529f\u80fd\u8fd8\u662f\u5f88\u5f3a\u5927\u7684\uff0c\u5373\u4f7f\u6709\u5411\u56fe\u5b58\u5728\u91cd\u8fb9\uff0c\u73af\uff0cSPFA\u90fd\u53ef\u4ee5\u6b63\u786e\u7684\u5904\u7406\uff0c\u4f46\u662f\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u53d7\u5f71\u54cd\u3002<\/p>\n<p>\u8fd9\u91cc\u7684\u4ee3\u7801\u662f\u7528\u961f\u5217\u5b9e\u73b0\u7684\uff0c\u961f\u5217\u91cc\u7684\u7ed3\u70b9\uff0c\u662f\u5f85\u4f18\u5316\u7684\u7ed3\u70b9\u3002\u5373\u8be5\u7ed3\u70b9\u6307\u5411\u7684\u6240\u6709\u7684\u7ed3\u70b9\u7684\u6700\u77ed\u8def\u5f84\u6709\u4f18\u5316\u7684\u53ef\u80fd\u3002<br \/>\n\u56fe\u7528vector\u5b58\u3002vis[i]\u8868\u793ai\u7ed3\u70b9\u662f\u5426\u5728\u961f\u5217\u4e2d\u3002dist[i]\u8868\u793a\u6e90\u70b9s\u5230\u7ed3\u70b9i\u7684\u6700\u77ed\u8def\u5f84\u3002<br \/>\n\u57fa\u4e8e\u6b64\uff0c\u7b97\u6cd5\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<br \/>\n<strong>1.\u521d\u59cb\u5316dist\u4e3ainf\uff0cvis\u4e3a0.<br \/>\n1.\u5148\u628a\u6e90\u70b9s\u5165\u961f\uff0c\u5e76\u4e14\u505a\u5165\u961f\u6807\u8bb0\u5904\u7406\uff0cdist[s]\u66f4\u65b0\u4e3a0.<br \/>\n3.\u4ece\u961f\u5217\u4e2d\u62ff\u51fa\u4e00\u4e2a\u7ed3\u70b9u\uff0c\u5bf9\u4e8e\u7ed3\u70b9u\u6307\u5411\u7684\u6240\u6709\u7ed3\u70b9v\uff0c\u5224\u65ad\u662f\u5426\u6709\uff1adist[v] &gt; dist[u] + w(u-&gt;v). \u82e5\u6709\uff0c\u5219\u8bf4\u660ev\u7ed3\u70b9\u7684\u6700\u77ed\u8defdist[v]\u9700\u8981\u4f18\u5316\uff0c\u540c\u65f6v\u6307\u5411\u7684\u7ed3\u70b9\u4e5f\u6709\u4f18\u5316\u7684\u53ef\u80fd\uff0c\u56e0\u6b64\u66f4\u65b0\u5b8cv\u7684dist\u503c\u540e\uff0c\u628av\u5165\u961f\uff1b\u82e5\u6ca1\u6709\uff0c\u5373dist[v]\u4e0d\u9700\u8981\u4f18\u5316\uff0c\u4e0d\u9700\u8981\u66f4\u65b0\uff0c\u90a3\u5c31\u4e0d\u9700\u8981\u5904\u7406\u4e86\u3002<br \/>\n4.\u91cd\u590d\u6b65\u9aa43\uff0c\u76f4\u81f3\u961f\u5217\u4e3a\u7a7a\u3002\u8f93\u51fadist[].<\/strong><\/p>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6\u4e0a\u9650O(nm)<\/strong>\uff0c\u70b9\u6570n\uff0c\u8fb9\u6570m<\/p>\n<p><a href=\"https:\/\/www.luogu.com.cn\/problem\/P3371\">\u6d1b\u8c37P3371 \u5355\u6e90\u6700\u77ed\u8def\u5f84(\u5f31\u5316\u7248)<\/a><\/p>\n<pre><code class=\"language-c++\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;cmath&gt;\n#include &lt;queue&gt;\nusing namespace std;\nconst int maxn = 1e4+10,inf = 2147483647;\ntypedef struct Edge{\n    int v,w;\n}Edge;\nint n,m,s;\nEdge tmp;\nint dist[maxn],vis[maxn];\nvector&lt;Edge&gt; G[maxn];\nint main(){\n    int u,v,w;\n    queue&lt;int&gt;  q;\n    cin &gt;&gt; n &gt;&gt; m &gt;&gt; s;\n    for (int i=1;i&lt;=m;i++){\n        cin &gt;&gt; u &gt;&gt; tmp.v &gt;&gt; tmp.w;\n        G[u].push_back(tmp);\n    }\n    for (int i=1;i&lt;=n;i++){\n        dist[i] = inf;\n        vis[i] = 0;\n    }\n    q.push(s);  dist[s] = 0;    vis[s] = 1;\n    while (!q.empty()){\n        int tmp = q.front();    q.pop();\n        vis[tmp] = 0;\n        for (int i=0;i&lt;G[tmp].size();i++){\n            if (dist[G[tmp][i].v] &gt; dist[tmp]+G[tmp][i].w){\n                dist[G[tmp][i].v] = dist[tmp]+G[tmp][i].w;\n                if (!vis[G[tmp][i].v]){\n                    q.push(G[tmp][i].v);\n                    vis[G[tmp][i].v] = 1;\n                }\n            }\n        }\n    }\n    for (int i=1;i&lt;=n;i++)\n        cout &lt;&lt; dist[i] &lt;&lt; &quot; &quot;;\n    return 0;\n}<\/code><\/pre>\n<p>\u94fe\u5f0f\u524d\u5411\u661f\u7248\u672c<\/p>\n<pre><code class=\"language-c++\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\nusing namespace std;\nconst int maxn = 1e4+10, maxm = 5e5+5, inf = 2147483647;\ntypedef struct Edge{\n    int from,to,weight;\n    int next;\n    Edge(){\n        next = inf;\n    }\n}Edge;\nint n,m,s;\nint dist[maxn],vis[maxn];\nint head[maxn];\nint tot;\nEdge e[maxm];\nvoid AddEdge(int from,int to,int weight){\n    tot++;\n    e[tot].from = from;\n    e[tot].to = to;\n    e[tot].weight = weight;\n    e[tot].next = head[from];\n    head[from] = tot;\n}\nint main(){\n    int from,to,weight;\n    queue&lt;int&gt;  q;\n    cin &gt;&gt; n &gt;&gt; m &gt;&gt; s;\n    for (int i=1;i&lt;=m;i++){\n        cin &gt;&gt; from &gt;&gt; to &gt;&gt; weight;\n        AddEdge(from,to,weight);\n    }\n    for (int i=1;i&lt;=n;i++){\n        dist[i] = inf;\n        vis[i] = 0;\n    }\n    q.push(s);  dist[s] = 0;    vis[s] = 1;\n    while (!q.empty()){\n        int tmp = q.front();    q.pop();\n        vis[tmp] = 0;\n        for (int i=head[tmp];i!=inf;i=e[i].next){\n            if (dist[e[i].to] &gt; dist[tmp]+e[i].weight){\n                dist[e[i].to] = dist[tmp]+e[i].weight;\n                if (!vis[e[i].to]){\n                    q.push(e[i].to);\n                    vis[e[i].to] = 1;\n                }\n            }\n        }\n    }\n    for (int i=1;i&lt;=n;i++)\n        cout &lt;&lt; dist[i] &lt;&lt; &quot; &quot;;\n    return 0;\n}<\/code><\/pre>\n<p>\u73b0\u5728\u666e\u904d\u8ba4\u4e3aSPFA\u5df2\u7ecf\u6b7b\u4e86\uff0c\u4f46\u4e5f\u6709\u5f88\u591a\u7528\u5230\u7684\u5730\u65b9\u3002<br \/>\n\u8bf4SPFA\u6b7b\u4e86\uff0c\u662f\u56e0\u4e3aSPFA\u662f\u65f6\u95f4\u590d\u6742\u5ea6\u5f88\u4e0d\u7a33\u5b9a\u7684\u7b97\u6cd5\uff0c\u5b83\u91c7\u53d6\u7684\u57fa\u672c\u601d\u60f3\u5c31\u662f\uff1a\u6e90\u70b9s\u5230\u7ed3\u70b9i\u7684\u6700\u77ed\u8def=min(s\u5230\u6307\u5411i\u7684\u6240\u6709\u7ed3\u70b9j\u7684\u6700\u77ed\u8def+w(j-&gt;i))<br \/>\n\u53ef\u4ee5\u60f3\u5230\u8fd9\u6837\u6c42\u7684\u8bdd\u5f88\u5bb9\u6613\u88ab\u5361\u7684\uff0c\uff0c\u4e0d\u540c\u7684\u6570\u636e\u5373\u4f7f\u6570\u636e\u91cf\u76f8\u540c\uff0c\u8017\u65f6\u4e5f\u53ef\u80fd\u5929\u5dee\u5730\u522b\uff01<br \/>\n\u6240\u4ee5SPFA\u591a\u7528\u4e8e\u6c42\u8d1f\u6743\uff0c\u5224\u65ad\u8d1f\u73af\u3002\u4e00\u822c\u7684\u6700\u77ed\u8def\u591a\u7528\u4f18\u5148\u6743\u961f\u5217\u4f18\u5316\u7684Dijkstra\u7b97\u6cd5\u6765\u6c42\u3002<\/p>\n<p>SPFA\u5728\u65af\u5766\u7eb3\u6811\u4e2d\u4e5f\u4f1a\u7528\u5230(\u56e0\u65af\u5766\u7eb3\u6811\u7684\u6570\u636e\u91cf\u5f88\u5c0f\uff0c\u7528SPFA\u662f\u5f88\u5408\u9002\u7684)\u3002<\/p>\n<h3>SPFA\u6c42\u8d1f\u73af<\/h3>\n<p>SPFA\u53ef\u4ee5\u5904\u7406Dijkstra\u5904\u7406\u4e0d\u4e86\u7684\u8d1f\u6743\u56fe\u3002<br \/>\n\u8d1f\u73af\uff1a\u4e00\u6761\u6743\u503c\u4e3a\u8d1f\u6570\u7684\u56de\u8def\u3002<br \/>\n\u90a3\u4e48\u5f53\u4e00\u4e2a\u8fde\u901a\u56fe\u5b58\u5728\u8d1f\u73af\u65f6\uff0c\u8fd9\u4e2a\u73af\u4e0a\u7684\u70b9\u7684\u6700\u77ed\u8def\u662f\u65e0\u6cd5\u6c42\u51fa\u6765\u7684\uff0c\u56e0\u4e3a\u7ed5\u7740\u73af\u8d70\u4e00\u5708\uff0c\u6743\u503c\u4f1a\u53d8\u5c0f\uff0c\u90a3\u4e48\u6700\u5c0f\u7684\u8bdd\u5c31\u4e00\u76f4\u8d70\u4e00\u76f4\u8d70\u6ca1\u6709\u7a77\u5c3d\u4e86\u3002<br \/>\n\u5982\u4f55\u5224\u65ad\u8d1f\u73af\u5462\uff0cSPFA\u7b97\u6cd5\u4e2d\uff0c\u82e5\u67d0\u4e2a\u7ed3\u70b9\u88ab\u66f4\u65b0\u4e86\u5f88\u591a\u5f88\u591a\u6b21\uff0c\u5c31\u53ef\u4ee5\u65ad\u5b9a\u5b83\u4e00\u5b9a\u5904\u5728\u67d0\u4e2a\u8d1f\u73af\u5185\u3002\u8fd9\u4e2a\u6b21\u6570\u7684\u4e0b\u754c\u662fn(\u70b9\u6570)\u3002<br \/>\n\u90a3\u4e48\u53ea\u9700\u8981\u5728SPFA\u7684\u677f\u5b50\u4e0a\u5c0f\u505a\u4fee\u6539\u5373\u53ef\u3002<br \/>\n<strong>\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u786e\u5b9a\u7684O(nm)<\/strong><br \/>\n<a href=\"https:\/\/www.luogu.com.cn\/problem\/P3385\">\u6d1b\u8c37P3385<\/a><\/p>\n<pre><code class=\"language-c++\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\n#include &lt;cstring&gt;\nusing namespace std;\nconst int maxn = 2e3+10, maxm = 1e4+5, inf = 2147483647;\ntypedef struct Edge{\n    int from,to,weight;\n    int next;\n    Edge(){\n        next = inf;\n    }\n}Edge;\nint n,m;\nint dist[maxn],vis[maxn],cnt[maxn];\nint head[maxn];\nint tot;\nEdge e[maxm];\ninline void AddEdge(int from,int to,int weight){\n    tot++;\n    e[tot].from = from;\n    e[tot].to = to;\n    e[tot].weight = weight;\n    e[tot].next = head[from];\n    head[from] = tot;\n}\nbool SPFA();\nint main(){\n    int t;\n    cin &gt;&gt; t;\n    while (t--){\n        \/\/memset(head,inf,sizeof head);\n        \/\/memset(dist,inf,sizeof dist);\n        for (int i=1;i&lt;maxn;i++)\n            head[i] = dist[i] = inf;\n        memset(cnt,0,sizeof cnt);\n        if (SPFA()) cout &lt;&lt; &quot;YES\\n&quot;;\n        else    cout &lt;&lt; &quot;NO\\n&quot;;\n    }\n    return 0;\n}\nbool SPFA(){\n    int from,to,weight;\n    tot = 0;\n    queue&lt;int&gt;  q;\n    cin &gt;&gt; n &gt;&gt; m;\n    for (int i=1;i&lt;=m;i++){\n        cin &gt;&gt; from &gt;&gt; to &gt;&gt; weight;\n        if (weight&gt;=0){\n            AddEdge(from,to,weight);\n            AddEdge(to,from,weight);\n        }else\n            AddEdge(from,to,weight);\n    }\n    for (int i=1;i&lt;=n;i++){\n        dist[i] = inf;\n        vis[i] = 0;\n    }\n    q.push(1);  dist[1] = 0;    vis[1] = 1;\n    while (!q.empty()){\n        int tmp = q.front();    q.pop();\n        vis[tmp] = 0;\n        for (int i=head[tmp];i!=inf;i=e[i].next){\n            if (dist[e[i].to] &gt; dist[tmp]+e[i].weight){\n                dist[e[i].to] = dist[tmp]+e[i].weight;\n                if (++cnt[e[i].to] &gt;= n)\n                    return 1;\n                if (!vis[e[i].to]){\n                    q.push(e[i].to);\n                    vis[e[i].to] = 1;\n                }\n            }\n        }\n    }\n    return 0;\n}<\/code><\/pre>\n<p>\u5c0f\u9519\u8bef\uff1a\u7528memset\u8bbe\u7f6e\u503cinf\u65f6\u51fa\u9519\u4e86\uff0c\uff0c\uff0cRE<\/p>\n<h4>\u6807\u8bb0\u6700\u77ed\u8def\u5f84<\/h4>\n<p><a href=\"https:\/\/www.luogu.com.cn\/problem\/P2384\">\u6d1b\u8c37P2384 \u6700\u77ed\u8def<\/a><br \/>\n\u5982\u4f55\u8f93\u51fa\u6700\u77ed\u8def\u4e0a\u7684\u8fb9\u5462\uff1f\uff1f\u8fd9\u9053\u9898\u53ef\u4ee5\u7528\u5230\uff01<br \/>\n\u9898\u610f\u770b\u9898\uff0c\u56e0\u4e3a\u4e0d\u65ad\u4e58\u7684\u8bdd\u80af\u5b9a\u4f1a\u7206\u7cbe\u5ea6\uff0c\u90a3\u4e48\u7528log\u64cd\u4f5c\u4e00\u4e0b\u5c31\u884c\u4e86\u3002\u3002\u4f46\u662f\u8bd5\u4e86\u5f88\u591a\u6b21\uff0c\u4ecd\u7136\u4f1a\u6709\u7cbe\u5ea6\u7684\u95ee\u9898\uff0c\u53ef\u80fd\u662flog\u76f8\u52a0\u7684\u65f6\u5019\u7cbe\u5ea6\u4e0d\u591f\u5427\uff0c\uff0c\u6700\u540e\u7528\u4e86\u6b63\u89e3\uff1a\u6807\u8bb0\u6700\u77ed\u8def\u5f84\u624d\u5f7b\u5e95\u89e3\u51b3\u4e86\u95ee\u9898\u3002<br \/>\n\u6807\u8bb0\u7684\u65b9\u6cd5\u5c31\u662f\uff0c\u4ece1--&gt;n\u7684\u6700\u77ed\u8def\u5f84\u4e2d\uff0c\u6807\u8bb0\u76f4\u63a5\u5230\u8fben\u7684\u90a3\u6761\u8fb9\uff0c\u7136\u540e\u56de\u6eaf\u7ec8\u70b9\u5373\u53ef\u3002<br \/>\ntrans[i]\u8868\u793a1-&gt;i\u7684\u6700\u77ed\u8def\u4e2d\uff0c\u6700\u540e\u4e00\u6761\u8fb9(\u5373\u76f4\u63a5\u6307\u5411i\u7684\u90a3\u6761\u8fb9)\u7684\u6807\u53f7\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;cmath&gt;\n#include &lt;queue&gt;\nusing namespace std;\nconst int maxn = 1e3+5, maxm = 1e6+5, inf = 4153151, mod = 9987;\ntypedef struct Edge{\n    int from,to,weight,next;\n}Edge;\nint n, m, tot;\nint head[maxn], vis[maxn], trans[maxn];\ndouble dist[maxn];\nEdge e[maxm];\nchar s[10];\ninline void AddEdge(int from, int to, int weight){\n    tot++;\n    e[tot].from = from;\n    e[tot].to = to;\n    e[tot].weight = weight;\n    e[tot].next = head[from];\n    head[from] = tot;\n    return;\n}\nint main(){\n    int u, v, w;\n    queue&lt;int&gt;  q;\n    scanf (&quot;%d %d&quot;,&amp;n, &amp;m);\n    for (int i=1;i&lt;=n;i++)  head[i] = dist[i] = inf;\n    for (int i=1;i&lt;=m;i++){\n        scanf (&quot;%d %d %d&quot;,&amp;u, &amp;v, &amp;w);\n        AddEdge(u, v, w);\n    }\n    q.push(1);  vis[1] = 1;     dist[1] = 0;\n    while (!q.empty()){\n        int tmp = q.front();    q.pop();\n        vis[tmp] = 0;\n        for (int i=head[tmp];i!=inf;i=e[i].next)\n            if (dist[e[i].to] &gt; dist[tmp] + log2(e[i].weight)){\n                dist[e[i].to] = dist[tmp] + log2(e[i].weight);\n                trans[e[i].to] = i;\n                if (!vis[e[i].to]){\n                    q.push(e[i].to);\n                    vis[e[i].to] = 1;\n                }\n            }\n    }\n    int tmp = n, ans = 1;\n    while (tmp!=1){\n        ans = (ans * e[trans[tmp]].weight) % mod;\n        tmp = e[trans[tmp]].from;\n    }\n    printf (&quot;%d&quot;, ans);\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SPFA Shortest Path Faster Algorithm\uff0c\u662f\u4e00\u4e2a\u7528\u4e8e\u6c42\u6709\u5411\u5e26\u6743\u56fe\u5355\u6e90\u6700 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[43],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/435"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=435"}],"version-history":[{"count":22,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/435\/revisions"}],"predecessor-version":[{"id":533,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/435\/revisions\/533"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}