{"id":346,"date":"2017-02-19T09:06:15","date_gmt":"2017-02-19T01:06:15","guid":{"rendered":"http:\/\/kylelv.com\/?p=346"},"modified":"2017-12-25T20:40:02","modified_gmt":"2017-12-25T12:40:02","slug":"bzoj-1715-usaco2006-decwormholes-%e8%99%ab%e6%b4%9e-spfa%e5%88%a4%e6%96%ad%e8%b4%9f%e7%8e%af","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=346","title":{"rendered":"bzoj 1715: [Usaco2006 Dec]Wormholes \u866b\u6d1e  &#8212; spfa\u5224\u65ad\u8d1f\u73af"},"content":{"rendered":"<p>&nbsp;<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_73 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fdef449f5ed\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fdef449f5ed\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.kylelv.com\/?p=346\/#1715_Usaco2006_DecWormholes_%E8%99%AB%E6%B4%9E\" title=\"1715: [Usaco2006 Dec]Wormholes \u866b\u6d1e\">1715: [Usaco2006 Dec]Wormholes \u866b\u6d1e<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.kylelv.com\/?p=346\/#Description\" title=\"Description\">Description<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.kylelv.com\/?p=346\/#Input\" title=\"Input\">Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.kylelv.com\/?p=346\/#Output\" title=\"Output\">Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.kylelv.com\/?p=346\/#Sample_Input\" title=\"Sample Input\">Sample Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.kylelv.com\/?p=346\/#Sample_Output\" title=\"Sample Output\">Sample Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.kylelv.com\/?p=346\/#HINT\" title=\"HINT\">HINT<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.kylelv.com\/?p=346\/#Source\" title=\"Source\">Source<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"1715_Usaco2006_DecWormholes_%E8%99%AB%E6%B4%9E\"><\/span>1715: [Usaco2006 Dec]Wormholes \u866b\u6d1e<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\"><span class=\"green\">Time Limit:\u00a05 Sec\u00a0\u00a0Memory Limit:\u00a064 MB<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Description\"><\/span>Description<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>John\u5728\u4ed6\u7684\u519c\u573a\u4e2d\u95f2\u901b\u65f6\u53d1\u73b0\u4e86\u8bb8\u591a\u866b\u6d1e\u3002\u866b\u6d1e\u53ef\u4ee5\u770b\u4f5c\u4e00\u6761\u5341\u5206\u5947\u7279\u7684\u6709\u5411\u8fb9\uff0c\u5e76\u53ef\u4ee5\u4f7f\u4f60\u8fd4\u56de\u5230\u8fc7\u53bb\u7684\u4e00\u4e2a\u65f6\u523b\uff08\u76f8\u5bf9\u4f60\u8fdb\u5165\u866b\u6d1e\u4e4b\u524d\uff09\u3002John\u7684\u6bcf\u4e2a\u519c\u573a\u6709M\u6761\u5c0f\u8def\uff08\u65e0\u5411\u8fb9)\u8fde\u63a5\u7740N \uff08\u4ece1..N\u6807\u53f7\uff09\u5757\u5730\uff0c\u5e76\u6709W\u4e2a\u866b\u6d1e\u3002\u5176\u4e2d1&lt;=N&lt;=500,1&lt;=M&lt;=2500,1&lt;=W&lt;=200\u3002 \u73b0\u5728John\u60f3\u501f\u52a9\u8fd9\u4e9b\u866b\u6d1e\u6765\u56de\u5230\u8fc7\u53bb\uff08\u51fa\u53d1\u65f6\u523b\u4e4b\u524d\uff09\uff0c\u8bf7\u4f60\u544a\u8bc9\u4ed6\u80fd\u529e\u5230\u5417\u3002 John\u5c06\u5411\u4f60\u63d0\u4f9bF(1&lt;=F&lt;=5)\u4e2a\u519c\u573a\u7684\u5730\u56fe\u3002\u6ca1\u6709\u5c0f\u8def\u4f1a\u8017\u8d39\u4f60\u8d85\u8fc710000\u79d2\u7684\u65f6\u95f4\uff0c\u5f53\u7136\u4e5f\u6ca1\u6709\u866b\u6d1e\u56de\u5e2e\u4f60\u56de\u5230\u8d85\u8fc710000\u79d2\u4ee5\u524d\u3002<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Input\"><\/span>Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>* Line 1: \u4e00\u4e2a\u6574\u6570 F, \u8868\u793a\u519c\u573a\u4e2a\u6570\u3002<\/p>\n<p>* Line 1 of each farm: \u4e09\u4e2a\u6574\u6570 N, M, W\u3002<\/p>\n<p>* Lines 2..M+1 of each farm: \u4e09\u4e2a\u6570\uff08S, E, T\uff09\u3002\u8868\u793a\u5728\u6807\u53f7\u4e3aS\u7684\u5730\u4e0e\u6807\u53f7\u4e3aE\u7684\u5730\u4e2d\u95f4\u6709\u4e00\u6761\u7528\u65f6T\u79d2\u7684\u5c0f\u8def\u3002<\/p>\n<p>* Lines M+2..M+W+1 of each farm: \u4e09\u4e2a\u6570\uff08S, E, T\uff09\u3002\u8868\u793a\u5728\u6807\u53f7\u4e3aS\u7684\u5730\u4e0e\u6807\u53f7\u4e3aE\u7684\u5730\u4e2d\u95f4\u6709\u4e00\u6761\u53ef\u4ee5\u4f7fJohn\u5230\u8fbeT\u79d2\u524d\u7684\u866b\u6d1e\u3002<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>* Lines 1..F: \u5982\u679cJohn\u80fd\u5728\u8fd9\u4e2a\u519c\u573a\u5b9e\u73b0\u4ed6\u7684\u76ee\u6807\uff0c\u8f93\u51fa&#8221;YES&#8221;,\u5426\u5219\u8f93\u51fa&#8221;NO&#8221;\u3002<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Input\"><\/span>Sample Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\"><span class=\"sampledata\"><span class=\"sampledata\">2<br \/>\n3 3 1<br \/>\n1 2 2<br \/>\n1 3 4<br \/>\n2 3 1<br \/>\n3 1 3<br \/>\n3 2 1<br \/>\n1 2 3<br \/>\n2 3 4<br \/>\n3 1 8<\/span><\/span>&nbsp;<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Output\"><\/span>Sample Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\"><span class=\"sampledata\">NO<br \/>\nYES<br \/>\n<\/span><\/div>\n<h2><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\"><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Source\"><\/span>Source<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u6ce8\u610f\u7b2c\u4e00\u6b21\u52a0\u8fb9\u662f\u53cc\u5411\u8fb9\u7b2c\u4e8c\u6b21\u662f\u5355\u5411\u8fb9\uff0c\u5e76\u4e14\u6bcf\u6b21\u8be2\u95ee\u524d\u6570\u7ec4\u8981\u6e05\u7a7a<\/p>\n<pre class=\"lang:c++ decode:true \">#include&lt;queue&gt;\r\n#include&lt;cstdio&gt;\r\n#include&lt;cstring&gt;\r\n#include&lt;iostream&gt;\r\nusing namespace std;\r\n#define N 510\r\ninline int read()\r\n{\r\n    int x=0;char ch=getchar();\r\n    while(ch&lt;'0'||ch&gt;'9') ch=getchar();\r\n    while(ch&gt;='0'&amp;&amp;ch&lt;='9'){x=x*10+ch-'0';ch=getchar();}\r\n    return x;\r\n}\r\nstruct qaz{int fro,to,v;}e[100010];\r\nint f,n,m,w,S,E,V,cnt,lj[N],dis[N],sum[N];\r\nbool used[N];\r\nvoid add(int a,int b,int c){cnt++;e[cnt].fro=lj[a];e[cnt].to=b;e[cnt].v=c;lj[a]=cnt;}\r\nqueue&lt;int&gt;q;\r\nbool spfa()\r\n{\r\n    memset(dis,127\/3,sizeof(dis));\r\n    memset(used,0,sizeof(used));\r\n    memset(sum,0,sizeof(sum));\r\n    q.push(1);\r\n    used[1]=1;\r\n    dis[1]=0;\r\n    while(!q.empty())\r\n    {\r\n        int t=q.front();q.pop();\r\n        used[t]=0;\r\n        for(int i=lj[t];i;i=e[i].fro)\r\n        {\r\n            int et=e[i].to;\r\n            if(e[i].v+dis[t]&lt;dis[et])\r\n            {\r\n                sum[et]++;\r\n                if(sum[et]&gt;n) return 1;\r\n                dis[et]=e[i].v+dis[t];\r\n                if(!used[et])\r\n                {\r\n                    q.push(et);\r\n                    used[et]=1;\r\n                }\r\n            }\r\n        }\r\n    }\r\n    return 0;\r\n}\r\nint main()\r\n{\r\n    f=read();\r\n    while(f--)\r\n    {\r\n        n=read();m=read();w=read();\r\n        memset(lj,0,sizeof(lj));\r\n        for(int i=0;i&lt;m;i++)\r\n        {\r\n            S=read();E=read();V=read();\r\n            add(S,E,V);add(E,S,V);\r\n        }\r\n        for(int i=0;i&lt;w;i++)\r\n        {\r\n            S=read();E=read();V=read();\r\n            add(S,E,-V);\r\n        }\r\n        if(spfa()) puts(\"YES\");\r\n        else puts(\"NO\");\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 1715: [Usaco2006 Dec]Wormholes \u866b\u6d1e Time Limit:\u00a05  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[23],"class_list":["post-346","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-spfa"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=346"}],"version-history":[{"count":2,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/346\/revisions"}],"predecessor-version":[{"id":350,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/346\/revisions\/350"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}