{"id":628,"date":"2017-06-14T18:52:00","date_gmt":"2017-06-14T10:52:00","guid":{"rendered":"http:\/\/kylelv.com\/?p=628"},"modified":"2020-03-04T14:54:43","modified_gmt":"2020-03-04T06:54:43","slug":"bzoj-1598-usaco2008-mar%e7%89%9b%e8%b7%91%e6%ad%a5-%e7%ac%ack%e7%9f%ad%e8%b7%af%ef%bc%8ca","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=628","title":{"rendered":"bzoj 1598: [Usaco2008 Mar]\u725b\u8dd1\u6b65 &#8212; \u7b2ck\u77ed\u8def\uff0cA*"},"content":{"rendered":"\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-69fdeefd8f17b\" 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-69fdeefd8f17b\"  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=628\/#1598_Usaco2008_Mar%E7%89%9B%E8%B7%91%E6%AD%A5\" title=\"1598: [Usaco2008 Mar]\u725b\u8dd1\u6b65\">1598: [Usaco2008 Mar]\u725b\u8dd1\u6b65<\/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=628\/#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=628\/#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=628\/#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=628\/#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=628\/#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=628\/#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=628\/#Source\" title=\"Source\">Source<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"has-text-align-center wp-block-heading\"><span class=\"ez-toc-section\" id=\"1598_Usaco2008_Mar%E7%89%9B%E8%B7%91%E6%AD%A5\"><\/span>1598: [Usaco2008 Mar]\u725b\u8dd1\u6b65<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"has-text-align-center\">Time Limit:&nbsp;10 Sec&nbsp;&nbsp;Memory Limit:&nbsp;162 MB<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Description\"><\/span>Description<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>BESSIE\u51c6\u5907\u7528\u4ece\u725b\u68da\u8dd1\u5230\u6c60\u5858\u7684\u65b9\u6cd5\u6765\u953b\u70bc. \u4f46\u662f\u56e0\u4e3a\u5979\u61d2,\u5979\u53ea\u51c6\u5907\u6cbf\u7740\u4e0b\u5761\u7684\u8def\u8dd1\u5230\u6c60\u5858, \u7136\u540e\u8d70\u56de\u725b\u68da. BESSIE\u4e5f\u4e0d\u60f3\u8dd1\u5f97\u592a\u8fdc,\u6240\u4ee5\u5979\u60f3\u8d70\u6700\u77ed\u7684\u8def\u7ecf. \u519c\u573a\u4e0a\u4e00\u5171\u6709M (1 &lt;= M &lt;= 10,000)\u6761\u8def, \u6bcf\u6761\u8def\u8fde\u63a5\u4e24\u4e2a\u75281..N(1 &lt;= N &lt;= 1000)\u6807\u53f7\u7684\u5730\u70b9. \u66f4\u65b9\u4fbf\u7684\u662f,\u5982\u679cX&gt;Y,\u5219\u5730\u70b9X\u7684\u9ad8\u5ea6\u5927\u4e8e\u5730\u70b9Y\u7684\u9ad8\u5ea6. \u5730\u70b9N\u662fBESSIE\u7684\u725b\u68da;\u5730\u70b91\u662f\u6c60\u5858. \u5f88\u5feb, BESSIE\u538c\u5026\u4e86\u4e00\u76f4\u8d70\u540c\u4e00\u6761\u8def.\u6240\u4ee5\u5979\u60f3\u8d70\u4e0d\u540c\u7684\u8def,\u66f4\u660e\u786e\u5730\u8bb2,\u5979\u60f3\u627e\u51faK (1 &lt;= K &lt;= 100)\u6761\u4e0d\u540c\u7684\u8def\u7ecf.\u4e3a\u4e86\u907f\u514d\u8fc7\u5ea6\u52b3\u7d2f,\u5979\u60f3\u4f7f\u8fd9K\u6761\u8def\u7ecf\u4e3a\u6700\u77ed\u7684K\u6761\u8def\u7ecf. \u8bf7\u5e2e\u52a9BESSIE\u627e\u51fa\u8fd9K\u6761\u6700\u77ed\u8def\u7ecf\u7684\u957f\u5ea6.\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u8bfb\u5165\u519c\u573a\u7684\u5730\u56fe, \u4e00\u4e9b\u4eceX_i\u5230Y_i \u7684\u8def\u7ecf\u548c\u5b83\u4eec\u7684\u957f\u5ea6(X_i, Y_i, D_i). \u6240\u6709(X_i, Y_i, D_i)\u6ee1\u8db3(1 &lt;= Y_i &lt; X_i; Y_i &lt; X_i &lt;= N, 1 &lt;= D_i &lt;= 1,000,000).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Input\"><\/span>Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>* \u7b2c1\u884c: 3\u4e2a\u6570: N, M, \u548cK<\/p>\n\n\n\n<p>* \u7b2c 2..M+1\u884c: \u7b2c i+1 \u884c\u5305\u542b3\u4e2a\u6570 X_i, Y_i, \u548c D_i, \u8868\u793a\u4e00\u6761\u4e0b\u5761\u7684\u8def.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>* \u7b2c1..K\u884c: \u7b2ci\u884c\u5305\u542b\u7b2ci\u6700\u77ed\u8def\u7ecf\u7684\u957f\u5ea6,\u6216-1\u5982\u679c\u8fd9\u6837\u7684\u8def\u7ecf\u4e0d\u5b58\u5728.\u5982\u679c\u591a\u6761\u8def\u7ecf\u6709\u540c\u6837\u7684\u957f\u5ea6,\u8bf7\u6ce8\u610f\u5c06\u8fd9\u4e9b\u957f\u5ea6\u9010\u4e00\u5217\u51fa.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Sample_Input\"><\/span>Sample Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>5 8 7<br>5 4 1<br>5 3 1<br>5 2 1<br>5 1 1<br>4 3 4<br>3 1 1<br>3 2 1<br>2 1 1<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Sample_Output\"><\/span>Sample Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>1<br>2<br>2<br>3<br>6<br>7<br>-1<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u8f93\u51fa\u89e3\u91ca:<br><\/p>\n\n\n\n<p>\u8def\u7ecf\u5206\u522b\u4e3a(5-1), (5-3-1), (5-2-1), (5-3-2-1), (5-4-3-1),<br><br>(5-4-3-2-1).<br><br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Source\"><\/span>Source<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>k\u77ed\u8def\u6a21\u677f\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u7528A*\u7b97\u6cd5\u6c42\u89e3<\/p>\n\n\n\n<p>\u5bf9\u6bcf\u4e2a\u70b9\u8bb0\u5f55\u5f53\u524d\u4ee3\u4ef7\u548c\u4f30\u4ef7\u503c\uff0c\u4f30\u4ef7\u503c\u5c31\u662f\u5f53\u524d\u70b9\u5230\u6c47\u70b9\u7684\u6700\u77ed\u8def\uff0c\u8fd9\u4e2a\u53ef\u4ee5\u53cd\u5411\u5efa\u56fe\uff0c\u76f8\u5f53\u4e8e\u8dd1\u5355\u6e90\u6700\u77ed\u8def<\/p>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u4ece\u6e90\u70b9\u51fa\u53d1bfs\uff0c\u7136\u540e\u6bcf\u6b21\u53d6\u961f\u4e2d&nbsp;\u5f53\u524d\u4ee3\u4ef7+\u4f30\u4ef7\u503c \u6700\u5c0f\u7684\u70b9\u7ee7\u7eedbfs<\/p>\n\n\n\n<p>\u8fd9\u6837\u7b2ck\u6b21\u5230\u8fbe\u6c47\u70b9\u7684\u65b9\u6848\u5c31\u662f\u5b83\u7684\u7b2ck\u77ed\u8def\uff08\u8bc1\u660e\u7684\u8bdd\u53ef\u4ee5\u7528\u6570\u5b66\u5f52\u7eb3\u6cd5\uff09<\/p>\n\n\n\n<p>\u5e76\u4e14\u6211\u4eec\u5f88\u5bb9\u6613\u53d1\u73b0\u4e00\u4e2a\u70b9\u5982\u679c\u5165\u961f\u5927\u4e8ek\u6b21\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u7531\u6b64\u526a\u679d\u4e00\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;map>\n#include&lt;cmath>\n#include&lt;queue>\n#include&lt;cstdio>\n#include&lt;cstring>\n#include&lt;iostream>\n#include&lt;algorithm>\nusing namespace std;\n#define inf 1000000007\n#define ll long long\n#define N 100010\ninline int rd()\n{\n    int x=0,f=1;char ch=getchar();\n    while(ch&lt;'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}\n    while(ch>='0'&amp;&amp;ch&lt;='9'){x=x*10+ch-'0';ch=getchar();}\n    return x*f;\n}\nint lj&#91;N],fro&#91;N],to&#91;N],v&#91;N],cnt;\ninline void add(int a,int b,int c){fro&#91;++cnt]=lj&#91;a];to&#91;cnt]=b;v&#91;cnt]=c;lj&#91;a]=cnt;}\nint lj2&#91;N],fro2&#91;N],to2&#91;N],v2&#91;N],cnt2;\ninline void add2(int a,int b,int c){fro2&#91;++cnt2]=lj2&#91;a];to2&#91;cnt2]=b;v2&#91;cnt2]=c;lj2&#91;a]=cnt2;}\nint q&#91;N*100],ji&#91;N];\nll dis&#91;N];\nint n,m,k;\nbool vs&#91;N];\nvoid spfa()\n{\n    memset(dis,0x3f,sizeof(dis));\n    int l=0,r=1,x;\n    dis&#91;1]=0;vs&#91;1]=1;\n    q&#91;0]=1;\n    while(l&lt;r)\n    {\n        x=q&#91;l++];vs&#91;x]=0;\n        for(int i=lj2&#91;x];i;i=fro2&#91;i])\n        {\n            if(dis&#91;to2&#91;i]]>dis&#91;x]+v2&#91;i])\n            {\n                dis&#91;to2&#91;i]]=dis&#91;x]+v2&#91;i];\n                if(!vs&#91;to2&#91;i]]){vs&#91;to2&#91;i]]=1;q&#91;r++]=to2&#91;i];}\n            }\n        }\n    }\n}\nstruct qaz\n{\n    int x;ll h,g;\n    bool operator &lt;(const qaz &amp;a)const\n    {return g+h>a.g+a.h;}\n};\nll ans&#91;N],tot;\nvoid Astar()\n{\n    priority_queue&lt;qaz>q;\n    q.push((qaz){n,0,dis&#91;n]});\n    qaz tp;int x;\n    while(!q.empty())\n    {\n        tp=q.top();q.pop();\n        x=tp.x;vs&#91;x]=0;\n        ji&#91;x]++;\n        if(ji&#91;x]>k) continue;\n        if(x==1) ans&#91;++tot]=tp.g+tp.h;\n        if(tot==k) return;\n        for(int i=lj&#91;x];i;i=fro&#91;i])\n            q.push((qaz){to&#91;i],tp.h+v&#91;i],dis&#91;to&#91;i]]});\n    }\n}\nint main()\n{\n    n=rd();m=rd();k=rd();\n    int x,y,d;\n    for(int i=1;i&lt;=m;i++)\n    {\n        x=rd();y=rd();d=rd();\n        if(x&lt;y) swap(x,y); \n        add(x,y,d);add2(y,x,d);\n    }\n    spfa();\n    memset(ans,-1,sizeof(ans));\n    Astar();\n    for(int i=1;i&lt;=k;i++) printf(\"%lld\\n\",ans&#91;i]);\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1598: [Usaco2008 Mar]\u725b\u8dd1\u6b65 Time Limit:&nbsp;10 Sec&nbsp;&#038; [&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":[67,68],"class_list":["post-628","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-a","tag-k"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/628","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=628"}],"version-history":[{"count":1,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":629,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/628\/revisions\/629"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}