{"id":78,"date":"2011-03-17T07:54:08","date_gmt":"2011-03-16T23:54:08","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=78"},"modified":"2012-03-03T07:54:42","modified_gmt":"2012-03-02T23:54:42","slug":"spoj-104-highways","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/spoj-104-highways\/","title":{"rendered":"SPOJ 104. Highways"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>\u751f\u6210\u6811\u8ba1\u6570\u95ee\u9898\uff1b\u6c42\u4e00\u4e2a\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b n \u4e2a\u7ed3\u70b9\u7684\u65e0\u5411\u56fe\u751f\u6210\u6811\u7684\u6570\u76ee\u3002<br \/>\n\uff081 <= n <= 12 ...\uff09\n\n<!--more--><\/p>\n<h3>Analyse :<\/h3>\n<p>\u3002\u3002\u8865\u5b8c\u4e86<a href=\"https:\/\/www.shuizilong.com\/house\/archives\/2172\">\u524d\u56de\u4e24\u4e2a\u56de\u8def\u95ee\u9898<\/a>\u7684\u8f6e\u5ed3\u7ebf\u7b97\u6cd5\uff0c\uff08\u4e4b\u524d\u5b8c\u5168\u662f\u6309\u7167\u81ea\u5df1\u81c6\u60f3\u8bbe\u8ba1\u4e86\u4e00\u4e2a\u548c\u8111\u6d77\u91cc\u5df2\u6709\u5370\u8c61\u5dee\u522b\u4e0d\u5927\u7684\u3002\u3002\uff09\u8fd9\u6837\u8bf4\u7740\uff0c\u5e94\u8be5\u8bf4\u8f6e\u5ed3\u7ebf\u662f\u5206\u6790\u4e00\u7c7b\u95ee\u9898\u7684\u601d\u8def\uff0c\u79f0\u4f5c\u5b8c\u5168\u4e0d\u540c\u7684\u7b97\u6cd5\u5e76\u4e0d\u6070\u5f53\uff0c\u5b83\u4eec\u5728\u8ba8\u8bba\u7684\u9636\u6bb5\u4e5f\u5b8c\u5168\u4e00\u81f4\uff0c\u4e0d\u8fc7\u6bd4\u8f83\u4e00\u756a\u4e5f\u6709\u5fc5\u8981\u3002<\/p>\n<p>\u4e0d\u8fc7\u5b66\u4e60\u4e00\u4e0b\u8fd9\u79cd\u7528\u4e24\u5f20\u6563\u5217\u6eda\u52a8\u9012\u63a8\u7684\u5199\u6cd5\u8fd8\u662f\u5f88\u597d\u554a\u554a\u3002@\uff08&#8230;\u5f8c\u8005 Formular \u4e00\u9898\u76f8\u8f83\u5f00\u59cb\u660e\u663e\uff0c\u9010\u884c\u63a8 0.640+ms\uff0c\u8f6e\u5ed3\u7ebf 0.040+ms\uff0c\u554a\u6682\u65f6\u4e0d\u8ba8\u8bba\u8fd9\u4e9b\u4e86&#8230; \uff09<\/p>\n<p>\u5bf9\u4e8e\u8fd9\u9898\uff0c\u65e0\u8bef\u7684\u8bdd\u5e94\u53ef\u72b6\u6001\u538b\u7f29\uff0c\u8003\u8651\u6bcf\u4e2a\u70b9\u662f\u5426\u5728\u5f53\u524d\u7684\u751f\u6210\u6811\u4e2d\uff0c\u4e4b\u5f8c\u2026<br \/>\n\u53d1\u73b0\u4e0d\u884c\uff08\u4f1a\u51fa\u73b0\u91cd\u590d\u8ba1\u6570\uff09\uff0c\u8fdb\u4e00\u6b65\u7ec6\u5316\uff0c\u5bf9\u6bcf\u4e2a\u70b9\u8fdb\u884c\u9ed1\u3001\u767d\u3001\u7070\u67d3\u8272\uff0c\u5982\u540c Prim \u7b97\u6cd5\u8fdb\u884c\u65f6\u90a3\u6837\u3002\u90a3\u4e48\u9012\u63a8\u7684\u8fc7\u7a0b\uff0c\u6bcf\u8f6e\u53d6\u51fa\u6240\u6709\u7070\u8272\u70b9\uff0c\u5e76\u901a\u8fc7\u4e00\u6b21DFS\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a $$O(3^n n^2)$$\u3002<\/p>\n<pre lang=\"cpp\" file=\"SCDP.cpp\">\r\n#include <iostream>\r\n#include <cstdio>\r\n#include <cstring>\r\nusing namespace std;\r\nconst int nn = 12, ss = 531441;\r\nconst int P[nn + 1] = {1,3,9,27,81,243,729,2187,6561,19683,59049,177147,531441};\r\nconst int WHITE = 0, GRAY = 1, BLACK = 2;\r\nint A[ss][nn], D[nn]; bool G[nn][nn];\r\nint n, m; long long ans, d;\r\n\r\n\r\nstruct hashTable{\r\n\tint state[ss], pos[ss], sz;\r\n\tlong long key[ss];\r\n\t\r\n\tinline void clear(){\r\n\t\tsz = 0;\r\n\t\tmemset(pos, -1, sizeof(pos));\r\n\t}\r\n\tinline bool empty(){\r\n\t\treturn sz == 0;\r\n\t}\r\n\tinline void insert(int s){\r\n\t\tif ( pos[s] != -1 ) {\r\n\t\t\tkey[pos[s]] += d;\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tstate[sz] = s, key[sz] = d;\r\n\t\tpos[s] = sz++;\r\n\t}\r\n} H[2] , *src , *des;\r\n\r\nvoid dfs(int s, int i){\r\n\tif (i==n){\r\n\t\tdes->insert(s);\r\n\t}\r\n\telse {\r\n\t\tif (D[i]!=0){\r\n\t\t\td *= D[i], dfs(s+P[i], i+1), d \/= D[i];\r\n\t\t}\r\n\t\tdfs(s, i+1);\r\n\t}\r\n}\r\n\r\n\r\nint main(){\r\n\t\r\n\t\/\/freopen(\"in.txt\", \"r\", stdin);\r\n\t\r\n\tfor (int i = 1; i < ss; i ++){\r\n\t\tfor (int j = 0, s = i; j < nn &#038;&#038; s != 0; j ++)\r\n\t\t\tA[i][j] = s % 3, s \/= 3;\r\n\t}\r\n\t\r\n\tint flag;\r\n\tint T; cin >> T;\r\n\twhile (T--){\r\n\t\tmemset(G, false, sizeof(G));\r\n\t\tcin >> n >> m;\r\n\t\tint x, y;\r\n\t\tfor (int i=0;i<m;i++){\r\n\t\t\tscanf(\"%d%d\", &#038;x, &#038;y); x--, y--;\r\n\t\t\tG[x][y] = G[y][x] = true;\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\tans = 0;\r\n\t\tH[0].clear(), H[1].clear();\r\n\t\tsrc = H, des = src + 1, d = 1, des->insert(1);\r\n\t\t\r\n\t\twhile (!des->empty()){\r\n\t\t\tswap(src, des), des->clear();\r\n\t\t\tfor (int it = 0; it < src->sz; it ++){\r\n\t\t\t\tint s = src->state[it], d = src->key[it];\r\n\t\t\t\t\/\/cout << s << \" \" << d << endl;\r\n\t\t\t\tmemset(D, 0, sizeof(D));\r\n\t\t\t\tflag = BLACK;\r\n\t\t\t\t\r\n\t\t\t\tfor (int i = 0; i < n; i ++)\r\n\t\t\t\t\tif (A[s][i]==GRAY){\r\n\t\t\t\t\t\tfor (int j = 0; j < n; j ++)\r\n\t\t\t\t\t\t\tif (A[s][j] == WHITE &#038;&#038; G[i][j]) D[j] ++, flag = GRAY;\r\n\t\t\t\t\t\ts += P[i];\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse{\r\n\t\t\t\t\t\tif (flag == BLACK &#038;&#038; A[s][i] == WHITE) flag = WHITE;\r\n\t\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t\t\r\n\t\t\t\tif (flag==GRAY) dfs(s, 0);\r\n\t\t\t\telse if (flag==BLACK) ans += d;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/cout << \"------\" << endl;\r\n\t\t\t\r\n\t\t\t\r\n\t\t}\r\n\t\t\r\n\t\tcout << ans << endl;\t\r\n\t}\r\n\t\r\n}\r\n<\/pre>\n<p><bk><bk><bk><bk><br \/>\n<bk><bk><bk><bk><\/p>\n<p>\u5c45\u7136\u8d85\u65f6\u4e86\u3002\u3002\u3002\u53ea\u597d\u7528 Kirchhoff \u77e9\u9635\u4e86\u3002\u3002<\/p>\n<pre lang=\"cpp\" file=\"Kirchhoff_Matrix.cpp\">\r\n#include <iostream>\r\n#include <cstdio>\r\n#include <cstring>\r\nusing namespace std;\r\nconst double ESP = 1e-15;\r\nconst int nn = 12;\r\ndouble K[nn][nn]; int A[nn][nn], D[nn][nn];\r\nint n, m; \r\n\r\nbool isZero(double x){\r\n\treturn (-ESP <= x &#038;&#038; x <= ESP);\r\n}\r\n\r\ndouble det(){\r\n\tdouble key, det = 1;\r\n\tint i, j, k;\r\n\tfor (i=0;i<n;i++){\r\n\t\tif (isZero(K[i][i])){\r\n\t\t\tfor (j=i+1;j<n;j++){\r\n\t\t\t\tif (isZero(K[j][i])) continue;\r\n\t\t\t\tfor (int k=i;k<n;k++)\r\n\t\t\t\t\tswap(K[i][k], K[j][k]);\r\n\t\t\t\tdet = -det;\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tif (j==n){\r\n\t\t\t\treturn 0;\r\n\t\t\t\t\/\/cout << \"WARNING!\" << endl;\r\n\t\t\t\t\/\/exit(0);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tkey = K[i][i]; K[i][i] = 1;\r\n\t\tfor (j=i+1;j<n;j++)\r\n\t\t\tK[i][j] \/=  key;\r\n\t\tdet *= key;\r\n\t\t\r\n\t\tfor (j=0;j<n;j++){\r\n\t\t\tif (i==j||isZero(K[j][i])) continue;\r\n\t\t\tkey = K[j][i], K[j][i] = 0;\r\n\t\t\tfor (k=i+1;k<n;k++)\r\n\t\t\t\tK[j][k] -= K[i][k] * key;\r\n\t\t}\r\n\t}\r\n\treturn det;\r\n}\r\n\r\nint main(){\r\n\t\r\n\t\/\/freopen(\"in.txt\", \"r\", stdin);\r\n\tint T; cin >> T;\r\n\twhile (T--){\r\n\t\tmemset(A, 0, sizeof(A));\r\n\t\tmemset(D, 0, sizeof(D));\r\n\t\tcin >> n >> m;\r\n\t\t\r\n\t\tint x, y;\r\n\t\tfor (int i=0;i<m;i++){\r\n\t\t\tscanf(\"%d%d\", &#038;x, &#038;y); x--, y--;\r\n\t\t\tA[x][y] = A[y][x] = 1; D[x][x] ++, D[y][y] ++;\r\n\t\t}\r\n\t\t\r\n\t\t\/\/Kirchhoff\u77e9\u9663\u751f\u6210\u3059\u308b...\r\n\t\tn--;\r\n\t\tfor (int i=0;i<n;i++)\r\n\t\t\tfor (int j=0;j<n;j++)\r\n\t\t\t\tK[i][j] = D[i][j] - A[i][j];\r\n\t\t\r\n\t\t\/\/..\u884c\u5217\u5f0f\u3092\u8a08\u7b97\u3059\u308b..\t\r\n\t\tcout << det() << endl;\r\n\t}\r\n}\r\n<\/pre>\n<p><bk><bk><bk><bk><br \/>\n<bk><bk><bk><bk><\/p>\n<h3>External link :<\/h3>\n<p><a href=\"https:\/\/www.spoj.pl\/problems\/HIGH\/\">https:\/\/www.spoj.pl\/problems\/HIGH\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : \u751f\u6210\u6811\u8ba1\u6570\u95ee\u9898\uff1b\u6c42\u4e00\u4e2a\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b n \u4e2a\u7ed3\u70b9\u7684\u65e0\u5411\u56fe\u751f\u6210\u6811\u7684\u6570\u76ee\u3002 \uff081<\/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-78","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-1g","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/78","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=78"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}