{"id":425,"date":"2017-04-01T14:26:34","date_gmt":"2017-04-01T06:26:34","guid":{"rendered":"http:\/\/kylelv.com\/?p=425"},"modified":"2018-01-03T18:09:04","modified_gmt":"2018-01-03T10:09:04","slug":"bzoj-1579-usaco2009-febrevamping-trails-%e9%81%93%e8%b7%af%e5%8d%87%e7%ba%a7-%e5%88%86%e5%b1%82%e5%9b%be%e6%9c%80%e7%9f%ad%e8%b7%af","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=425","title":{"rendered":"bzoj 1579: [Usaco2009 Feb]Revamping Trails \u9053\u8def\u5347\u7ea7  &#8212; \u5206\u5c42\u56fe\u6700\u77ed\u8def"},"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-69fdef440c37c\" 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-69fdef440c37c\"  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=425\/#1579_Usaco2009_FebRevamping_Trails_%E9%81%93%E8%B7%AF%E5%8D%87%E7%BA%A7\" title=\"1579: [Usaco2009 Feb]Revamping Trails \u9053\u8def\u5347\u7ea7\">1579: [Usaco2009 Feb]Revamping Trails \u9053\u8def\u5347\u7ea7<\/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=425\/#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=425\/#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=425\/#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=425\/#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=425\/#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=425\/#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=425\/#Source\" title=\"Source\">Source<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"1579_Usaco2009_FebRevamping_Trails_%E9%81%93%E8%B7%AF%E5%8D%87%E7%BA%A7\"><\/span>1579: [Usaco2009 Feb]Revamping Trails \u9053\u8def\u5347\u7ea7<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\"><span class=\"green\">Time Limit:\u00a010 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>\u6bcf\u5929,\u519c\u592bJohn\u9700\u8981\u7ecf\u8fc7\u4e00\u4e9b\u9053\u8def\u53bb\u68c0\u67e5\u725b\u68daN\u91cc\u9762\u7684\u725b. \u519c\u573a\u4e0a\u6709M(1&lt;=M&lt;=50,000)\u6761\u53cc\u5411\u6ce5\u571f\u9053\u8def,\u7f16\u53f7\u4e3a1..M. \u9053\u8defi\u8fde\u63a5\u725b\u68daP1_i\u548cP2_i (1 &lt;= P1_i &lt;= N; 1 &lt;= P2_i&lt;= N). John\u9700\u8981T_i (1 &lt;= T_i &lt;= 1,000,000)\u65f6\u95f4\u5355\u4f4d\u7528\u9053\u8defi\u4eceP1_i\u8d70\u5230P2_i\u6216\u8005\u4eceP2_i \u8d70\u5230P\uff11_i \u4ed6\u60f3\u66f4\u65b0\u4e00\u4e9b\u8def\u7ecf\u6765\u51cf\u5c11\u6bcf\u5929\u82b1\u5728\u8def\u4e0a\u7684\u65f6\u95f4.\u5177\u4f53\u5730\u8bf4,\u4ed6\u60f3\u66f4\u65b0K (1 &lt;= K &lt;= 20)\u6761\u8def\u7ecf\uff0c\u5c06\u5b83\u4eec\u6240\u987b\u65f6\u95f4\u51cf\u4e3a\uff10\uff0e\u5e2e\u52a9FJ\u9009\u62e9\u54ea\u4e9b\u8def\u7ecf\u9700\u8981\u66f4\u65b0\u4f7f\u5f97\u4ece\uff11\u5230N\u7684\u65f6\u95f4\u5c3d\u91cf\u5c11.<\/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>* \u7b2c\u4e00\u884c: \u4e09\u4e2a\u7a7a\u683c\u5206\u5f00\u7684\u6570: N, M, \u548c K * \u7b2c2..M+1\u884c: \u7b2ci+1\u884c\u6709\u4e09\u4e2a\u7a7a\u683c\u5206\u5f00\u7684\u6570\uff1aP1_i, P2_i, \u548c T_i<\/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>* \u7b2c\u4e00\u884c: \u66f4\u65b0\u6700\u591a\uff2b\u6761\u8def\u7ecf\u540e\u7684\u6700\u77ed\u8def\u7ecf\u957f\u5ea6\uff0e<\/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\">4 4 1<br \/>\n1 2 10<br \/>\n2 4 10<br \/>\n1 3 1<br \/>\n3 4 100<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\">\n<p><span class=\"sampledata\">1<\/span><\/p>\n<p>&nbsp;<\/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>K\u662f1; \u66f4\u65b0\u9053\u8def3-&gt;4\u4f7f\u5f97\u4ece\uff13\u5230\uff14\u7684\u65f6\u95f4\u7531\uff11\uff10\uff10\u51cf\u5c11\u5230\uff10. \u6700\u65b0\u6700\u77ed\u8def\u7ecf\u662f1-&gt;3-&gt;4,\u603b\u7528\u65f6\u4e3a\uff11\u5355\u4f4d. N&lt;=10000<\/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>\u5c06\u56fe\u62c6\u6210 k+1 \u5c42\uff0c\u7b2c i \u5c42\u4ee3\u8868\u5df2\u7528\u4e86 i \u6b21\u514d\u8d39\u7684\u60c5\u51b5\uff0c\u8fd9\u6837\u4fdd\u8bc1\u4e0d\u53ef\u8fd4\u56de\u4e0a\u4e00\u5c42\uff0c\u4e14\u6700\u591a\u53ea\u80fd\u8d70 k \u6b21\u3002<\/p>\n<p>\u6211\u4eec\u53d1\u73b0\u5bf9\u4e8e\u6bcf\u4e00\u5c42\u7684\u56fe\u90fd\u662f\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5bf9\u4e8e\u6bcf\u4e00\u5c42\u7684\u8fb9\u7684\u4fe1\u606f\u90fd\u53ef\u4ee5\u901a\u8fc7\u7b2c 0 \u5c42\u6620\u5c04\u8fc7\u6765\uff0c\u8fd9\u6837\u6211\u4eec\u53ea\u9700\u5efa\u4e00\u5c42\u56fe\uff0c\u5e76\u8bb0\u5f55\u6bcf\u5c42\u70b9\u7684 dis \u503c\uff0c\u7136\u540e\u5728\u66f4\u65b0\u7684\u65f6\u5019\u5206\u522b\u66f4\u65b0\u4e00\u4e0b\u5f53\u524d\u5c42\u548c\u4e0b\u4e00\u5c42\u7684\u503c\u5373\u53ef\u3002<\/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 M 10100\r\n#define N 100100\r\n#define pa pair&lt;int,int&gt;\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\nint n,m,k,s=1,t;\r\nint cnt,lj[M],fro[N],to[N],v[N];\r\ninline void add(int a,int b,int c){fro[++cnt]=lj[a];to[cnt]=b;v[cnt]=c;lj[a]=cnt;}\r\nint dis[M][22];\r\nbool vs[M][22];\r\npriority_queue&lt;pa,vector&lt;pa&gt;,greater&lt;pa&gt; &gt;q;\r\nvoid dijkstra()\r\n{\r\n    memset(dis,0x3f,sizeof(dis));\r\n    dis[s][0]=0;\r\n    q.push(make_pair(0,s));\r\n    int u,et,x;\r\n    while(!q.empty())\r\n    {\r\n        u=q.top().second;q.pop();\r\n        x=u\/(n+1);u=u%(n+1);\r\n        if(vs[u][x]) continue;\r\n        vs[u][x]=1;\r\n        for(int i=lj[u];i;i=fro[i])\r\n        {\r\n            et=to[i];\r\n            if(dis[et][x]&gt;dis[u][x]+v[i])\r\n            {\r\n                dis[et][x]=dis[u][x]+v[i];\r\n                q.push(make_pair(dis[et][x],et+x*(n+1)));\r\n            }\r\n            if(x==k) continue;\r\n            if(dis[et][x+1]&gt;dis[u][x])\r\n            {\r\n                dis[et][x+1]=dis[u][x];\r\n                q.push(make_pair(dis[et][x+1],et+(x+1)*(n+1)));\r\n            }\r\n        }\r\n    }\r\n}\r\nint a,b,c;\r\nint main()\r\n{\r\n    t=n=read();m=read();k=read();\r\n    for(int i=1;i&lt;=m;i++)\r\n    {\r\n        a=read();b=read();c=read();\r\n        add(a,b,c);add(b,a,c);\r\n    }\r\n    dijkstra();\r\n    printf(\"%d\\n\",dis[n][k]);\r\n    return 0;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 1579: [Usaco2009 Feb]Revamping Trails \u9053\u8def\u5347\u7ea7 Time  [&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":[42,22],"class_list":["post-425","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-42","tag-22"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/425","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=425"}],"version-history":[{"count":2,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/425\/revisions"}],"predecessor-version":[{"id":427,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/425\/revisions\/427"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}