{"id":417,"date":"2017-03-30T16:09:02","date_gmt":"2017-03-30T08:09:02","guid":{"rendered":"http:\/\/kylelv.com\/?p=417"},"modified":"2018-01-02T12:17:17","modified_gmt":"2018-01-02T04:17:17","slug":"bzoj-3931-cqoi2015%e7%bd%91%e7%bb%9c%e5%90%9e%e5%90%90%e9%87%8f-%e6%9c%80%e7%9f%ad%e8%b7%af%e7%bd%91%e7%bb%9c%e6%b5%81","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=417","title":{"rendered":"bzoj 3931: [CQOI2015]\u7f51\u7edc\u541e\u5410\u91cf  &#8212; \u6700\u77ed\u8def+\u7f51\u7edc\u6d41"},"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-69fdef3f919c4\" 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-69fdef3f919c4\"  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=417\/#3931_CQOI2015%E7%BD%91%E7%BB%9C%E5%90%9E%E5%90%90%E9%87%8F\" title=\"3931: [CQOI2015]\u7f51\u7edc\u541e\u5410\u91cf\">3931: [CQOI2015]\u7f51\u7edc\u541e\u5410\u91cf<\/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=417\/#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=417\/#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=417\/#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=417\/#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=417\/#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=417\/#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=417\/#Source\" title=\"Source\">Source<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"3931_CQOI2015%E7%BD%91%E7%BB%9C%E5%90%9E%E5%90%90%E9%87%8F\"><\/span>3931: [CQOI2015]\u7f51\u7edc\u541e\u5410\u91cf<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\"><span class=\"green\">Time Limit:\u00a010 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>\u00a0\u8def\u7531\u662f\u6307\u901a\u8fc7\u8ba1\u7b97\u673a\u7f51\u7edc\u628a\u4fe1\u606f\u4ece\u6e90\u5730\u5740\u4f20\u8f93\u5230\u76ee\u7684\u5730\u5740\u7684\u6d3b\u52a8\uff0c\u4e5f\u662f\u8ba1\u7b97\u673a\u7f51\u7edc\u8bbe\u8ba1\u4e2d\u7684\u91cd\u70b9\u548c\u96be\u70b9\u3002\u7f51\u7edc\u4e2d\u5b9e\u73b0\u8def\u7531\u8f6c\u53d1\u7684\u786c\u4ef6\u8bbe\u5907\u79f0\u4e3a\u8def\u7531\u5668\u3002\u4e3a\u4e86\u4f7f\u6570\u636e\u5305\u6700\u5feb\u7684\u5230\u8fbe\u76ee\u7684\u5730\uff0c\u8def\u7531\u5668\u9700\u8981\u9009\u62e9\u6700\u4f18\u7684\u8def\u5f84\u8f6c\u53d1\u6570\u636e\u5305\u3002\u4f8b\u5982\u5728\u5e38\u7528\u7684\u8def\u7531\u7b97\u6cd5OSPF(\u5f00\u653e\u5f0f\u6700\u77ed\u8def\u5f84\u4f18\u5148)\u4e2d\uff0c\u8def\u7531\u5668\u4f1a\u4f7f\u7528\u7ecf\u5178\u7684Dijkstra\u7b97\u6cd5\u8ba1\u7b97\u6700\u77ed\u8def\u5f84\uff0c\u7136\u540e\u5c3d\u91cf\u6cbf\u6700\u77ed\u8def\u5f84\u8f6c\u53d1\u6570\u636e\u5305\u3002\u73b0\u5728\uff0c\u82e5\u5df2\u77e5\u4e00\u4e2a\u8ba1\u7b97\u673a\u7f51\u7edc\u4e2d\u5404\u8def\u7531\u5668\u95f4\u7684\u8fde\u63a5\u60c5\u51b5\uff0c\u4ee5\u53ca\u5404\u4e2a\u8def\u7531\u5668\u7684\u6700\u5927\u541e\u5410\u91cf\uff08\u5373\u6bcf\u79d2\u80fd\u8f6c\u53d1\u7684\u6570\u636e\u5305\u6570\u91cf\uff09\uff0c\u5047\u8bbe\u6240\u6709\u6570\u636e\u5305\u4e00\u5b9a\u6cbf\u6700\u77ed\u8def\u5f84\u8f6c\u53d1\uff0c\u8bd5\u8ba1\u7b97\u4ece\u8def\u7531\u56681\u5230\u8def\u7531\u5668n\u7684\u7f51\u7edc\u7684\u6700\u5927\u541e\u5410\u91cf\u3002\u8ba1\u7b97\u4e2d\u5ffd\u7565\u8f6c\u53d1\u53ca\u4f20\u8f93\u7684\u65f6\u95f4\u5f00\u9500\uff0c\u4e0d\u8003\u8651\u94fe\u8def\u7684\u5e26\u5bbd\u9650\u5236\uff0c\u5373\u8ba4\u4e3a\u6570\u636e\u5305\u53ef\u4ee5\u77ac\u95f4\u901a\u8fc7\u7f51\u7edc\u3002\u8def\u7531\u56681\u5230\u8def\u7531\u5668n\u4f5c\u4e3a\u8d77\u70b9\u548c\u7ec8\u70b9\uff0c\u81ea\u8eab\u7684\u541e\u5410\u91cf\u4e0d\u7528\u8003\u8651\uff0c\u7f51\u7edc\u4e0a\u4e5f\u4e0d\u5b58\u5728\u5c061\u548cn\u76f4\u63a5\u76f8\u8fde\u7684\u94fe\u8def\u3002<\/p>\n<div><\/div>\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>\u8f93\u5165\u6587\u4ef6\u7b2c\u4e00\u884c\u5305\u542b\u4e24\u4e2a\u7a7a\u683c\u5206\u5f00\u7684\u6b63\u6574\u6570n\u548cm\uff0c\u5206\u522b\u8868\u793a\u8def\u7531\u5668\u6570\u91cf\u548c\u94fe\u8def\u7684\u6570\u91cf\u3002\u7f51\u7edc\u4e2d\u7684\u8def\u7531\u5668\u4f7f\u75281\u5230n\u7f16\u53f7\u3002\u63a5\u4e0b\u6765m\u884c\uff0c\u6bcf\u884c\u5305\u542b\u4e09\u4e2a\u7a7a\u683c\u5206\u5f00\u7684\u6b63\u6574\u6570a\u3001b\u548cd\uff0c\u8868\u793a\u4ece\u8def\u7531\u5668a\u5230\u8def\u7531\u5668b\u5b58\u5728\u4e00\u6761\u8ddd\u79bb\u4e3ad\u7684\u53cc\u5411\u94fe\u8def\u3002 \u63a5\u4e0b\u6765n\u884c\uff0c\u6bcf\u884c\u5305\u542b\u4e00\u4e2a\u6b63\u6574\u6570c\uff0c\u5206\u522b\u7ed9\u51fa\u6bcf\u4e00\u4e2a\u8def\u7531\u5668\u7684\u541e\u5410\u91cf\u3002<\/p>\n<div><\/div>\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\u4e2a\u6574\u6570\uff0c\u4e3a\u9898\u76ee\u6240\u6c42\u541e\u5410\u91cf\u3002<\/p>\n<div><\/div>\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\">7 10<br \/>\n1 2 2<br \/>\n1 5 2<br \/>\n2 4 1<br \/>\n2 3 3<br \/>\n3 7 1<br \/>\n4 5 4<br \/>\n4 3 1<br \/>\n4 6 1<br \/>\n5 6 2<br \/>\n6 7 1<br \/>\n1<br \/>\n100<br \/>\n20<br \/>\n50<br \/>\n20<br \/>\n60<br \/>\n1<br \/>\n<\/span><\/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\">70<\/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\">\n<p>&nbsp;<\/p>\n<p>\u5bf9\u4e8e100%\u7684\u6570\u636e\uff0cn\u2264500\uff0cm\u2264100000\uff0cd,c\u226410^9<\/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<p>\u8dd1\u5b8c\u6700\u77ed\u8def\u4e4b\u540e\u91cd\u65b0\u5efa\u8fb9\uff0c\u7136\u540e\u5c31\u662f\u88f8\u7684\u7f51\u7edc\u6d41<\/p>\n<p>\u6ce8\u610f\u4e00\u5b9a\u8981\u5f00long long\uff0c\u5e76\u4e14inf\u4e00\u5b9a\u8981\u5927\u3002\u3002\u3002T_T<\/p>\n<pre class=\"lang:c++ decode:true \">#include&lt;map&gt;\r\n#include&lt;cmath&gt;\r\n#include&lt;queue&gt;\r\n#include&lt;cstdio&gt;\r\n#include&lt;cstring&gt;\r\n#include&lt;iostream&gt;\r\n#include&lt;algorithm&gt;\r\nusing namespace std;\r\n#define ll long long\r\n#define pa pair&lt;ll,ll&gt;\r\n#define N 410010\r\n#define M 510\r\ninline ll read()\r\n{\r\n    ll 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\nconst ll inf=(1LL&lt;&lt;60)-1;\r\nll cnt,lj[N],fro[N],to[N],v[N];\r\ninline void add(ll a,ll b,ll c){fro[++cnt]=lj[a];to[cnt]=b;v[cnt]=c;lj[a]=cnt;}\r\ninline void ins(ll a,ll b,ll c){add(a,b,c);add(b,a,0);}\r\nll dis[N],ans;\r\nbool vs[N];\r\nvoid dijsktra()\r\n{\r\n    priority_queue&lt;pa,vector&lt;pa&gt;,greater&lt;pa&gt; &gt;q;\r\n    memset(dis,0x3f,sizeof(dis));\r\n    dis[1]=0;\r\n    q.push(make_pair(0,1));\r\n    ll u,et;\r\n    while(!q.empty())\r\n    {\r\n        u=q.top().second;q.pop();\r\n        if(vs[u]) continue;\r\n        vs[u]=1;\r\n        for(int i=lj[u];i;i=fro[i])\r\n        {\r\n            et=to[i];\r\n            if(dis[et]&gt;dis[u]+v[i])\r\n            {\r\n                dis[et]=dis[u]+v[i];\r\n                q.push(make_pair(dis[et],et));\r\n            }\r\n        }\r\n    }\r\n}\r\nll n,m;\r\nqueue&lt;ll&gt;q;\r\nbool bfs()\r\n{\r\n    memset(dis,0,sizeof(dis));\r\n    dis[1]=1;\r\n    q.push(1);\r\n    ll u;\r\n    while(!q.empty())\r\n    {\r\n        u=q.front();q.pop();\r\n        for(int i=lj[u];i;i=fro[i])\r\n        {\r\n            if(v[i]&amp;&amp;!dis[to[i]])\r\n            {\r\n                dis[to[i]]=dis[u]+1;\r\n                q.push(to[i]);\r\n            }\r\n        }\r\n    }\r\n    return dis[n]?1:0;\r\n}\r\nll dfs(ll x,ll p)\r\n{\r\n    if(x==n||p==0) return p;\r\n    ll tmp=0,tp;\r\n    for(int i=lj[x];i;i=fro[i])\r\n    {\r\n        if(v[i]&amp;&amp;dis[to[i]]==dis[x]+1)\r\n        {\r\n            tp=dfs(to[i],min(p-tmp,v[i]));\r\n            v[i]-=tp;\r\n            v[i^1]+=tp;\r\n            tmp+=tp;\r\n            if(p==tmp) return tmp;\r\n        }\r\n    }\r\n    if(tmp==0) dis[x]=1;\r\n    return tmp;\r\n}\r\nll x[N],y[N],d[N],c;\r\nint main()\r\n{\r\n    n=read();m=read();\r\n    for(int i=1;i&lt;=m;i++)\r\n    {\r\n        x[i]=read();y[i]=read();d[i]=read();\r\n        add(x[i],y[i],d[i]);add(y[i],x[i],d[i]);\r\n    }\r\n    dijsktra();\r\n    memset(lj,0,sizeof(lj));cnt=1;\r\n    for(int i=1;i&lt;=m;i++)\r\n    {\r\n        if(dis[x[i]]+d[i]==dis[y[i]]) ins(x[i]+n,y[i],inf);\r\n        if(dis[y[i]]+d[i]==dis[x[i]]) ins(y[i]+n,x[i],inf);\r\n    }\r\n    for(int i=1;i&lt;=n;i++){c=read();ins(i,i+n,(i==1)||(i==n)?inf:c);}\r\n    n&lt;&lt;=1;\r\n    while(bfs()) ans+=dfs(1,inf);\r\n    printf(\"%lld\\n\",ans);\r\n    return 0;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 3931: [CQOI2015]\u7f51\u7edc\u541e\u5410\u91cf Time Limit:\u00a010 Sec\u00a0\u00a0Memory [&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":[22,43],"class_list":["post-417","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-22","tag-43"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/417","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=417"}],"version-history":[{"count":2,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":419,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/417\/revisions\/419"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}