{"id":118,"date":"2010-08-20T08:30:32","date_gmt":"2010-08-20T00:30:32","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=118"},"modified":"2012-11-15T08:58:02","modified_gmt":"2012-11-15T00:58:02","slug":"ural-1076-trash","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/ural-1076-trash\/","title":{"rendered":"URAL 1076. Trash"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>n \u4e2a\u7bb1\u5b50\u5b58\u653e\u6709 n \u79cd\u7269\u54c1\uff0c\u8981\u6c42\u786e\u5b9a\u4e00\u79cd\u5206\u7c7b\u65b9\u6848\uff0c\u4f7f\u5f97\u79fb\u52a8\u4ee3\u4ef7\u6700\u5c0f\u3002<\/p>\n<h3>Analyse :<\/h3>\n<p>\u6c42\u79fb\u52a8\u4ee3\u4ef7\u6700\u5c0f\u5212\u5f52\u5230\u8ba1\u7b97\u4fdd\u7559\u5728\u539f\u5904\u7684\u7269\u54c1\u6570\u6700\u5927\u3002\u3002\u3002\u7136\u540e\u3002\u3002\u3002<br \/>\n<!--more--><\/p>\n<pre class=\"brush: cpp; collapse: true; light: false; title: KM-1; toolbar: true; notranslate\" title=\"KM-1\">\r\nconst int N = 150;\r\n\r\nint W&#x5B;N]&#x5B;N], lx&#x5B;N], ly&#x5B;N], p&#x5B;N];\r\nbool vx&#x5B;N], vy&#x5B;N];\r\nint n;\r\n\r\nvoid init(){\r\n\tRD(n); REP_2(i, j, n, n) RD(W&#x5B;i]&#x5B;j]);\r\n}\r\n\r\n#define w(x, y) (lx&#x5B;x] + ly&#x5B;y] - W&#x5B;x]&#x5B;y])\r\n\r\nbool dfs(int x){\r\n\tvx&#x5B;x] = true; REP(y, n) if (!vy&#x5B;y] &amp;&amp; !w(x, y)){\r\n        vy&#x5B;y] = true; if(p&#x5B;y]==-1||dfs(p&#x5B;y])){\r\n            p&#x5B;y] = x;\r\n            return true;\r\n        }\r\n\t}\r\n\treturn false;\r\n}\r\n\r\nvoid KM(){\r\n\r\n\tFLC(p, -1); RST(lx, ly);\r\n\r\n\tREP_2(i, j, n, n) checkMax(lx&#x5B;i], W&#x5B;i]&#x5B;j]);\r\n\r\n\tREP(x, n){\r\n\t\twhile (true){\r\n\t\t\tRST(vx, vy); if (dfs(x)) break;\r\n\r\n\t\t\tint delta = INF;\r\n\t\t\tREP_2(i, j, n, n) if(vx&#x5B;i] &amp;&amp; !vy&#x5B;j])\r\n                checkMin(delta, w(i, j));\r\n\r\n\t\t\tREP(i, n){\r\n\t\t\t\tif (vx&#x5B;i]) lx&#x5B;i] -= delta;\r\n\t\t\t\tif (vy&#x5B;i]) ly&#x5B;i] += delta;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n\r\nvoid print(){\r\n\tint res = 0; REP_2(i, j, n, n) res += W&#x5B;i]&#x5B;j];\r\n    REP(i, n) res -= W&#x5B;p&#x5B;i]]&#x5B;i];\r\n\tOT(res);\r\n}\r\n\r\nint main(){\r\n\tinit(); KM();\r\n\tprint();\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; collapse: true; light: false; title: KM-2; toolbar: true; notranslate\" title=\"KM-2\">\r\nconst int N = 150;\r\n\r\nint W&#x5B;N]&#x5B;N], lx&#x5B;N], ly&#x5B;N], p&#x5B;N], slack&#x5B;N];\r\nbool vx&#x5B;N], vy&#x5B;N];\r\nint n;\r\n\r\nvoid init(){\r\n\tRD(n); REP_2(i, j, n, n) RD(W&#x5B;i]&#x5B;j]);\r\n}\r\n\r\n#define w(x, y) (lx&#x5B;x] + ly&#x5B;y] - W&#x5B;x]&#x5B;y])\r\n\r\nbool dfs(int x){\r\n    vx&#x5B;x] = true; REP(y, n) if (!vy&#x5B;y]){\r\n        if (!w(x, y)){\r\n            vy&#x5B;y] = true; if(p&#x5B;y]==-1||dfs(p&#x5B;y])){\r\n                p&#x5B;y] = x;\r\n                return true;\r\n            }\r\n        }\r\n        else {\r\n            checkMin(slack&#x5B;y], w(x, y));\r\n        }\r\n\t}\r\n\treturn false;\r\n}\r\n\r\nvoid KM(){\r\n\r\n\tFLC(p, -1); RST(lx, ly);\r\n\r\n\tREP_2(i, j, n, n) checkMax(lx&#x5B;i], W&#x5B;i]&#x5B;j]);\r\n\r\n\tREP(x, n){\r\n\t    FLC(slack, 0x7f); while (true){\r\n\t\t\tRST(vx, vy); if (dfs(x)) break;\r\n\r\n\t\t\tint delta = INF;\r\n\t\t\tREP(i, n) if (!vy&#x5B;i]) checkMin(delta, slack&#x5B;i]);\r\n\r\n\t\t\tREP(i, n){\r\n\t\t\t\tif (vx&#x5B;i]) lx&#x5B;i] -= delta;\r\n\t\t\t\tif (vy&#x5B;i]) ly&#x5B;i] += delta; else slack&#x5B;i] -= delta;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n\r\nvoid print(){\r\n\tint res = 0; REP_2(i, j, n, n) res += W&#x5B;i]&#x5B;j];\r\n    REP(i, n) res -= W&#x5B;p&#x5B;i]]&#x5B;i];\r\n\tOT(res);\r\n}\r\n\r\nint main(){\r\n\tinit(); KM();\r\n\tprint();\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; collapse: true; light: false; title: KM-3; toolbar: true; notranslate\" title=\"KM-3\">\r\nconst int N = 150;\r\n\r\nint W&#x5B;N]&#x5B;N], lx&#x5B;N], ly&#x5B;N], p&#x5B;N], delta;\r\nbool vx&#x5B;N], vy&#x5B;N];\r\nint n;\r\n\r\nvoid init(){\r\n\tRD(n); REP_2(i, j, n, n) RD(W&#x5B;i]&#x5B;j]);\r\n}\r\n\r\n#define w(x, y) (lx&#x5B;x] + ly&#x5B;y] - W&#x5B;x]&#x5B;y])\r\n\r\nbool dfs(int x){\r\n    vx&#x5B;x] = true; REP(y, n) if (!vy&#x5B;y]){\r\n        if (!w(x, y)){\r\n            vy&#x5B;y] = true; if(p&#x5B;y]==-1||dfs(p&#x5B;y])){\r\n                p&#x5B;y] = x;\r\n                return true;\r\n            }\r\n        }\r\n        else {\r\n            checkMin(delta, w(x, y));\r\n        }\r\n\t}\r\n\treturn false;\r\n}\r\n\r\nvoid KM(){\r\n\r\n\tFLC(p, -1); RST(lx, ly);\r\n\r\n\tREP_2(i, j, n, n) checkMax(lx&#x5B;i], W&#x5B;i]&#x5B;j]);\r\n\r\n\tREP(x, n){\r\n\t    while (true){\r\n\t\t\tRST(vx, vy); delta = INF; if (dfs(x)) break;\r\n\r\n\t\t\tREP(i, n){\r\n\t\t\t\tif (vx&#x5B;i]) lx&#x5B;i] -= delta;\r\n\t\t\t\tif (vy&#x5B;i]) ly&#x5B;i] += delta;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n\r\nvoid print(){\r\n\tint res = 0; REP_2(i, j, n, n) res += W&#x5B;i]&#x5B;j];\r\n    REP(i, n) res -= W&#x5B;p&#x5B;i]]&#x5B;i];\r\n\tOT(res);\r\n}\r\n\r\nint main(){\r\n\tinit(); KM();\r\n\tprint();\r\n}\r\n<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<pre class=\"brush: cpp; collapse: true; light: false; title: Mincost-Flow-1; toolbar: true; notranslate\" title=\"Mincost-Flow-1\">\r\nconst int INF = 0x3f3f3f3f;\r\nconst int N = 302;\r\n\r\nint C&#x5B;N]&#x5B;N], W&#x5B;N]&#x5B;N];\r\nint Q&#x5B;512], d&#x5B;N], p&#x5B;N], head, tail; bool inQ&#x5B;N];\r\nint n, s, t, tot, cost;\r\n\r\nvoid init(){\r\n\tRD(n), s = 0, t = 2 * n + 1, tot = 0;\r\n\tint w; REP_1(i, n){\r\n\t    C&#x5B;s]&#x5B;i] = C&#x5B;i+n]&#x5B;t] = 1;\r\n\t    REP_1(j, n){\r\n\t        C&#x5B;i]&#x5B;j+n] = 1, tot += _RD(w);\r\n\t        W&#x5B;i]&#x5B;j+n] = -w, W&#x5B;j+n]&#x5B;i] = w;\r\n\t    }\r\n\t}\r\n}\r\n\r\nbool spfa(){\r\n    RST(inQ); FLC(d, 0x3f), d&#x5B;Q&#x5B;head = 0] = s] = 0, tail = 1;\r\n    while (head &lt; tail){\r\n        int u = Q&#x5B;head++ &amp; 511]; inQ&#x5B;u] = false;\r\n        FOR_1(v, 1, t){\r\n            if (C&#x5B;u]&#x5B;v] &amp;&amp; d&#x5B;v] &gt; d&#x5B;u] + W&#x5B;u]&#x5B;v]){\r\n                d&#x5B;v] = d&#x5B;u] + W&#x5B;u]&#x5B;v], p&#x5B;v] = u;\r\n                if (!inQ&#x5B;v]) Q&#x5B;tail++ &amp; 511] = v, inQ&#x5B;v] = true;\r\n            }\r\n        }\r\n    }\r\n    return d&#x5B;t] != INF;\r\n}\r\n\r\nvoid addPath(){\r\n    int u, v = t; do{\r\n        u = p&#x5B;v], --C&#x5B;u]&#x5B;v], ++C&#x5B;v]&#x5B;u],\r\n        cost += W&#x5B;u]&#x5B;v], v = u;\r\n    }while(u != s);\r\n}\r\n\r\nvoid solve(){\r\n    cost = 0;\r\n    while (spfa()) addPath();\r\n}\r\n\r\nint main(){\r\n\tinit(); solve();\r\n\tOT(tot + cost);\r\n}\r\n<\/pre>\n<p><\/bk><\/p>\n<h3>Further discussion :<\/h3>\n<p>KM-1\uff08 O(n4) \u5b9e\u73b0\u3002\u3002<br \/>\nKM-2 (.. \u6807\u51c6 O(n3) \u5b9e\u73b0\u3002\u3002\u5f15\u5165 \u61d2\u6807\u8bb0(slack label) \u601d\u60f3\u3002\u3002\u3002\u3002<a href=\"http:\/\/cuitianyi.com\/blog\/%e6%b1%82%e6%9c%80%e5%a4%a7%e6%9d%83%e4%ba%8c%e5%88%86%e5%8c%b9%e9%85%8d%e7%9a%84km%e7%ae%97%e6%b3%95\">\u53c2\u89c1 dd \u8fd9\u7bc7 <\/a>\u3002\u3002 :<\/p>\n<blockquote><p>&#8220;&#8230; . \u7ef4\u62a4\u53f3\u8fb9\u7684\u70b9 j \u7684\u6240\u6709\u4e0d\u5728\u5bfc\u51fa\u5b50\u56fe\u7684\u8fb9\u5bf9\u5e94\u7684 lx[i]+ly[j]-w[i][j] \u7684\u6700\u5c0f\u503c&#8230;&#8230;&#8221;<\/p><\/blockquote>\n<p>KM-3\uff08\u3002\u3002\u81ea\u5df1 yy \u7684\u3002\u3002dfs \u7684\u8fc7\u7a0b\u4e2d\u52a8\u6001\u66f4\u65b0 slack\u3002\u3002\u3002<del>O(n3)<\/del>\u3002\u3002\u3002<\/p>\n<p>&#8212;- UPD &#8212;-<\/p>\n<p><a href=\"http:\/\/paste.ubuntu.com\/1359295\/\">.. KM-4 ..<\/a> ( \u975e\u9012\u5f52\u5b9e\u73b0\u3002\u3002<a href=\"http:\/\/en.wikipedia.org\/wiki\/Kuhn-Munkres_algorithm\">\u3002\u3002\u5eb6\u6c11\u5199\u6cd5\u3002\u3002\u3002<\/a>\u3002\u3002\u3002<br \/>\n<a href=\"http:\/\/paste.ubuntu.com\/1359264\/\">.. KM-5 ..<\/a> ( \u975e\u9012\u5f52\u5b9e\u73b0\u3002\u3002<a href=\"http:\/\/community.topcoder.com\/tc?module=Static&#038;d1=tutorials&#038;d2=hungarianAlgorithm\">\u3002\u3002TC \u9ad8\u7aef\u72d7\u5199\u6cd5\u3002\u3002<\/a>\u3002\u3002\u3002<br \/>\n<a href=\"http:\/\/paste.ubuntu.com\/1357371\/\"> .. Mincost-Flow-2&#8230; <\/a>\uff08\u8d39\u7528\u6d41\u8fde\u7eed\u589e\u5149\u8def\u7b97\u6cd5\u7684 Dijkstra \u5b9e\u73b0\u3002\u3002\u3002\u4e0d\u8fc7\u5728\u8fd9\u9898\u91cc\u662f\u9519\u7684\u3002\u3002\u56e0\u4e3a\u53d6\u8d1f\u503c\u4ee5\u540e\u4ea7\u751f\u4e86\u8d1f\u6743\u73af\u3002\u3002\u3002\u4e0d\u77e5\u9053\u8fd8\u80fd\u4e0d\u80fd\u62a2\u6551\u3002\u3002\uff08\uff1f<br \/>\n<a style=\"color:red;\" href=\"https:\/\/www.shuizilong.com\/house\/archives\/hdu-3435-a-new-graph-game\/\">\u3002\u3002\u6ce8\u610f KM-3 \u6bcf\u6b21\u66f4\u65b0\u53ef\u80fd\u8fbe\u4e0d\u5230\u4e0b\u754c\u3002\uff01\uff01\u3002\uff08\u3002\u3002\u590d\u6742\u5ea6\u4e0d\u80fd\u4fdd\u8bc1 O(n3) \uff1f\u3002\u3002\u3002<\/a><\/p>\n<h3>External link :<\/h3>\n<p><a href=\"http:\/\/acm.timus.ru\/problem.aspx?space=1&#038;num=1076\">http:\/\/acm.timus.ru\/problem.aspx?space=1&#038;num=1076<\/a><br \/>\n<a href=\"http:\/\/hi.baidu.com\/fqq11679\/blog\/item\/9c5c22a69c647392d14358bc.html\">http:\/\/hi.baidu.com\/fqq11679\/blog\/item\/9c5c22a69c647392d14358bc.html<\/a><br \/>\n<a href=\"http:\/\/www.cppblog.com\/MatoNo1\/archive\/2011\/07\/23\/151724.html\">http:\/\/www.cppblog.com\/MatoNo1\/archive\/2011\/07\/23\/151724.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : n \u4e2a\u7bb1\u5b50\u5b58\u653e\u6709 n \u79cd\u7269\u54c1\uff0c\u8981\u6c42\u786e\u5b9a\u4e00\u79cd\u5206\u7c7b\u65b9\u6848\uff0c\u4f7f\u5f97\u79fb\u52a8\u4ee3\u4ef7\u6700\u5c0f\u3002 Analyse : \u6c42\u79fb\u52a8\u4ee3\u4ef7\u6700\u5c0f\u5212\u5f52\u5230\u8ba1\u7b97\u4fdd\u7559\u5728\u539f\u5904\u7684\u7269\u54c1\u6570\u6700\u5927\u3002\u3002\u3002\u7136\u540e\u3002\u3002\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[33],"tags":[64,92],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-ural","tag-64","tag-92"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-1U","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":1,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":176,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/118\/revisions\/176"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}