{"id":405,"date":"2017-03-19T21:35:49","date_gmt":"2017-03-19T13:35:49","guid":{"rendered":"http:\/\/kylelv.com\/?p=405"},"modified":"2018-01-02T12:10:20","modified_gmt":"2018-01-02T04:10:20","slug":"bzoj-1674-usaco2005part-acquisition-dijkstra%ef%bc%88%e5%a0%86%e4%bc%98%e5%8c%96%ef%bc%89","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=405","title":{"rendered":"bzoj 1674: [Usaco2005]Part Acquisition  &#8212; dijkstra\uff08\u5806\u4f18\u5316\uff09"},"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-69fdef4554a80\" 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-69fdef4554a80\"  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=405\/#1674_Usaco2005Part_Acquisition\" title=\"1674: [Usaco2005]Part Acquisition\">1674: [Usaco2005]Part Acquisition<\/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=405\/#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=405\/#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=405\/#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=405\/#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=405\/#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=405\/#HINT\" title=\"HINT\">HINT<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"1674_Usaco2005Part_Acquisition\"><\/span>1674: [Usaco2005]Part Acquisition<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\">The cows have been sent on a mission through space to acquire a new milking machine for their barn. They are flying through a cluster of stars containing N (1 &lt;= N &lt;= 50,000) planets, each with a trading post. The cows have determined which of K (1 &lt;= K &lt;= 1,000) types of objects (numbered 1..K) each planet in the cluster desires, and which products they have to trade. No planet has developed currency, so they work under the barter system: all trades consist of each party trading exactly one object (presumably of different types). The cows start from Earth with a canister of high quality hay (item 1), and they desire a new milking machine (item K). Help them find the best way to make a series of trades at the planets in the cluster to get item K. If this task is impossible, output -1.<\/div>\n<div class=\"content\"><\/div>\n<div class=\"content\">\u8fd9\u4e9b\u725b\u88ab\u6d3e\u53bb\u592a\u7a7a\u4efb\u52a1\uff0c\u4e3a\u4ed6\u4eec\u7684\u8c37\u4ed3\u8d2d\u7f6e\u4e00\u53f0\u65b0\u6324\u5976\u673a\u3002\u4ed6\u4eec\u662f\u901a\u8fc7\u4e00\u7ec4\u5305\u542bN\uff081 &lt; = N = 50000\uff09\u884c\u661f\uff0c\u6bcf\u4e00\u4e2a\u4ea4\u6613\u5c97\u4f4d\u7684\u6052\u661f\u98de\u884c\u3002\u5976\u725b\u5df2\u7ecf\u786e\u5b9a\u7684K\uff081 &lt; = K = 1000\uff09\u7c7b\u578b\u7684\u5bf9\u8c61\uff08\u7f16\u53f7\u4e3a1 K\uff09\u6bcf\u4e2a\u884c\u661f\u5728\u96c6\u7fa4\u7684\u6b32\u671b\uff0c\u4ee5\u53ca\u4ed6\u4eec\u5fc5\u987b\u4ea4\u6613\u7684\u4ea7\u54c1\u3002\u6ca1\u6709\u884c\u661f\u53d1\u5c55\u8d27\u5e01\uff0c\u6240\u4ee5\u4ed6\u4eec\u5728\u6613\u8d27\u5236\u5ea6\u4e0b\u5de5\u4f5c\uff1a\u6240\u6709\u4ea4\u6613\u7531\u6bcf\u4e00\u65b9\u4ea4\u6613\u5b8c\u5168\u4e00\u4e2a\u5bf9\u8c61\uff08\u5927\u6982\u662f\u4e0d\u540c\u7c7b\u578b\uff09\u3002\u5976\u725b\u4ece\u5730\u7403\u5f00\u59cb\u4e0e\u9ad8\u54c1\u8d28\u7684\u5e72\u8349\u7b52\uff08\u9879\u76ee1\uff09\uff0c\u4ed6\u4eec\u5e0c\u671b\u4e00\u4e2a\u65b0\u7684\u6324\u5976\u673a\uff08\u9879\u76eek\uff09\u3002\u5e2e\u52a9\u4ed6\u4eec\u627e\u5230\u6700\u597d\u7684\u65b9\u6cd5\uff0c\u4f7f\u4e00\u7cfb\u5217\u7684\u4ea4\u6613\u5728\u884c\u661f\u7684\u96c6\u7fa4\uff0c\u4ee5\u83b7\u5f97\u9879\u76eeK.\u5982\u679c\u8fd9\u4e2a\u4efb\u52a1\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u8f93\u51fa- 1\u3002<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Input\"><\/span>Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">* Line 1: Two space-separated integers, N and K. * Lines 2..N+1: Line i+1 contains two space-separated integers, a_i and b_i respectively, that are planet i&#8217;s trading trading products. The planet will give item b_i in order to receive item a_i.<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">* Line 1: One more than the minimum number of trades to get the milking machine which is item K (or -1 if the cows cannot obtain item K).<\/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\">6 5 \/\/6\u4e2a\u661f\u7403,\u5e0c\u671b\u5f97\u52305,\u5f00\u59cb\u65f6\u4f60\u624b\u4e2d\u67091\u53f7\u8d27\u7269.<br \/>\n1 3 \/\/1\u53f7\u661f\u7403,\u5e0c\u671b\u5f97\u52301\u53f7\u8d27\u7269,\u5c06\u7ed9\u4f603\u53f7\u8d27\u7269<br \/>\n3 2<br \/>\n2 3<br \/>\n3 1<br \/>\n2 5<br \/>\n5 4<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\">4<\/span><\/p>\n<p>OUTPUT DETAILS:<\/p>\n<p>The cows possess 4 objects in total: first they trade object 1 for<br \/>\nobject 3, then object 3 for object 2, then object 2 for object 5.<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u9898\u76ee\u5c31\u662f\u76f8\u5f53\u4e8e\u6709n\u6761\u6709\u5411\u8fb9\uff0c\u8fb9\u6743\u4e3a1\uff0c\u6c421\u5230k\u7684\u6700\u77ed\u8def<\/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 M 50010\r\n#define N 1010\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 cnt,lj[N],fro[M],to[M],v[M];\r\nvoid add(int a,int b,int w){fro[++cnt]=lj[a];to[cnt]=b;v[cnt]=w;lj[a]=cnt;}\r\nint dis[N];\r\nbool vs[N];\r\nvoid dijkstra()\r\n{\r\n    priority_queue&lt;pa,vector&lt;pa&gt;,greater&lt;pa&gt; &gt;q;\r\n    memset(dis,127,sizeof(dis));\r\n    dis[1]=0;\r\n    q.push(make_pair(0,1));\r\n    int u;\r\n    while(!q.empty())\r\n    {\r\n        u=q.top().second;q.pop();\r\n        for(int i=lj[u];i;i=fro[i])\r\n        {\r\n            if(dis[to[i]]&gt;dis[u]+v[i])\r\n            {\r\n                dis[to[i]]=dis[u]+v[i];\r\n                q.push(make_pair(dis[to[i]],to[i]));\r\n            } \r\n        }\r\n    }\r\n}\r\nint n,k,x,y;\r\nint main()\r\n{\r\n    n=read();k=read();\r\n    for(int i=1;i&lt;=n;i++)\r\n    {\r\n        x=read();y=read();\r\n        add(x,y,1);\r\n    }\r\n    dijkstra();\r\n    dis[k]&gt;100000000?puts(\"-1\"):printf(\"%d\\n\",dis[k]+1);\r\n    return 0;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 1674: [Usaco2005]Part Acquisition 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":[40,22],"class_list":["post-405","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-dijkstra","tag-22"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/405","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=405"}],"version-history":[{"count":2,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/405\/revisions"}],"predecessor-version":[{"id":407,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/405\/revisions\/407"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}