{"id":125,"date":"2010-08-14T08:34:20","date_gmt":"2010-08-14T00:34:20","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=125"},"modified":"2012-03-03T08:34:34","modified_gmt":"2012-03-03T00:34:34","slug":"zoj-2314-reactor-cooling","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/zoj-2314-reactor-cooling\/","title":{"rendered":"ZOJ 2314. Reactor Cooling"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>\u65e0\u6e90\u6c47\u6709\u4e0a\u4e0b\u754c\u7f51\u7edc\u7684\u53ef\u884c\u6d41\u3002<br \/>\n<!--more--><br \/>\nhttp:\/\/acm.zju.edu.cn\/onlinejudge\/showProblem.do?problemId=1314<\/p>\n<pre lang=\"cpp\" file=\"Edmonds_Karp.cpp\">\r\n\/*\r\n    ID: xiaodao\r\n    PROG: ZOJ 2314. Reactor Cooling\r\n    TAGS: Network_Flow\r\n*\/\r\n\r\n#include <iostream>\r\n#include <cstdio>\r\n#include <list>\r\nusing namespace std;\r\nconst int INF = 1000000;\r\nconst int N = 302, M = 1000000;\r\nstruct rec{\r\n    int v, p, k;\r\n    \/\/ Vertex, Parient ,key\r\n};\r\nint x[M], y[M], l[M], r; \/\/ Edge ..\r\nint C[N][N], F[N][N]; \/\/ Capacity, Flow..\r\nbool V[N]; rec Q[N]; \/\/ Visited, Queue..\r\nint head, tail;\r\nint n, m, ans;\r\n\r\n\r\n\r\n\r\nbool find_path(){\r\n    memset(V, false, sizeof(V));\r\n    Q[0].v = 0; Q[0].k = INF; V[0] = true;\r\n    head = 0; tail = 1;\r\n    int u, v;\r\n\r\n    while (head<tail){\r\n        u = Q[head].v;\r\n        for (v=0;v<=n;v++){\r\n            if (!V[v] &#038;&#038; F[u][v] < C[u][v]){\r\n                Q[tail].v = v; Q[tail].p = head;\r\n                Q[tail].k = min(Q[head].k, C[u][v] - F[u][v]);\r\n                if (v == n) return true;\r\n                V[v] = true; tail++;\r\n            }\r\n        }\r\n        head++;\r\n    }\r\n    return false;\r\n}\r\n\r\nvoid add_path(){\r\n    int u, v; int delta = Q[tail].k;\r\n\r\n    while (tail!=0){\r\n        u = Q[head].v; v = Q[tail].v;\r\n        F[u][v] += delta; F[v][u] -= delta;\r\n        tail = head; head = Q[head].p;\r\n    }\r\n}\r\n\r\nvoid Edmonds_Karp(){\r\n    while (find_path())\r\n        add_path();\r\n}\r\n\r\nvoid init(){\r\n    memset(C, 0, sizeof(C));\r\n    memset(F, 0, sizeof(F));\r\n    cin >> n >> m; n++;\r\n    for (int i=0;i<m;i++){\r\n        scanf(\"%d%d%d%d\", &#038;x[i], &#038;y[i], &#038;l[i], &#038;r);\r\n        C[x[i]][y[i]] += r - l[i];\r\n        C[0][y[i]] += l[i];\r\n        C[x[i]][n] += l[i];\r\n    }\r\n}\r\n\r\nint main(){\r\n\r\n    int T; cin >> T;\r\n    for (int i=0;i<T;i++){\r\n        if (i!=0) cout << endl;\r\n        init(); Edmonds_Karp();\r\n        for (int j=0;j<m;j++){\r\n            if (F[0][y[j]] != C[0][y[j]]){\r\n                cout << \"NO\" << endl; goto quay;\r\n\t\t\t}\r\n        }\r\n        cout << \"YES\" << endl;\r\n        for ( int i = 0 ; i < m ; i ++ )\r\n            cout << F[x[i]][y[i]] + l[i] << endl ;\r\n        quay:;\r\n    }\r\n}\r\n<\/pre>\n<pre lang=\"cpp\" file=\"Relabel_to_Front\">\r\n\/*\r\n    ID: xiaodao\r\n    PROG: ZOJ 2314. Reactor Cooling\r\n    TAGS: Network_Flow\r\n*\/\r\n\r\n#include <iostream>\r\n#include <cstdio>\r\n#include <list>\r\nusing namespace std;\r\nconst int INF = 1000000;\r\nconst int N = 302, M = 1000000;\r\nint x[M], y[M], l[M], r; \/\/ Edge ..\r\nint C[N][N], F[N][N]; \/\/ Capacity, Flow..\r\nint H[N], E[N]; \/\/ .Height, Excess ...\r\nint n, m, sum;\r\nbool changed;\r\n\r\n\r\n\r\nvoid push(int u, int v, int delta){\r\n    F[u][v] += delta; F[v][u] -= delta;\r\n    E[u] -= delta; E[v] += delta;\r\n}\r\n\r\nvoid relabel(int u){\r\n    H[u] = INF;\r\n    for (int v=0;v<=n;v++){\r\n        if (u!=v &#038;&#038; F[u][v] < C[u][v])\r\n            H[u] = min(H[u], H[v]+1);\r\n    }\r\n}\r\n\r\nvoid discharge(int u){\r\n    if (E[u]==0){\r\n        changed = false;\r\n        return;\r\n    }\r\n\r\n    changed = true;\r\n    for (int v=0;v<=n;v++){\r\n        if (u == v) continue;\r\n        if (H[u] > H[v] && F[u][v] < C[u][v]){\r\n            push(u, v, min(E[u], C[u][v] - F[u][v]));\r\n            if (E[u]==0) {changed = false; return;}\r\n        }\r\n    }\r\n    relabel(u);\r\n}\r\n\r\n\r\n\r\nvoid relabel_to_front(){\r\n    \/\/Relabel-to-front algorithm, ie. using FIFO heuristic\r\n\r\n    \/\/1.Send as much flow from s as possible.\r\n    memset(E, 0, sizeof(E));\r\n    memset(H, 0, sizeof(H));\r\n    E[0] = INF; H[0] = n+1;\r\n\r\n    for (int i=1;i<=n;i++){\r\n        if (C[0][i] > 0) {\r\n            push(0, i, C[0][i]);\r\n            if (i!=n) H[i] = 1;\r\n        }\r\n    }\r\n\r\n    \/\/2.Build a list of all nodes except s and t.\r\n    list<int> L;\r\n    for (int i=1;i<n;i++)\r\n        L.push_back(i);\r\n\r\n\r\n    \/\/3.As long as we have not traversed the entire list:\r\n    list<int>::iterator it=L.begin();\r\n    while (it!=L.end()){\r\n        \/\/1.Discharge the current node.\r\n        discharge(*it);\r\n        \/\/2.If the height of the current node changed:\r\n        if (changed){\r\n            \/\/ Move the current node to the front of the list ...\r\n            L.push_front(*it); L.erase(it);\r\n            \/\/ .. And restart the traversal from the front of the list.\r\n            it = L.begin();\r\n        }\r\n        else\r\n            it++;\r\n    }\r\n}\r\n\r\nvoid init(){\r\n    memset(C, 0, sizeof(C));\r\n    memset(F, 0, sizeof(F));\r\n    cin >> n >> m; n++;\r\n    sum = 0;\r\n    for (int i=0;i<m;i++){\r\n        scanf(\"%d%d%d%d\", &#038;x[i], &#038;y[i], &#038;l[i], &#038;r);\r\n        C[x[i]][y[i]] += r - l[i];\r\n        C[0][y[i]] += l[i];\r\n        C[x[i]][n] += l[i];\r\n        sum += l[i];\r\n    }\r\n}\r\n\r\nint main(){\r\n\r\n    int T; cin >> T;\r\n    for (int i=0;i<T;i++){\r\n        if (i!=0) cout << endl;\r\n        init(); relabel_to_front();\r\n\r\n        if (E[n] != sum)\r\n            cout << \"NO\" << endl;\r\n        else {\r\n            cout << \"YES\" << endl;\r\n            for ( int i = 0 ; i < m ; i ++ )\r\n                cout << F[x[i]][y[i]] + l[i] << endl ;\r\n        }\r\n    }\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : \u65e0\u6e90\u6c47\u6709\u4e0a\u4e0b\u754c\u7f51\u7edc\u7684\u53ef\u884c\u6d41\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":[1],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-21","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/125","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=125"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}