{"id":123,"date":"2010-08-16T08:33:34","date_gmt":"2010-08-16T00:33:34","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=123"},"modified":"2012-03-03T08:33:47","modified_gmt":"2012-03-03T00:33:47","slug":"zoj-2760-how-many-shortest-path","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/zoj-2760-how-many-shortest-path\/","title":{"rendered":"ZOJ 2760. How Many Shortest Path"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>\u6c42\u4ece\u6e90\u70b9\u5230\u7ec8\u70b9\uff0c\u4e92\u4e0d\u76f8\u4ea4\u7684\u6700\u77ed\u8def\u5f84\u7684\u6700\u5927\u503c\u3002<br \/>\n<!--more--><\/p>\n<h3>Analyse :<\/h3>\n<p>\u9884\u5904\u7406\u5b8c\u4ee5\u540e\uff0c\u7528\u6700\u5927\u6d41\u6c42\u8fb9\u8fde\u901a\u5ea6\u3002<br \/>\n&#8230;<\/p>\n<pre lang=\"cpp\" file=\"ZOJ_2760.cpp\">\r\n\/*\r\n    Author: xiaodao\r\n    Prog: ZOJ 2760. How Many Shortest Path\r\n    Status: Accepted\r\n    Last modify: GMT +8 Aug.20th 11:32    \r\n    TAGS: Network-Flow, Floyd\r\n*\/\r\n\r\n#include <iostream>\r\n#include <cstdio>\r\n#include <vector>\r\nusing namespace std;\r\nconst int INF = 0x7f7f7f7f;\r\nconst int N = 100;\r\nint G[N][N]; bool C[N][N]; int D[N][N];\r\nint P[N], Q[N]; int head, tail;\r\nint n, s, t, u, v;\r\nint ans;\r\n\r\n\r\n\r\nvoid add_path(){\r\n    ans++;\r\n    while (u!=-2){\r\n        C[u][v] = false; C[v][u] = true;\r\n        v = u; u = P[u];\r\n    }\r\n}\r\n\r\nbool find_path(){\r\n    memset(P, -1, sizeof(P));\r\n    Q[0] = s; P[s] = -2; head = 0; tail = 1;\r\n    while (head<tail){\r\n        u = Q[head];\r\n        for (v=0;v<n;v++){\r\n            if (P[v] == -1 &#038;&#038; C[u][v]){\r\n                Q[tail] = v; P[v] = u;\r\n                if (v == t) return true;\r\n                tail++;\r\n            }\r\n        }\r\n        head++;\r\n    }\r\n    return false;\r\n}\r\n\r\nvoid Edmonds_Karp(){\r\n    ans = 0;\r\n    while (find_path())\r\n        add_path();\r\n}\r\n\r\n\r\n\r\n\r\nvoid building_the_graph(){    \r\n    memcpy(D, G, sizeof(G));\r\n    for(int k=0;k<n;k++){\r\n        for(int i=0;i<n;i++){\r\n            if(D[i][k]==INF) continue;\r\n            for(int j=0;j<n;j++){\r\n                if (D[k][j]==INF) continue;\r\n                D[i][j] = min(D[i][j], D[i][k] + D[k][j]);\r\n            }\r\n        }\r\n    }\r\n\r\n    for (int i=0;i<n;i++){\r\n        if (D[s][i]==INF) continue;\r\n        for (int j=0;j<n;j++){\r\n            if (D[j][t] == INF) continue;\r\n            C[i][j] = (G[i][j] != INF &#038;&#038; D[s][i] + G[i][j] + D[j][t] == D[s][t]);\r\n        }\r\n    }\r\n}\r\n\r\n\r\nvoid init(){\r\n    memset(G, 0, sizeof(G));\r\n    for (int i=0;i<n;i++)\r\n        for (int j=0;j<n;j++){\r\n            scanf(\"%d\", &#038;G[i][j]);  \r\n            if(i == j) G[i][j] = 0; else \/\/ \u8fd9\u53e5\u53bb\u6389\u7684\u5316\u3002\u3002\u3002\u5c31\u8981\u7455\u75b5\u4e86\u3002\u3002\r\n            if (G[i][j]==-1) G[i][j] = INF;\r\n        }\r\n    scanf(\"%d%d\", &#038;s, &#038;t);\r\n}\r\n\r\n\r\n\r\nint main(){\r\n    while (scanf(\"%d\", &#038;n) == 1){\r\n        init();\r\n        if (s == t) printf(\"inf\\n\");\r\n        else {\r\n            building_the_graph(); Edmonds_Karp();\r\n            printf(\"%d\\n\", ans);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<pre lang=\"cpp\" file=\"ZOJ_2760.cpp\">\r\n\/*\r\n    Author: xiaodao\r\n    Prog: ZOJ 2760. How Many Shortest Path\r\n    Status: Accepted\r\n    Last modify: GMT +8 Aug.20th 11:56    \r\n    TAGS: Network-Flow, Dijikstra\r\n*\/\r\n\r\n#include <iostream>\r\n#include <cstdio>\r\n#include <vector>\r\nusing namespace std;\r\nconst int INF = 0x7f7f7f7f;\r\nconst int N = 100;\r\nint G[N][N]; bool C[N][N]; int D[N];\r\nint P[N], Q[N]; int head, tail; bool V[N];\r\nint n, s, t, u, v;\r\nint ans;\r\n\r\n\r\n\r\nvoid add_path(){\r\n    ans++;\r\n    while (u!=-2){\r\n        C[u][v] = false; C[v][u] = true;\r\n        v = u; u = P[u];\r\n    }\r\n}\r\n\r\nbool find_path(){\r\n    memset(P, -1, sizeof(P));\r\n    Q[0] = s; P[s] = -2; head = 0; tail = 1;\r\n    while (head<tail){\r\n        u = Q[head];\r\n        for (v=0;v<n;v++){\r\n            if (P[v] == -1 &#038;&#038; C[u][v]){\r\n                Q[tail] = v; P[v] = u;\r\n                if (v == t) return true;\r\n                tail++;\r\n            }\r\n        }\r\n        head++;\r\n    }\r\n    return false;\r\n}\r\n\r\nvoid Edmonds_Karp(){\r\n    ans = 0;\r\n    while (find_path())\r\n        add_path();\r\n}\r\n\r\n\r\n\r\nint extract(){\r\n    int i, k;\r\n    for (i=0;V[i];i++); k = i;\r\n    for (i++;i<n;i++) if (!V[i] &#038;&#038; D[i]<D[k]) k = i;\r\n    V[k] = true;\r\n    return k;\r\n}\r\n\r\nvoid relax(){\r\n    D[v] = min(D[v], D[u] + G[u][v]);\r\n}\r\n\r\nvoid dijkstra(int s){\r\n    memset(V, false, sizeof(V));\r\n    memset(D, 0x7f, sizeof(D));\r\n    D[s] = 0;\r\n    for (int i=0;i<n;i++){\r\n        u = extract();  \/\/ \u201c\u3002\u3002\u3002\u3002\u770b\u6765\u3002\u3002\u3002\u53ea\u6709\u8fd9\u4e48\u89e3\u91ca\u3002\u3002\u3002\u4ece t \u70b9\u5230\u522b\u7684\u70b9\u8fd8\u6709\u4ee3\u4ef7\u4e3a 0 \u7684\u8fb9\u3002\u3002\u3002\u201d\r\n        \/\/if (D[u] == INF || u == t) break;\r\n        if (D[u] == INF) break;\r\n        \r\n        for (v=0;v<n;v++)\r\n            if (!V[v] &#038;&#038; G[u][v]!=-1) relax();\r\n    }\r\n}\r\n\r\nvoid building_the_graph(){\r\n    memset(V, false, sizeof(V));\r\n    memset(D, 0x7f, sizeof(D));\r\n    dijkstra(s);\r\n\r\n    for (int i=0;i<n;i++){\r\n        if (D[i]==INF) continue;\r\n        for (int j=0;j<n;j++){\r\n            if (D[j]==INF) continue;\r\n            C[i][j] = (G[i][j]!=-1 &#038;&#038; D[j] == D[i] + G[i][j]);\r\n        }\r\n    }\r\n\r\n\r\n\/*\r\n    for (int k=0;k<n;k++)\r\n        for (int i=0;i<n;i++)\r\n            for (int j=0;j<n;j++)\r\n                C[i][j] = C[i][j] || C[i][k] &#038;&#038; C[k][j]; *\/\r\n\r\n}\r\n\r\nvoid init(){\r\n    memset(G, 0, sizeof(G));\r\n    for (int i=0;i<n;i++)\r\n        for (int j=0;j<n;j++){\r\n            scanf(\"%d\", &#038;G[i][j]);\r\n            if(i == j) G[i][j] = 0;\r\n        }\r\n    scanf(\"%d%d\", &#038;s, &#038;t);\r\n}\r\n\r\n\r\n\r\nint main(){\r\n    while (scanf(\"%d\", &#038;n) == 1){\r\n        init();\r\n        if (s == t) printf(\"inf\\n\");\r\n        else {\r\n            building_the_graph(); Edmonds_Karp();\r\n            printf(\"%d\\n\", ans);\r\n        }\r\n    }\r\n}\r\n\r\n<\/pre>\n<p>...<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : \u6c42\u4ece\u6e90\u70b9\u5230\u7ec8\u70b9\uff0c\u4e92\u4e0d\u76f8\u4ea4\u7684\u6700\u77ed\u8def\u5f84\u7684\u6700\u5927\u503c\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-123","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-1Z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/123","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=123"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}