{"id":137,"date":"2010-08-04T08:40:52","date_gmt":"2010-08-04T00:40:52","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=137"},"modified":"2012-03-03T08:41:04","modified_gmt":"2012-03-03T00:41:04","slug":"hoj-2853-happy-value","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/hoj-2853-happy-value\/","title":{"rendered":"HOJ 2853. Happy Value"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5355\u5411\u56fe\uff0c\u8fb9\u6743\u8bb0\u505a G[i][j]\uff0c\u7ec8\u70b9 n \u5904\u6709\u4e00\u4e2a\u9b54\u738b\u3002<br \/>\n\u82f1\u96c4\u8981\u4ece\u51fa\u53d1\u70b91\u5f00\u59cb\uff0c\u7ecf\u8fc7\u8ff7\u5bab\uff0c\u6bcf\u901a\u8fc7\u4e00\u6761\u8fb9\u81ea\u5df1\u7684\u751f\u547d\u503c\u4f1a\u53d1\u751f\u6539\u53d8\u3002<br \/>\n\uff08\u8ba1\u7b97\u7684\u516c\u5f0f\u662f Hp = (Hp*2)+G[i][j]\uff0c\u751f\u547d<=0 \u65f6\u88ab\u7b97\u505a\u662f\u6b7b\u4ea1\u3002\uff09\n\u6c42\u7684\u8981\u78b0\u5230\u9b54\u738b\uff0c\u82f1\u96c4\u6240\u9700\u8981\u7684\u6700\u5c0f\u521d\u59cb\u751f\u547d\u503c\u3002\n<!--more--><\/p>\n<h3>Analyse :<\/h3>\n<p>\u4e24\u79cd\u89e3\u6cd5\uff0c\u5206\u522b\u662f\u4ece\u6b63\u9762\u4e8c\u5206\u7b54\u6848\u641c\u7d22\uff0c\u548c\u4e00\u79cd\u4ece\u7ec8\u70b9\u5c06\u6574\u4e2a\u8fc7\u7a0b\u9006\u5411\u8fdb\u884c\u7684\u65b9\u6cd5\uff0c\u4e24\u79cd\u65b9\u6cd5\u5404\u6709\u5229\u5f0a\uff0c\u4e0d\u8fc7\u6211\u8ba4\u4e3a\u6807\u7a0b\u662f\u540e\u8005\uff0c\u6240\u4ee5<a href=\"http:\/\/acm.hit.edu.cn\/judge\/overview.php?Contestid=215\">\u6bd4\u8d5b\u7684\u65f6\u5019<\/a>\u5199\u7684\u4e5f\u662f\u3002<br \/>\n\u6ca1\u6709\u6ce8\u610f\u5230double\u7684\u95ee\u9898\uff0c\u800c\u4e14\u521d\u59cb\u503c\u8d4b\u7684\u4e5f\u4e0d\u5bf9\u4e00\u76f4WA\u5230\u6bd4\u8d5b\u7ed3\u675f\u3002<br \/>\n\uff08\u5e94\u8be5\u8d4b\u503c\u4e3a 0 \u7136\u540e\u5728\u8ba1\u7b97\u51fa 3 \u7684\u65f6\u5019\u6270\u52a8\u4e00\u4e0b&#8230; \u800c\u4e4b\u524d\u6211\u8d4b\u503c\u4e3a 1 &#8230;\uff09<br \/>\n\u9006\u5411\u5199\u7684\u65f6\u5019\u8fd8\u4e0d\u5f97\u4e0d\u8003\u8651\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u5982\u679c\u4e2d\u9014\u53d1\u751f\u6b7b\u4ea1\u4e8b\u4ef6\u7684\u5904\u7406\u3002\u3002<br \/>\n\u8bf7\u53c2\u8003 Relax \u7684\u5730\u65b9\u3002\uff08\u8fd9\u4e2a\u5730\u65b9\u503c\u5f97\u4ed4\u7ec6\u601d\u8003\u5440\u3002\uff09<\/p>\n<p>\u5bf9\u4e8e\u5177\u4f53\u641c\u7d22\u7684\u8fc7\u7a0b\u4e5f\u6709\u8bb8\u591a\u65b9\u6cd5\u53ef\u4ee5\u5199\uff0c\u4ece\u666e\u901a\u7684\u641c\u7d22\uff0c\u6216\u8005\u7528\u8fb9\u76ee\u5f55\u5b58\u7684Bellman\uff0c\u90fd\u662f\u53ef\u8fc7\u7684\u3002\u5bf9\u4e8e\u4e8c\u5206\u7b54\u6848\u7684\u65b9\u6cd5\uff0c\u8003\u8651\u5230\u8fd9\u4e2a\u5b50\u7a0b\u5e8f\u4f1a\u53cd\u590d\u8c03\u7528\uff0c\u53e6\u4e00\u79cd\u66f4\u597d\u7684\u5199\u65b9\u662f\u5148\u5c06\u6574\u4e2a\u56fe\u5148\u9884\u5904\u7406\u5efa\u7acb\u51fa Dag \u94fe\uff0c<br \/>\n\u7136\u540e\u6267\u884c\u52a8\u6001\u89c4\u5212\u3002\uff08\u8fd9\u4e2a\u65b9\u6cd5\u6211\u662f\u5b66\u4e60 Xiaodai \u7684\u3002^ ^\uff09<\/p>\n<p>\u4e0b\u9762\u7684\u4e24\u4efd\u4ee3\u7801\u90fd\u662f\u7528Bellman\u7684\u3002\u3002\u3002<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n#include <vector>\r\n#include <cmath>\r\nusing namespace std;\r\nconst double INF = 1e10;\r\nconst int N = 100;\r\nstruct edge{\r\n    int a, b;\r\n    int w;\r\n}; vector<edge> E;\r\nint G[N][N]; double D[N];\r\nint n;\r\n\r\n\r\nvoid relax(int u, int v){\r\n    D[v] = max(D[v], D[u] * 2 + G[u][v]);\r\n}\r\n\r\nbool check(int x){\r\n    int i, j;\r\n    for (i=1;i<n;i++)\r\n        D[i] = -INF;\r\n    D[0] = x;\r\n    \/\/Bellman_ford;\r\n    int u, v;\r\n    for (i=0;i<n;i++){\r\n        for (j=0;j<E.size();j++){\r\n            if (D[E[j].a]<=0) continue;\r\n                relax(E[j].a, E[j].b);\r\n        }\r\n    }\r\n\r\n    return D[n-1]>0;\r\n}\r\n\r\nvoid init(){\r\n    E.clear(); edge x;\r\n    for (int i=0;i<n;i++)\r\n        for (int j=0;j<n;j++){\r\n            cin >> G[i][j]; if (G[i][j]==0) continue;\r\n                x.a = i; x.b = j;\r\n                E.push_back(x);\r\n            }\r\n}\r\n\r\n\r\nint main(){\r\n    while (cin >> n){\r\n        init(); int l = 1, r = 1e9, m;\r\n        while (l<r){\r\n            m = (l + r) \/ 2;\r\n            if (check(m)) r = m; else l = m+1;\r\n        }\r\n        cout << l << endl;\r\n    }\r\n}\r\n<\/pre>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n#include <vector>\r\n#include <cmath>\r\nusing namespace std;\r\nconst double INF = 1e10;\r\nconst int N = 100;\r\nstruct edge{\r\n    int a, b;\r\n    int w;\r\n}; vector<edge> E;\r\nint G[N][N];\r\ndouble D[N];\r\nint n;\r\n\r\n\r\nvoid relax(int u, int v){\r\n    double w = (D[v]-G[u][v])\/2;\r\n    D[u] = min(D[u], w); D[u] = max(D[u], .0);\r\n}\r\n\r\nvoid bellman_ford(){\r\n    int i, j;\r\n    for (i=0;i<n-1;i++)\r\n        D[i] = INF;\r\n    D[n-1] = 0;\r\n\r\n    int u, v;\r\n    for (i=0;i<n;i++){\r\n        for (j=0;j<E.size();j++){\r\n            if (D[E[j].b]==INF) continue;\r\n                relax(E[j].a, E[j].b);\r\n        }\r\n    }\r\n}\r\n\r\nvoid init(){\r\n    E.clear(); edge x;\r\n    for (int i=0;i<n;i++)\r\n        for (int j=0;j<n;j++){\r\n            cin >> G[i][j]; if (G[i][j]==0) continue;\r\n                x.a = i; x.b = j;\r\n                E.push_back(x);\r\n            }\r\n}\r\n\r\n\r\nint main(){\r\n    while (cin >> n){\r\n        init(); bellman_ford();\r\n        cout << ceil(D[0]+1e-10) << endl;\r\n    }\r\n}\r\n<\/pre>\n<p><bk><br \/>\n<bk><br \/>\n<bk><\/p>\n<h3>External link :<\/h3>\n<p>http:\/\/acm.hit.edu.cn\/judge\/show.php?Proid=2853<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : \u7ed9\u5b9a\u4e00\u4e2a\u5355\u5411\u56fe\uff0c\u8fb9\u6743\u8bb0\u505a G[i][j]\uff0c\u7ec8\u70b9 n \u5904\u6709\u4e00\u4e2a\u9b54\u738b\u3002 \u82f1\u96c4\u8981\u4ece\u51fa\u53d1\u70b91\u5f00\u59cb\uff0c\u7ecf\u8fc7\u8ff7\u5bab\uff0c\u6bcf\u901a\u8fc7\u4e00\u6761\u8fb9\u81ea\u5df1\u7684\u751f\u547d\u503c\u4f1a\u53d1\u751f\u6539\u53d8\u3002 \uff08\u8ba1\u7b97\u7684\u516c\u5f0f\u662f Hp = (Hp*2)+G[i][j]\uff0c\u751f\u547d<\/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-137","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-2d","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/137","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=137"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}