{"id":323,"date":"2017-01-10T22:58:42","date_gmt":"2017-01-10T14:58:42","guid":{"rendered":"http:\/\/kylelv.com\/?p=323"},"modified":"2017-12-22T20:06:45","modified_gmt":"2017-12-22T12:06:45","slug":"bzoj-3669-noi2014%e9%ad%94%e6%b3%95%e6%a3%ae%e6%9e%97-%e5%8a%a8%e7%82%b9spfa","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=323","title":{"rendered":"bzoj 3669: [Noi2014]\u9b54\u6cd5\u68ee\u6797  &#8212; \u52a8\u70b9spfa"},"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-69fdef6d1e057\" 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-69fdef6d1e057\"  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=323\/#3669_Noi2014%E9%AD%94%E6%B3%95%E6%A3%AE%E6%9E%97\" title=\"3669: [Noi2014]\u9b54\u6cd5\u68ee\u6797\">3669: [Noi2014]\u9b54\u6cd5\u68ee\u6797<\/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=323\/#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=323\/#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=323\/#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=323\/#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=323\/#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=323\/#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=323\/#Source\" title=\"Source\">Source<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blog.kylelv.com\/?p=323\/#%E5%8A%A8%E7%82%B9spfa\" title=\"\u52a8\u70b9spfa\">\u52a8\u70b9spfa<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"3669_Noi2014%E9%AD%94%E6%B3%95%E6%A3%AE%E6%9E%97\"><\/span>3669: [Noi2014]\u9b54\u6cd5\u68ee\u6797<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\"><span class=\"green\">Time Limit:\u00a030 Sec\u00a0\u00a0Memory Limit:\u00a0512 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 class=\"p18\">\u4e3a\u4e86\u5f97\u5230\u4e66\u6cd5\u5927\u5bb6\u7684\u771f\u4f20\uff0c\u5c0fE\u540c\u5b66\u4e0b\u5b9a\u51b3\u5fc3\u53bb\u62dc\u8bbf\u4f4f\u5728\u9b54\u6cd5\u68ee\u6797\u4e2d\u7684\u9690\u58eb\u3002\u9b54\u6cd5\u68ee\u6797\u53ef\u4ee5\u88ab\u770b\u6210\u4e00\u4e2a\u5305\u542b\u4e2aN\u8282\u70b9M\u6761\u8fb9\u7684\u65e0\u5411\u56fe\uff0c\u8282\u70b9\u6807\u53f7\u4e3a1..N\uff0c\u8fb9\u6807\u53f7\u4e3a1..M\u3002\u521d\u59cb\u65f6\u5c0fE\u540c\u5b66\u5728\u53f7\u8282\u70b91\uff0c\u9690\u58eb\u5219\u4f4f\u5728\u53f7\u8282\u70b9N\u3002\u5c0fE\u9700\u8981\u901a\u8fc7\u8fd9\u4e00\u7247\u9b54\u6cd5\u68ee\u6797\uff0c\u624d\u80fd\u591f\u62dc\u8bbf\u5230\u9690\u58eb\u3002<\/p>\n<p class=\"p18\">\u9b54\u6cd5\u68ee\u6797\u4e2d\u5c45\u4f4f\u4e86\u4e00\u4e9b\u5996\u602a\u3002\u6bcf\u5f53\u6709\u4eba\u7ecf\u8fc7\u4e00\u6761\u8fb9\u7684\u65f6\u5019\uff0c\u8fd9\u6761\u8fb9\u4e0a\u7684\u5996\u602a\u5c31\u4f1a\u5bf9\u5176\u53d1\u8d77\u653b\u51fb\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5728\u53f7\u8282\u70b9\u4f4f\u7740\u4e24\u79cd\u5b88\u62a4\u7cbe\u7075\uff1aA\u578b\u5b88\u62a4\u7cbe\u7075\u4e0eB\u578b\u5b88\u62a4\u7cbe\u7075\u3002\u5c0fE\u53ef\u4ee5\u501f\u52a9\u5b83\u4eec\u7684\u529b\u91cf\uff0c\u8fbe\u5230\u81ea\u5df1\u7684\u76ee\u7684\u3002<\/p>\n<p class=\"p18\">\u53ea\u8981\u5c0fE\u5e26\u4e0a\u8db3\u591f\u591a\u7684\u5b88\u62a4\u7cbe\u7075\uff0c\u5996\u602a\u4eec\u5c31\u4e0d\u4f1a\u53d1\u8d77\u653b\u51fb\u4e86\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u65e0\u5411\u56fe\u4e2d\u7684\u6bcf\u4e00\u6761\u8fb9Ei\u5305\u542b\u4e24\u4e2a\u6743\u503cAi\u4e0eBi\u3002\u82e5\u8eab\u4e0a\u643a\u5e26\u7684A\u578b\u5b88\u62a4\u7cbe\u7075\u4e2a\u6570<span class=\"15\">\u4e0d\u5c11\u4e8eAi\uff0c\u4e14B\u578b\u5b88\u62a4\u7cbe\u7075\u4e2a\u6570\u4e0d\u5c11\u4e8eBi\uff0c\u8fd9\u6761\u8fb9\u4e0a\u7684\u5996\u602a\u5c31\u4e0d\u4f1a\u5bf9\u901a\u8fc7\u8fd9\u6761\u8fb9\u7684\u4eba\u53d1\u8d77\u653b\u51fb\u3002\u5f53\u4e14\u4ec5\u5f53\u901a\u8fc7\u8fd9\u7247\u9b54\u6cd5\u68ee\u6797\u7684\u8fc7\u7a0b\u4e2d\u6ca1\u6709\u4efb\u610f\u4e00\u6761\u8fb9\u7684\u5996\u602a\u5411\u5c0fE\u53d1\u8d77\u653b\u51fb\uff0c\u4ed6\u624d\u80fd\u6210\u529f\u627e\u5230\u9690\u58eb\u3002<\/span><\/p>\n<p class=\"p17\">\u7531\u4e8e\u643a\u5e26\u5b88\u62a4\u7cbe\u7075\u662f\u4e00\u4ef6\u975e\u5e38\u9ebb\u70e6\u7684\u4e8b\uff0c\u5c0fE\u60f3\u8981\u77e5\u9053\uff0c\u8981\u80fd\u591f\u6210\u529f\u62dc\u8bbf\u5230\u9690\u58eb\uff0c\u6700\u5c11\u9700\u8981\u643a\u5e26\u5b88\u62a4\u7cbe\u7075\u7684\u603b\u4e2a\u6570\u3002\u5b88\u62a4\u7cbe\u7075\u7684\u603b\u4e2a\u6570\u4e3aA\u578b\u5b88\u62a4\u7cbe\u7075\u7684\u4e2a\u6570\u4e0eB\u578b\u5b88\u62a4\u7cbe\u7075\u7684\u4e2a\u6570\u4e4b\u548c\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>\u7b2c1\u884c\u5305\u542b\u4e24\u4e2a\u6574\u6570N,M\uff0c\u8868\u793a\u65e0\u5411\u56fe\u5171\u6709N\u4e2a\u8282\u70b9\uff0cM\u6761\u8fb9\u3002 \u63a5\u4e0b\u6765M\u884c\uff0c\u7b2c\u884c\u5305\u542b4\u4e2a\u6b63\u6574\u6570Xi,Yi,Ai,Bi\uff0c\u63cf\u8ff0\u7b2ci\u6761\u65e0\u5411\u8fb9\u3002\u5176\u4e2dXi\u4e0eYi\u4e3a\u8be5\u8fb9\u4e24\u4e2a\u7aef\u70b9\u7684\u6807\u53f7\uff0cAi\u4e0eBi\u7684\u542b\u4e49\u5982\u9898\u6240\u8ff0\u3002 \u6ce8\u610f\u6570\u636e\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u8fb9\u4e0e\u81ea\u73af\u3002<\/p>\n<p>&nbsp;<\/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>\u8f93\u51fa\u4e00\u884c\u4e00\u4e2a\u6574\u6570\uff1a\u5982\u679c\u5c0fE\u53ef\u4ee5\u6210\u529f\u62dc\u8bbf\u5230\u9690\u58eb\uff0c\u8f93\u51fa\u5c0fE\u6700\u5c11\u9700\u8981\u643a\u5e26\u7684\u5b88\u62a4\u7cbe\u7075\u7684\u603b\u4e2a\u6570\uff1b\u5982\u679c\u65e0\u8bba\u5982\u4f55\u5c0fE\u90fd\u65e0\u6cd5\u62dc\u8bbf\u5230\u9690\u58eb\uff0c\u8f93\u51fa\u201c-1\u201d\uff08\u4e0d\u542b\u5f15\u53f7\uff09\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/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\">\u3010\u8f93\u5165\u6837\u4f8b1\u3011<br \/>\n4 5<br \/>\n1 2 19 1<br \/>\n2 3 8 12<br \/>\n2 4 12 15<br \/>\n1 3 17 8<br \/>\n3 4 1 17<\/span><\/span>\u3010\u8f93\u5165\u6837\u4f8b2\u3011<\/p>\n<p>3 1<br \/>\n1 2 1 1<\/p>\n<p>&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\">\n<p><span class=\"sampledata\">\u3010\u8f93\u51fa\u6837\u4f8b1\u3011<\/span><\/p>\n<p>32<br \/>\n\u3010\u6837\u4f8b\u8bf4\u660e1\u3011<br \/>\n\u5982\u679c\u5c0fE\u8d70\u8def\u5f841\u21922\u21924\uff0c\u9700\u8981\u643a\u5e2619+15=34\u4e2a\u5b88\u62a4\u7cbe\u7075\uff1b<br \/>\n\u5982\u679c\u5c0fE\u8d70\u8def\u5f841\u21923\u21924\uff0c\u9700\u8981\u643a\u5e2617+17=34\u4e2a\u5b88\u62a4\u7cbe\u7075\uff1b<br \/>\n\u5982\u679c\u5c0fE\u8d70\u8def\u5f841\u21922\u21923\u21924\uff0c\u9700\u8981\u643a\u5e2619+17=36\u4e2a\u5b88\u62a4\u7cbe\u7075\uff1b<br \/>\n\u5982\u679c\u5c0fE\u8d70\u8def\u5f841\u21923\u21922\u21924\uff0c\u9700\u8981\u643a\u5e2617+15=32\u4e2a\u5b88\u62a4\u7cbe\u7075\u3002<br \/>\n\u7efc\u4e0a\u6240\u8ff0\uff0c\u5c0fE\u6700\u5c11\u9700\u8981\u643a\u5e2632\u4e2a\u5b88\u62a4\u7cbe\u7075\u3002<\/p>\n<p>\u3010\u8f93\u51fa\u6837\u4f8b2\u3011<\/p>\n<p>-1<br \/>\n\u3010\u6837\u4f8b\u8bf4\u660e2\u3011<br \/>\n\u5c0fE\u65e0\u6cd5\u4ece1\u53f7\u8282\u70b9\u5230\u8fbe3\u53f7\u8282\u70b9\uff0c\u6545\u8f93\u51fa-1\u3002<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>&nbsp;<\/p>\n<p>2&lt;=n&lt;=50,000<\/p>\n<p>0&lt;=m&lt;=100,000<\/p>\n<p>1&lt;=ai ,bi&lt;=50,000<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Source\"><\/span>Source<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h4><span class=\"ez-toc-section\" id=\"%E5%8A%A8%E7%82%B9spfa\"><\/span>\u52a8\u70b9spfa<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre class=\"lang:c++ decode:true \">#include&lt;cstdio&gt;\r\n#include&lt;iostream&gt;\r\n#include&lt;algorithm&gt;\r\n#define N 100010\r\n#define inf 1e9+5\r\nusing namespace std;\r\ninline int read()\r\n{\r\n    int x=0,f=1;char ch=getchar();\r\n    while(ch&lt;'0'||ch&gt;'9'){if(ch=='-')f=-1;ch=getchar();}\r\n    while(ch&gt;='0'&amp;&amp;ch&lt;='9'){x=x*10+ch-'0';ch=getchar();}\r\n    return x*f;\r\n}\r\nstruct qaz{int x,y,a,b;}p[N];\r\nbool cmp(qaz a,qaz b)\r\n{\r\n    if(a.a==b.a) return a.b&lt;b.b;\r\n    return a.a&lt;b.a;\r\n}\r\nstruct ljn{int fro,to,w;}e[N&lt;&lt;1];\r\nint lj[N],n,m,h,t,cnt,dis[N],ans=inf,hs[N],q[N*100];\r\ninline void add(int a,int b,int v){cnt++;e[cnt].fro=lj[a];e[cnt].to=b;e[cnt].w=v;lj[a]=cnt;}\r\nint main()\r\n{\r\n    n=read();m=read();\r\n    for(int i=2;i&lt;=n;i++) dis[i]=inf; \r\n    for(int i=1;i&lt;=m;i++){p[i].x=read();p[i].y=read();p[i].a=read();p[i].b=read();}\r\n    sort(p+1,p+m+1,cmp);\r\n    for(int i=1;i&lt;=m;i++)\r\n    {\r\n        int t1=p[i].x,t2=p[i].y,tv=p[i].b,qt;\r\n        add(t1,t2,tv);add(t2,t1,tv);\r\n        h=0;t=2;\r\n        q[0]=t1;q[1]=t2;\r\n        hs[t1]=hs[t2]=i;\r\n        while(h&lt;t)\r\n        {\r\n            qt=q[h];\r\n            for(int j=lj[qt];j;j=e[j].fro)\r\n            {\r\n                if(dis[e[j].to]&gt;max(dis[qt],e[j].w))\r\n                {\r\n                    dis[e[j].to]=max(dis[qt],e[j].w);\r\n                    if(hs[e[j].to]!=i)\r\n                    {\r\n                        q[t++]=e[j].to;\r\n                        hs[e[j].to]=i;\r\n                    }\r\n                }\r\n            }\r\n            hs[q[h++]]=0;\r\n        }\r\n        if(ans&gt;p[i].a+dis[n]) ans=p[i].a+dis[n];\r\n    }\r\n    if(ans==inf) printf(\"-1\");\r\n    else printf(\"%d\\n\",ans);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 3669: [Noi2014]\u9b54\u6cd5\u68ee\u6797 Time Limit:\u00a030 Sec\u00a0\u00a0Memory L [&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,22],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-spfa","tag-22"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/323","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=323"}],"version-history":[{"count":2,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":325,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/323\/revisions\/325"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}