{"id":7,"date":"2011-10-30T09:26:48","date_gmt":"2011-10-30T01:26:48","guid":{"rendered":"http:\/\/rexdf.org\/?p=7"},"modified":"2011-10-30T09:26:48","modified_gmt":"2011-10-30T01:26:48","slug":"%e6%9c%80%e5%a4%a7%e6%b5%81-dinic%e7%ae%97%e6%b3%95%e5%92%8csap%e7%ae%97%e6%b3%95%e7%ae%80%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/blog.rexdf.org\/en\/2011\/10\/%e6%9c%80%e5%a4%a7%e6%b5%81-dinic%e7%ae%97%e6%b3%95%e5%92%8csap%e7%ae%97%e6%b3%95%e7%ae%80%e4%bb%8b\/","title":{"rendered":"\u6700\u5927\u6d41-Dinic\u7b97\u6cd5\u548cSAP\u7b97\u6cd5\u7b80\u4ecb"},"content":{"rendered":"<p><a href=\"http:\/\/rexdf.org\/wp-content\/uploads\/2011\/10\/1280wallpaperQ.jpg\"><img decoding=\"async\" src=\"https:\/\/static.rexdf.org\/plugins\/images-lazyload-and-slideshow\/blank_250x250.gif\" file=\"http:\/\/rexdf.org\/wp-content\/uploads\/2011\/10\/1280wallpaperQ.jpg\" alt=\"\" title=\"1280wallpaperQ\" class=\"aligncenter size-full wp-image-12 ls_lazyimg\" \/><noscript><img decoding=\"async\" src=\"http:\/\/rexdf.org\/wp-content\/uploads\/2011\/10\/1280wallpaperQ.jpg\" alt=\"\" title=\"1280wallpaperQ\" class=\"aligncenter size-full wp-image-12\" \/><\/noscript><\/a>\u6c42\u89e3\u6700\u5927\u6d41\u4e00\u822c\u91c7\u7528\u4e24\u79cd\u601d\u8def\uff0c\u4e00\u79cd\u662f\u9884\u6d41\uff0c\u53e6\u4e00\u5404\u662f\u589e\u5e7f\u8def\u3002\u589e\u5e7f\u8def\u8fd9\u79cd\u601d\u60f3\u662f\u57fa\u4e8e\u4ee5\u4e0b\u5b9a\u7406\uff1a<\/p>\n<p>\u5b9a\u7406\u4e00\uff1a\u8bbe\u7f51\u7edc G \u7684\u6e90\u4e3a S, \u6c47\u548c T\uff0cF<br \/>\n\u548c C \u5206\u522b\u4e3a G \u7684\u6d41\u548c\u5bb9\u91cf\uff0c\u5219 F \u662f\u6700\u5927\u6d41\u5f53\u4e14\u4ec5\u5f53 G \u4e2d\u4e0d\u5b58\u5728\u53ef\u589e\u5e7f\u8def\u3002<\/p>\n<p>\u7531\u6b64\u5b9a\u7406\u53ef\u4ee5\u8bbe\u8ba1\u5f88\u591a\u7b97\u6cd5\u6765\u8ba1\u7b97\u6700\u5927\u6d41\uff0cDinic \u7b97\u6cd5\u662f\u5176\u4e2d\u4e00\u79cd\u6bd4\u8f83\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u5176\u590d\u6742\u5ea6\u4e3a O(n2*m)<\/p>\n<p>Dinic \u7b97\u6cd5\u7684\u57fa\u672c\u6b65\u9aa4\u4e3a:<\/p>\n<p>1) \u8ba1\u7b97\u6b8b\u4f59\u7f51\u7edc\u7684\u5c42\u6b21\u56fe\u3002\u6211\u4eec\u5b9a\u4e49 h[i] \u4e3a\u9876\u70b9 i \u8ddd\u79bb\u6e90 S \u6240\u7ecf\u8fc7\u5230\u6700\u5c0f\u8fb9\u6570\uff0c\u6c42\u51fa\u6240\u6709\u9876\u70b9\u7684 h \u503c\uff0ch[] \u503c\u76f8\u540c\u7684\u9876\u70b9\u5c5e\u4e8e\u540c\u4e00\u5c42\uff0c\u8fd9\u5c31\u662f\u7f51\u7edc\u7684\u5c42\u6b21\u56fe\u3002<\/p>\n<p>2) \u5728\u5c42\u6b21\u56fe\u4e0a\u8fdb\u884c BFS \u589e\u5e7f\uff0c\u76f4\u5230\u4e0d\u5b58\u5728\u589e\u5e7f\u8def\u5f84\u3002\u8fd9\u65f6\u6c42\u5f97\u7684\u589e\u5e7f\u8def\u5f84\u4e0a\u9876\u70b9\u662f\u5206\u5c42\u7684\uff0c\u8def\u5f84\u4e0a\u4e0d\u53ef\u80fd\u5b58\u5728\u4e24\u4e2a\u9876\u70b9\u5c5e\u4e8e\u540c\u4e00\u5c42\uff0c\u5373 h[i]== h[j] (i!= j )\u3002\u540c\u65f6\uff0c\u6c42\u5f97\u5c42\u6b21\u56fe\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5c42\u6b21\u56fe\u4e0a\u8fdb\u884c\u591a\u6b21\u589e\u5e7f\u3002<\/p>\n<p>3) \u91cd\u590d 1 \u548c<br \/>\n2\u3002\u76f4\u5230\u4e0d\u5b58\u5728\u589e\u5e7f\u8def\u5f84\u3002<\/p>\n<p>\u53ef\u77e5\uff0cDinic \u7b97\u6cd5\u627e\u5230\u7684\u589e\u5e7f\u8def\u5f84\u662f\u6700\u77ed\u7684\uff0c\u5373\u7ecf\u8fc7\u7684\u9876\u70b9\u6570\u6700\u5c11\u3002\u518d\u8005\uff0cDinic \u7b97\u6cd5\u627e\u4e00\u6761\u589e\u5e7f\u8def\u5f84\u540c\u65f6\u53ef\u4ee5\u627e\u5230\u591a\u6761\uff0c\u7c7b\u4f3c\u589e\u5e7f\u8def\u5f84\u6811\u3002\u6bd4\u5982\u6211\u4eec\u627e\u5230\u4e86\u4e00\u6761\u589e\u5e7f\u8def\u5f84\uff0c\u8fd9\u6761\u589e\u5e7f\u8def\u5f84\u6240\u589e\u52a0\u7684\u6d41\u91cf\u4e3a C,\u5219\u8fd9\u6761\u589e\u5e7f\u8def\u5f84\u4e0a\u5fc5\u7136\u6709\u4e00\u6761\u8fb9&lt;i,j&gt;\u6b8b\u4f59\u5bb9\u91cf\u4e3a C\uff0c\u8fd9\u662f\u6211\u4eec\u4e0d\u5fc5\u53c8\u4ece\u8d77\u70b9\u5f00\u59cb\u5bfb\u627e\u589e\u5e7f\u8def\uff0c\u800c\u662f\u4ece i \u9876\u70b9\u51fa\u53d1\u627e\u589e\u5e7f\u8def\uff0c\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u9ad8\u4e86\u6548\u7387\uff0c\u8fd9\u597d\u50cf\u5c31\u662f\u6240\u8bf4\u7684\u591a\u8def\u589e\u5e7f<\/p>\n<pre class=\"brush: cpp; gutter: true\">\/*\nPku 3469 Dual Core CPU ( Dinic )\n*\/\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n \nint const N= 20010, M= 880010;\nint n, m, idx= -1, S, T;\n \n#define min(a,b) ( (a)&lt; (b)? (a): (b) )\n \nstruct Edge{\n    int wt, v;\n\n    Edge* next;\n }tb[M];\n\nEdge* mat[N];\n int h[N], que[N];\n\nvoid inline add( int u, int v, int x, int y ){\n\n    idx++;\n\n    tb[idx].wt= x; tb[idx].v= v;\n\n    tb[idx].next= mat[u]; mat[u]= tb+ idx;\n\n    idx++;\n\n    tb[idx].wt= y; tb[idx].v= u;\n\n    tb[idx].next= mat[v]; mat[v]= tb+ idx;\n }\n\ninline Edge* reserve( Edge* p ){\n\n    return tb+ ((p-tb)^1);\n }\n\nint bfs(){\n\n    int head= 0, tail= 0;\n\n    for( int i= 0; i&lt;= T; ++i ) h[i]= -1;\n\n    que[0]= S; h[S]= 0;\n\n    while( head&lt;= tail ){\n\n        int u= que[head++];\n\n        for( Edge* p= mat[u]; p; p= p-&gt;next ){\n\n            int v= p-&gt;v, w= p-&gt;wt;\n\n            if( h[v]== -1 &amp;&amp; w&gt; 0 ){\n\n                h[v]= h[u]+ 1; que[++tail]= v;\n\n            }\n\n        }\n\n    }\n\n    return h[T]!= -1;\n }\n\nint dfs( int u, int flow ){\n\n    if( u== T ) return flow;\n\n    int tf= 0, f;\n\n    for( Edge* p= mat[u]; p; p= p-&gt;next ){\n\n        int v= p-&gt;v, w= p-&gt;wt;\n\n        if( h[v]== h[u]+ 1 &amp;&amp; w&gt; 0 &amp;&amp; tf&lt; flow &amp;&amp; ( f= dfs( v, min( w, flow- tf ) ) ) ){\n\n            p-&gt;wt-= f;\n\n            reserve(p)-&gt;wt+= f;\n\n            tf+= f;\n\n        }\n\n    }\n\n    if( tf== 0 ) h[u]= -1;\n\n    return tf;\n }\n\nint inline read(){\n\n    char ch;\n\n    int d;\n\n    while( ch= getchar(), ch== ' ' || ch== 'n');\n\n    d= ch- '0';\n\n    while( ch= getchar(), ch&gt;= '0' &amp;&amp; ch&lt;= '9' )\n\n    d= d* 10+ ch- '0';\n\n    return d;\n }\n\nint main(){\n\n    scanf(\"%d%d\",&amp;n,&amp;m );\n\n    S= 0, T= n+ 1; idx= -1;\n\n    for( int i= 0; i&lt;= n+ 1; ++i ) mat[i]= 0;\n\n    for( int i= 1; i&lt;= n; ++i ){\n\n        int x, y;\n\n        x= read(),y=read();\n\n        add( S, i, x, 0 ); add( i, T, y, 0 );\n\n    }\n\n    for( int i= 0; i&lt; m; ++i ){\n\n        int x, y, z;\n\n        x=read(),y=read(),z=read();\n\n        add( x, y, z, z );\n\n    }\n\n    int ans= 0;\n\n    while( bfs()) ans+= dfs( S, 0x7fffffff );\n\n    printf(\"%dn\", ans );\n\n    return 0;\n }<\/pre>\n<p>\u7136\u5219\u7b97\u6cd5\u7684\u4f18\u5316\u662f\u65e0\u5c3d\u5934\u7684\uff0cDinic \u7b97\u6cd5\u8981\u591a\u6b21\u8ba1\u7b97\u5c42\u6b21\u56fe\uff0c\u589e\u52a0\u4e86\u590d\u6742\u5ea6\u3002\u662f\u4e0d\u662f\u53ef\u4ee5\u4e0d\u591a\u6b21\u8ba1\u7b97\u5c42\u6b21\u56fe\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\uff0c\u8fd9\u5c31\u4ea7\u751f\u4e86 SAP \u7b97\u6cd5\u3002<\/p>\n<p>SAP \u8ba1\u7b97\u7684\u662f\u53cd\u5411\u56fe\u7684\u5c42\u6b21\u56fe\uff0c\u8fd9\u548c\u539f\u56fe\u7684\u5c42\u6b21\u56fe\u662f\u4f5c\u7528\u662f\u4e00\u6837\u7684\uff0c\u5f53\u7136\u5176\u5b9eDinic\u4e5f\u53ef\u4ee5\u8ba1\u7b97\u53cd\u5411\u56fe\u7684\u5c42\u6b21\u56fe\u3002\u786e\u4fdd\u589e\u5e7f\u8def\u5f84\u6700\u77ed\uff0cSAP \u610f\u601d\u5c31\u662f Shortest Augmenting Paths\uff0c\u6700\u77ed\u589e\u5e7f\u8def\u5f84\u3002\u8ba1\u7b97\u53cd\u5411\u56fe\u7684\u5c42\u6b21\u56fe\u662f\u4fbf\u4e8e\u91cd\u65b0\u7ed9\u9876\u70b9\u6807\u53f7\uff0c\u5373\u91cd\u65b0\u786e\u5b9a\u5176\u5c42\u6b21\u56fe\u3002\u5177\u4f53\u505a\u6cd5\u4e3a\uff0c\u5f53\u6211\u4eec\u627e\u5230\u4e00\u6761\u7ecf\u8fc7\u9876\u70b9 i \u7684\u589e\u5e7f\u8def\u5f84\u540e\uff0c\u5bf9\u4e8e\u6240\u6709\u8fb9&lt;i,j&gt;\uff0c\u8ba1\u7b97\u51fa m= min{ h[j] ]\uff0c\u8fd9\u662f\u6211\u4eec\u5c31\u53ef\u4ee5\u628a i \u91cd\u65b0\u6807\u53f7\u4e3a h[i]= min+ 1\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u9996\u5148\u4e0d\u9700\u8981\u8ba1\u7b97\u53cd\u5411\u56fe\u7684\u5c42\u6b21\u56fe\uff0c\u800c\u662f\u628a\u6240\u6709\u9876\u70b9\u7684\u5c42\u6b21\u6807\u4e3a0\uff0c\u8fd9\u5bf9\u6548\u7387\u6ca1\u591a\u5927\u5f71\u54cd\u3002\u7136\u5219\u8fd9\u6837\u4f18\u5316\u540e\u8fd8\u4e0d\u591f\uff0c\u53c8\u51fa\u73b0\u4e86\u4e00\u4e2a gap \u4f18\u5316\u3002\u6240\u8c13 gap \u4f18\u5316\u5c31\u662f\u8ba1\u7b97\u51fa\u5c42\u6b21\u56fe\u540e\uff0c\u5c42\u6b21\u51fa\u73b0\u65ad\u5c42\uff0c\u8fd9\u662f\u53ef\u4ee5\u786e\u5b9a\u6b8b\u4f59\u7f51\u7edc\u4e2d\u4e0d\u5b58\u5728\u589e\u5e7f\u8def\u5f84\u4e86\uff0c\u7b97\u6cd5\u5c31\u53ef\u4ee5\u63d0\u524d\u7ed3\u675f\u3002\u8fd9\u4e2a\u4f18\u5316\u770b\u4f3c\u5fae\u5c0f\uff0c\u5b9e\u9645\u4f5c\u7528\u786e\u4e0d\u5c0f\u3002\u505a\u6cd5\u5c31\u662f\u4fdd\u5b58\u67d0\u4e00\u4e2a\u6807\u53f7\u5728\u6b8b\u4f59\u7f51\u7edc\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5982\u679c\u662f 0 \uff0c\u5c31\u65ad\u5c42\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u89e3\u6700\u5927\u6d41\u4e00\u822c\u91c7\u7528\u4e24\u79cd\u601d\u8def\uff0c\u4e00\u79cd\u662f\u9884\u6d41\uff0c\u53e6\u4e00\u5404\u662f\u589e\u5e7f\u8def\u3002\u589e\u5e7f\u8def\u8fd9\u79cd\u601d\u60f3\u662f\u57fa\u4e8e\u4ee5\u4e0b\u5b9a\u7406\uff1a \u5b9a\u7406\u4e00\uff1a\u8bbe\u7f51\u7edc G \u7684\u6e90\u4e3a S, \u6c47\u548c T\uff0cF \u548c C \u5206\u522b\u4e3a G \u7684\u6d41\u548c\u5bb9\u91cf\uff0c\u5219 F \u662f\u6700\u5927\u6d41\u5f53\u4e14\u4ec5\u5f53 G \u4e2d\u4e0d\u5b58\u5728\u53ef\u589e\u5e7f\u8def\u3002 \u7531\u6b64\u5b9a\u7406\u53ef\u4ee5\u8bbe\u8ba1\u5f88\u591a\u7b97\u6cd5\u6765\u8ba1\u7b97\u6700\u5927\u6d41\uff0cDinic \u7b97\u6cd5\u662f\u5176\u4e2d\u4e00\u79cd\u6bd4\u8f83\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u5176\u590d\u6742\u5ea6\u4e3a O(n2*m) Dinic \u7b97\u6cd5\u7684\u57fa\u672c\u6b65\u9aa4\u4e3a: 1) \u8ba1\u7b97\u6b8b\u4f59\u7f51\u7edc\u7684\u5c42\u6b21\u56fe\u3002\u6211\u4eec\u5b9a\u4e49 h[i] \u4e3a\u9876\u70b9 i \u8ddd\u79bb\u6e90 S \u6240\u7ecf\u8fc7\u5230\u6700\u5c0f\u8fb9\u6570\uff0c\u6c42\u51fa\u6240\u6709\u9876\u70b9\u7684 h \u503c\uff0ch[] \u503c\u76f8\u540c\u7684\u9876\u70b9\u5c5e\u4e8e\u540c\u4e00\u5c42\uff0c\u8fd9\u5c31\u662f\u7f51\u7edc\u7684\u5c42\u6b21\u56fe\u3002 2) \u5728\u5c42\u6b21\u56fe\u4e0a\u8fdb\u884c BFS \u589e\u5e7f\uff0c\u76f4\u5230\u4e0d\u5b58\u5728\u589e\u5e7f\u8def\u5f84\u3002\u8fd9\u65f6\u6c42\u5f97\u7684\u589e\u5e7f\u8def\u5f84\u4e0a\u9876\u70b9\u662f\u5206\u5c42\u7684\uff0c\u8def\u5f84\u4e0a\u4e0d\u53ef\u80fd\u5b58\u5728\u4e24\u4e2a\u9876\u70b9\u5c5e\u4e8e\u540c\u4e00\u5c42\uff0c\u5373 h[i]== h[j] (i!= j &hellip; <a href=\"https:\/\/blog.rexdf.org\/en\/2011\/10\/%e6%9c%80%e5%a4%a7%e6%b5%81-dinic%e7%ae%97%e6%b3%95%e5%92%8csap%e7%ae%97%e6%b3%95%e7%ae%80%e4%bb%8b\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[36,55,214,207],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-algorithm","tag-dinic","tag-sap","tag-214","tag-acm"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":0,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}