{"id":779,"date":"2020-08-14T21:51:11","date_gmt":"2020-08-14T13:51:11","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=779"},"modified":"2020-08-15T13:18:47","modified_gmt":"2020-08-15T05:18:47","slug":"%e6%9c%80%e5%a4%a7%e6%b5%81","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/08\/14\/%e6%9c%80%e5%a4%a7%e6%b5%81\/","title":{"rendered":"\u6700\u5927\u6d41"},"content":{"rendered":"<h3>\u7f51\u7edc\u6d41<\/h3>\n<p>\u7ed9\u4e00\u4e2a\u6709\u5411\u56fe\uff0c\u6bcf\u6761\u8fb9\u6709\u4e00\u4e2a\u6743\u503c\uff1a\u6d41\u91cf\uff0c\u5373\u4eceu\u5230v\u7684\u6d41\u91cf\u4e0d\u80fd\u8d85\u8fc7\u8fd9\u4e2a\u503c\uff0c\u7136\u540e\u7ed9\u4e24\u4e2a\u70b9\uff1a\u6e90\u70b9\u548c\u6c47\u70b9\uff0c\u6c42\u4ece\u6e90\u70b9\u5230\u6c47\u70b9\u7684\u6700\u5927\u603b\u6d41\u91cf\u3002<\/p>\n<p>\u89e3\u51b3\u6700\u5927\u6d41\u95ee\u9898\u7684\u7b97\u6cd5\u6709(\u65f6\u95f4\u590d\u6742\u5ea6\u4ece\u52a3\u5230\u4f18)\uff1aFF\u7b97\u6cd5\uff0cEK\u7b97\u6cd5\uff0cDINIC\u7b97\u6cd5\uff0cHLPP\u7b97\u6cd5..<\/p>\n<p>\u89c2\u5bdf\u4e86\u51e0\u4e2a\u7b97\u6cd5\uff0c\u6bcf\u4e2a\u7b97\u6cd5\u90fd\u6709\u4e00\u4e2a\u6b65\u9aa4\uff0c\u627e\u5230\u4e00\u6761\u8def\u4e4b\u540e\uff0c\u628a\u5b83\u4eec\u7684\u6d41\u91cf\u53cd\u5411\uff0c\u6bd4\u5982u\u5230v\u6709x\u7684\u6d41\u91cf\uff0c\u90a3\u4e48\u4ecev\u5230u\u5c31\u6709-x\u7684\u6d41\u91cf\uff0c\u5bf9\u4e8e\u975e\u6e90\u70b9\u548c\u975e\u6c47\u70b9\u7684\u6bcf\u4e2a\u70b9\uff0c\u5b83\u4eec\u81ea\u8eab\u7684\u6d41\u91cf\u548c\u4e3a0\uff0c\u6ee1\u8db3\u80fd\u91cf\u5b88\u6052\u5b9a\u7406\u3002<\/p>\n<h4>EK\u7b97\u6cd5<\/h4>\n<p><code class=\"katex-inline\">\u65f6\u95f4\u590d\u6742\u5ea6O(V^2E)<\/code><br \/>\nBFS\u505a\u6cd5\uff0c\u6bcf\u6b21\u4f7f\u7528BFS\u627e\u5230\u4e00\u6761\u8def\uff0c\u76f4\u5230\u627e\u4e0d\u5230\u4e3a\u6b62\u3002<\/p>\n<p>c\u7528\u6765\u5b58\u56fe\uff0c\u8868\u793a\u6bcf\u6761\u8fb9\u7684\u6700\u5927\u6d41\u91cf\uff0cf\u8868\u793a\u5df2\u7ecf\u6d41\u8fc7\u7684\u6d41\u91cf\uff0c\u4e8c\u8005\u76f8\u51cf\u4e3a\u5269\u4f59\u6d41\u91cf\uff0c\u5269\u4f59\u6d41\u91cf\u5927\u4e8e0\u8868\u793a\u8fd9\u6761\u8fb9\u8fd8\u53ef\u4ee5\u901a\u6d41\u3002vis[i]\u8868\u793a\u6e90\u70b9\u5230i\u7684\u6700\u5927\u6d41\u91cf\u3002<br \/>\n<a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=3549\">HDU3549 Flow Problem<\/a><\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;cstring&gt;\n#include &lt;queue&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nint n, m, x, y, z;\nint c[16][16], f[16][16], vis[16], pre[16], sum;\nvoid EK(){\n    queue&lt;int&gt; q;\n    while (1){\n        memset (vis, 0, sizeof vis);\n        memset (pre, 0, sizeof pre);\n        q.push(1);  vis[1] = 9999999;\n        while (!q.empty()){\n            int x = q.front();  q.pop();\n            for (int i=1; i&lt;=n; i++)    if (!vis[i] &amp;&amp; f[x][i]&lt;c[x][i]){\n                vis[i] = min(vis[x], c[x][i]-f[x][i]);\n                pre[i] = x;\n                q.push(i);\n            }\n        }\n        if (!vis[n])    break;\n        int tmp = n;\n        sum += vis[n];\n        while (pre[tmp]){\n            f[pre[tmp]][tmp] += vis[n];\n            f[tmp][pre[tmp]] -= vis[n];\n            tmp = pre[tmp];\n        }\n    }\n}\nint main(){\n    int cas = 0;\n    scanf (&quot;%d&quot;, &amp;n);\n    while (~scanf (&quot;%d %d&quot;, &amp;n, &amp;m)){\n        memset (c, 0, sizeof c);\n        memset (f, 0, sizeof f);\n        sum = 0;\n        for (int i=1; i&lt;=m; i++){\n            scanf (&quot;%d %d %d&quot;, &amp;x, &amp;y, &amp;z);\n            c[x][y] += z;\n        }\n        EK();\n        printf (&quot;Case %d: %d\\n&quot;, ++cas, sum);\n    }\n    return 0;\n}<\/code><\/pre>\n<h4>DINIC\u7b97\u6cd5<\/h4>\n<p><code class=\"katex-inline\">\u65f6\u95f4\u590d\u6742\u5ea6O(VE)<\/code><br \/>\n\u5728EK\u7b97\u6cd5\u4e2d\uff0c\u6bcf\u5bfb\u627e\u4e00\u6761\u8def\u5c31\u8981BFS\u4e00\u6b21\uff0c\u662f\u5728\u592a\u6162\u4e86\uff0cDINIC\u7b97\u6cd5\u662f\u5bf9EK\u7b97\u6cd5\u7684\u4e00\u4e2a\u5927\u4f18\u5316\u3002<br \/>\nBFS+DFS\u6765\u89e3\u51b3\u95ee\u9898\u3002<br \/>\n\u6bcf\u6b21\u5bfb\u627e\u8def\u524d\uff0c\u5148\u7528BFS\u8dd1\u4e00\u904d\uff0c\u5224\u65ad\u662f\u5426\u8fd8\u5b58\u5728\u8def\u5230\u8fbe\u6c47\u70b9\uff0c\u5e76\u7ed9\u6bcf\u4e2a\u70b9\u6807\u5c42\uff0c\u7136\u540eDFS\u5bfb\u627e\u6d41\u8def\u3002DFS\u51fd\u6570\u6709\u4e09\u4e2a\u53c2\u6570\uff1a\u6e90\u70b9s\uff0c\u6c47\u70b9t\uff0c\u53ef\u4ee5\u5206\u53d1\u7684\u6d41\u91cfflow\uff0c\u5373\u628a\u8fd9flow\u7684\u6d41\u91cf\u5206\u7ed9s\u6307\u5411\u7684\u51e0\u4e2a\u70b9\uff0c\u6700\u540e\u5269\u4e0brest\u6d41\u91cf\uff0c\u90a3\u4e48\u5c31\u6709flow-rest\u7684\u6d41\u91cf\u6d41\u5230\u4e86\u6c47\u70b9\u3002<\/p>\n<p>\u5b58\u56fe\u662f\u7528vector\u5b58\uff0c\u5b58\u8fb9\u7528\u6570\u7ec4\uff0c\u5177\u4f53\u89c1\u4ee3\u7801\uff0c\u6bcf\u6761\u8fb9\u90fd\u6709\u5bf9\u5e94\u7684\u53cd\u5411\uff0c\u7b2ci\u6761\u8fb9\u7684\u53cd\u5411\u662f\u7b2ci^1\u6761\u8fb9\uff0c\u8fb9\u7684\u4e0b\u6807\u5fc5\u987b\u4ece0\u5f00\u59cb(\u56e0\u4e3a0\u548c1\u5bf9\u5e94\uff0c\u5728^\u8fd0\u7b97\u7b26\u4e0b)\u3002<br \/>\n\u8fd8\u8981\u6ce8\u610f\u7684\u662f\u8fd9\u91cc\u9762\u662f\u6709\u591a\u91cd\u8fb9\u7684\uff0c\u800c\u4e14\u6bcf\u6761\u8fb9\u90fd\u4f1a\u5efa\u7acb\u53cd\u5411\uff0cEK\u7b97\u6cd5\u91cc\u7684\u90bb\u63a5\u77e9\u9635\u662f\u628a\u591a\u91cd\u8fb9\u5408\u5e76\uff0c\u56e0\u4e3a\u662f\u7528\u77e9\u9635\u5b58\u7684\u56fe\uff0c\u90a3\u6837\u7684\u8bdd\u5bf9\u4e8e\u70b9\u6570\u8f83\u5c0f\u7684\u56fe\u5c31\u5f88\u5feb\u7684\uff0c\u4f46\u5f53\u70b9\u6570\u8f83\u591a\u65f6\uff0c\u8fb9\u6570\u5c31\u76f8\u5bf9\u6765\u8bf4\u5c11\u5f88\u591a\u4e86(\u76f8\u5bf9V^2\uff0c\u5b8c\u5168\u56fe)\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;cstring&gt;\n#include &lt;queue&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nint n, m, x, y, z, cnt;\nstruct edge{\n    int from, to, c, f;\n    edge(int u, int v, int cap, int flow): from(u), to(v), c(cap), f(flow){}\n};\nint depth[30], cur[30];\nvector&lt;edge&gt; e;\nvector&lt;int&gt; G[30];\ninline bool BFS(int s, int t){\n    memset (depth, 0, sizeof depth);\n    queue&lt;int&gt; q;\n    q.push(s);  depth[s] = 1;\n    while (!q.empty()){\n        s = q.front();  q.pop();\n        for (int i=0; i&lt;G[s].size(); i++){\n            edge &amp;tmp = e[G[s][i]];\n            if (tmp.c&gt;tmp.f &amp;&amp; !depth[tmp.to]){           \/\/\u53ea\u8003\u8651\u6709\u5269\u4f59\u6d41\u91cf\u7684\u8fb9\n                depth[tmp.to] = depth[s] + 1;\n                q.push(tmp.to);\n            }\n        }\n    }\n    return depth[t];\n}\ninline int DFS(int s, int flow, int t){\n    if (s==t || flow&lt;=0)    return flow;\n    int rest = flow;\n    for (int &amp;i=cur[s]; i&lt;G[s].size(); i++)   if (e[G[s][i]].c&gt;e[G[s][i]].f &amp;&amp; depth[e[G[s][i]].to]==depth[s]+1){           \/\/\u679a\u4e3es\u6307\u5411\u7684\u6bcf\u4e2a\u70b9\uff0c\u82e5\u8fde\u63a5\u4ed6\u4fe9\u7684\u8fb9\u8fd8\u6709\u5269\u4f59\u6d41\u91cf\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u8003\u8651        \u5f27\u4f18\u5316\n        int tmp = DFS(e[G[s][i]].to, min(rest, e[G[s][i]].c-e[G[s][i]].f), t);         \/\/tmp\u6682\u5b58\u53ef\u4ee5\u4ece\u8fd9\u6761\u8fb9\u6d41\u51fa\u53bb\u7684\u6d41\u91cf\n        if (tmp&lt;=0){                \/\/\u526a\u679d\u4f18\u5316\n            depth[e[G[s][i]].to] = 0;\n            continue;\n        }\n        rest -= tmp;              \/\/\u53ef\u4ee5\u6d41\u51fatmp\uff0c\u90a3\u4e48rest\u5c31\u51cf\u53bbtmp\uff0c\u5bf9\u5e94\u7684\u8fb9\u548c\u53cd\u5411\u8fb9\u5c31\u5206\u522b\u52a0\u51cftmp\n        e[G[s][i]].f += tmp;\n        e[G[s][i]^1].f -= tmp;\n        if (rest==0)    break;      \/\/\u5f88\u91cd\u8981\u7684\u4e00\u6b65\u3002\u3002\u3002\n    }\n    return flow - rest;\n}\nint DINIC(int s, int t){\n    int ans = 0;\n    while (BFS(s, t)){\n        ans += DFS(s, 999999999, t);\n        for (int i=1; i&lt;=n; i++) cur[i] = 0;\n    }\n    return ans;\n}\ninline void add(int from, int to, int cap){\n    e.push_back(edge(from, to, cap, 0));\n    e.push_back(edge(to, from, 0, 0));\n    cnt += 2;\n    G[from].push_back(cnt-2);\n    G[to].push_back(cnt-1);\n    return;\n}\nint main(){\n    scanf (&quot;%d&quot;, &amp;x);\n    int cas = 0;\n    while (~scanf (&quot;%d %d&quot;, &amp;n, &amp;m)){\n        e.clear();\n        for (int i=1; i&lt;=n; i++)    G[i].clear();\n        cnt = 0;\n        for (int i=1; i&lt;=m; i++){\n            scanf (&quot;%d %d %d&quot;, &amp;x, &amp;y, &amp;z);\n            add(x, y, z);\n        }\n        printf (&quot;Case %d: %d\\n&quot;, ++cas, DINIC(1, n));\n    }\n    return 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u7edc\u6d41 \u7ed9\u4e00\u4e2a\u6709\u5411\u56fe\uff0c\u6bcf\u6761\u8fb9\u6709\u4e00\u4e2a\u6743\u503c\uff1a\u6d41\u91cf\uff0c\u5373\u4eceu\u5230v\u7684\u6d41\u91cf\u4e0d\u80fd\u8d85\u8fc7\u8fd9\u4e2a\u503c\uff0c\u7136\u540e\u7ed9\u4e24\u4e2a\u70b9\uff1a\u6e90\u70b9\u548c\u6c47 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[59],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/779"}],"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=779"}],"version-history":[{"count":13,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/779\/revisions"}],"predecessor-version":[{"id":782,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/779\/revisions\/782"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=779"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}