{"id":66940,"date":"2026-01-27T22:09:18","date_gmt":"2026-01-27T14:09:18","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/66940.html"},"modified":"2026-01-27T22:09:18","modified_gmt":"2026-01-27T14:09:18","slug":"usaco06nov-roadblocks-g%ef%bc%88%e6%b4%9b%e8%b0%b7p2865%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/66940.html","title":{"rendered":"[USACO06NOV] Roadblocks G\uff08\u6d1b\u8c37P2865\uff09"},"content":{"rendered":"<h3>\u9898\u76ee\u63cf\u8ff0<\/h3>\n<p>Bessie \u642c\u5230\u4e86\u4e00\u4e2a\u5c0f\u519c\u573a&#xff0c;\u6709\u65f6\u559c\u6b22\u56de\u53bb\u62dc\u8bbf\u5979\u7684\u4e00\u4e2a\u597d\u670b\u53cb\u3002\u5979\u4e0d\u60f3\u592a\u5feb\u5230\u8fbe\u5979\u7684\u65e7\u5bb6&#xff0c;\u56e0\u4e3a\u5979\u559c\u6b22\u6cbf\u9014\u7684\u98ce\u666f\u3002\u5979\u51b3\u5b9a\u9009\u62e9\u7b2c\u4e8c\u77ed\u7684\u8def\u5f84\u800c\u4e0d\u662f\u6700\u77ed\u7684\u8def\u5f84\u3002\u5979\u77e5\u9053\u4e00\u5b9a\u5b58\u5728\u67d0\u6761\u7b2c\u4e8c\u77ed\u8def\u5f84\u3002<\/p>\n<p>\u4e61\u6751\u7531\u00a0R(1\u2264R\u2264100,000)\u00a0\u6761\u53cc\u5411\u9053\u8def\u7ec4\u6210&#xff0c;\u6bcf\u6761\u9053\u8def\u8fde\u63a5\u00a0N(1\u2264N\u22645000)\u00a0\u4e2a\u4ea4\u53c9\u8def\u53e3\u4e2d\u7684\u4e24\u4e2a&#xff0c;\u8fd9\u4e9b\u4ea4\u53c9\u8def\u53e3\u88ab\u65b9\u4fbf\u5730\u7f16\u53f7\u4e3a\u00a01\u00a0\u5230\u00a0N\u3002Bessie \u4ece\u4ea4\u53c9\u8def\u53e3\u00a01\u00a0\u51fa\u53d1&#xff0c;\u5979\u7684\u670b\u53cb&#xff08;\u76ee\u7684\u5730&#xff09;\u5728\u4ea4\u53c9\u8def\u53e3\u00a0N\u3002<\/p>\n<p>\u7b2c\u4e8c\u77ed\u8def\u5f84\u53ef\u4ee5\u4e0e\u4efb\u4f55\u6700\u77ed\u8def\u5f84\u5171\u4eab\u9053\u8def&#xff0c;\u5e76\u4e14\u53ef\u4ee5\u56de\u6eaf&#xff0c;\u5373\u591a\u6b21\u4f7f\u7528\u76f8\u540c\u7684\u9053\u8def\u6216\u4ea4\u53c9\u8def\u53e3\u3002\u7b2c\u4e8c\u77ed\u8def\u5f84\u662f\u957f\u5ea6\u6bd4\u6700\u77ed\u8def\u5f84\u957f\u7684\u6700\u77ed\u8def\u5f84&#xff08;\u5373&#xff0c;\u5982\u679c\u5b58\u5728\u4e24\u6761\u6216\u591a\u6761\u6700\u77ed\u8def\u5f84&#xff0c;\u7b2c\u4e8c\u77ed\u8def\u5f84\u662f\u957f\u5ea6\u6bd4\u8fd9\u4e9b\u8def\u5f84\u957f\u4f46\u4e0d\u6bd4\u4efb\u4f55\u5176\u4ed6\u8def\u5f84\u957f\u7684\u8def\u5f84&#xff09;\u3002<\/p>\n<h3>\u8f93\u5165\u683c\u5f0f<\/h3>\n<p>\u7b2c\u00a01\u00a0\u884c&#xff1a;\u4e24\u4e2a\u7528\u7a7a\u683c\u5206\u9694\u7684\u6574\u6570&#xff1a;N\u00a0\u548c\u00a0R\u3002<\/p>\n<p>\u7b2c\u00a02\u00a0\u884c\u5230\u7b2c\u00a0R&#043;1\u00a0\u884c&#xff1a;\u6bcf\u884c\u5305\u542b\u4e09\u4e2a\u7528\u7a7a\u683c\u5206\u9694\u7684\u6574\u6570&#xff1a;A\u3001B\u00a0\u548c\u00a0D&#xff0c;\u63cf\u8ff0\u8fde\u63a5\u4ea4\u53c9\u8def\u53e3\u00a0A\u00a0\u548c\u00a0B\u00a0\u7684\u4e00\u6761\u957f\u5ea6\u4e3a\u00a0D(1\u2264D\u22645000)\u00a0\u7684\u9053\u8def\u3002<\/p>\n<h3>\u8f93\u51fa\u683c\u5f0f<\/h3>\n<p>\u7b2c\u00a01\u00a0\u884c&#xff1a;\u8282\u70b9\u00a01\u00a0\u548c\u8282\u70b9\u00a0N\u00a0\u4e4b\u95f4\u7b2c\u4e8c\u77ed\u8def\u5f84\u7684\u957f\u5ea6\u3002<\/p>\n<p>\u663e\u793a\u7ffb\u8bd1<\/p>\n<h3>\u9898\u610f\u7ffb\u8bd1<\/h3>\n<\/p>\n<h3>\u8f93\u5165\u8f93\u51fa\u6837\u4f8b<\/h3>\n<p>\u8f93\u5165 #1\u590d\u5236\u8fd0\u884c<\/p>\n<p>4 4<br \/>\n1 2 100<br \/>\n2 4 200<br \/>\n2 3 250<br \/>\n3 4 100 <\/p>\n<p>\u8f93\u51fa #1\u590d\u5236\u8fd0\u884c<\/p>\n<p>450 <\/p>\n<h3>\u8bf4\u660e\/\u63d0\u793a<\/h3>\n<p>\u4e24\u6761\u8def\u5f84&#xff1a;1\u21922\u21924&#xff08;\u957f\u5ea6\u00a0100&#043;200&#061;300&#xff09;\u548c\u00a01\u21922\u21923\u21924&#xff08;\u957f\u5ea6\u00a0100&#043;250&#043;100&#061;450&#xff09;\u3002<\/p>\n<p>&#xff08;\u7531 ChatGPT 4o \u7ffb\u8bd1&#xff09;<\/p>\n<\/p>\n<h3>1. \u9898\u76ee\u91cd\u8ff0<\/h3>\n<p>\u9898\u76ee\u540d\u79f0&#xff1a;Roadblocks (USACO 06 NOV)<\/p>\n<p>\u6838\u5fc3\u95ee\u9898&#xff1a;\u7ed9\u5b9a\u4e00\u5f20N\u4e2a\u70b9\u3001R\u6761\u8fb9\u7684\u53cc\u5411\u56fe&#xff0c;\u6c42\u4ece\u8d77\u70b91\u5230\u7ec8\u70b9N\u7684\u4e25\u683c\u6b21\u77ed\u8def\u5f84\u957f\u5ea6\u3002<\/p>\n<p>\u5b9a\u4e49&#xff1a;\u4e25\u683c\u6b21\u77ed\u8def\u5f84\u662f\u6307\u957f\u5ea6\u4e25\u683c\u5927\u4e8e\u6700\u77ed\u8def\u5f84\u957f\u5ea6\u7684\u6240\u6709\u8def\u5f84\u4e2d&#xff0c;\u957f\u5ea6\u6700\u5c0f\u7684\u90a3\u4e00\u6761\u3002<\/p>\n<ul>\n<li>\n<p>\u6570\u636e\u8303\u56f4&#xff1a;<img decoding=\"async\" alt=\"$N \\\\le 5000$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140916-6978c70ca125d.png\" \/>&#xff0c;<img decoding=\"async\" alt=\"$R \\\\le 100,000$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140916-6978c70cb007d.png\" \/>\u3002<\/p>\n<\/li>\n<li>\n<p>\u7279\u6b8a\u8bf4\u660e&#xff1a;\u8def\u5f84\u53ef\u4ee5\u5305\u542b\u91cd\u590d\u7684\u8282\u70b9\u6216\u8fb9&#xff08;\u5373\u5141\u8bb8\u8d70\u56de\u5934\u8def&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>2. \u9898\u76ee\u5206\u6790<\/h3>\n<h4>\u8bef\u533a&#xff1a;K \u77ed\u8def\u7b97\u6cd5&#xff1f;<\/h4>\n<p>\u4e00\u542c\u5230\u201c\u7b2c K \u77ed\u8def\u201d&#xff0c;\u5f88\u591a\u4eba\u7b2c\u4e00\u53cd\u5e94\u662f A* \u7b97\u6cd5\u3002\u867d\u7136 A* \u53ef\u4ee5\u89e3&#xff0c;\u4f46\u5bf9\u4e8eK&#061;2\u4e14\u56fe\u6bd4\u8f83\u7b80\u5355\u7684\u60c5\u51b5&#xff0c;A* \u5199\u8d77\u6765\u592a\u7e41\u7410\u4e14\u5bb9\u6613\u51fa\u9519\u3002<\/p>\n<h4>\u6b63\u89e3&#xff1a;Dijkstra \u72b6\u6001\u62c6\u5206<\/h4>\n<p>\u666e\u901a\u7684 Dijkstra \u7ef4\u62a4\u4e00\u4e2a dis[i] \u8868\u793a\u8d77\u70b9\u5230i\u7684\u6700\u77ed\u8ddd\u79bb\u3002<\/p>\n<p>\u4e3a\u4e86\u6c42\u6b21\u77ed\u8def&#xff0c;\u6211\u4eec\u53ea\u9700\u8981\u8ba9\u6bcf\u4e2a\u8282\u70b9\u591a\u7ef4\u62a4\u4e00\u4e2a\u72b6\u6001\u3002<\/p>\n<p>\u72b6\u6001\u5b9a\u4e49&#xff1a;<\/p>\n<ul>\n<li>\n<p>dis[i][0]&#xff1a;\u8d77\u70b9\u5230i\u7684\u6700\u77ed\u8def\u957f\u5ea6\u3002<\/p>\n<\/li>\n<li>\n<p>dis[i][1]&#xff1a;\u8d77\u70b9\u5230i\u7684\u4e25\u683c\u6b21\u77ed\u8def\u957f\u5ea6\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u521d\u59cb\u5316&#xff1a;<\/p>\n<ul>\n<li>\n<p>dis[1][0]&#061;0&#xff0c;\u5176\u4f59\u5168\u4e3a <img decoding=\"async\" alt=\"$\\\\infty$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140916-6978c70cbadd7.png\" \/>\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u677e\u5f1b\u64cd\u4f5c&#xff08;\u6838\u5fc3\u903b\u8f91&#xff09;&#xff1a;<\/p>\n<p>\u5047\u8bbe\u5f53\u524d\u4ece\u70b9u\u62ff\u51fa\u7684\u8ddd\u79bb\u662fd&#xff0c;\u51c6\u5907\u901a\u8fc7\u8fb9\u6743w\u66f4\u65b0\u90bb\u5c45v&#xff0c;\u65b0\u8ddd\u79bb<img decoding=\"async\" alt=\"$new\\\\_dist &#061; d &#043; w$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140916-6978c70cc5979.png\" \/>\u3002<\/p>\n<li>\n<p>\u66f4\u65b0\u6700\u77ed\u8def&#xff1a;<\/p>\n<p>\u5982\u679c <img decoding=\"async\" alt=\"$new\\\\_dist &lt; dis[v][0]$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140916-6978c70cd28f3.png\" \/>&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u539f\u6709\u7684\u6700\u77ed\u8def<img decoding=\"async\" alt=\"$dis[v][0]$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140916-6978c70ce0f4a.png\" \/>\u6ca6\u4e3a\u6b21\u77ed\u8def&#xff0c;\u8d4b\u503c\u7ed9<img decoding=\"async\" alt=\"$dis[v][1]$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140916-6978c70ceec82.png\" \/>&#xff0c;\u5e76\u52a0\u5165\u961f\u5217\u3002<\/p>\n<\/li>\n<li>\n<p><img decoding=\"async\" alt=\"$new\\\\_dist$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140917-6978c70d05708.png\" \/>\u6210\u4e3a\u65b0\u7684\u6700\u77ed\u8def&#xff0c;\u8d4b\u503c\u7ed9<img decoding=\"async\" alt=\"$dis[v][0]$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140916-6978c70ce0f4a.png\" \/>&#xff0c;\u5e76\u52a0\u5165\u961f\u5217\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u66f4\u65b0\u6b21\u77ed\u8def&#xff1a;<\/p>\n<p>\u5982\u679c<img decoding=\"async\" alt=\"$new\\\\_dist &gt; dis[v][0]$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140917-6978c70d105ac.png\" \/>\u4e14<img decoding=\"async\" alt=\"$new\\\\_dist &lt; dis[v][1]$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140917-6978c70d1c38f.png\" \/>&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8bf4\u660e\u627e\u5230\u4e86\u4e00\u6761\u6bd4\u6700\u77ed\u8def\u957f\u3001\u4f46\u6bd4\u65e7\u6b21\u77ed\u8def\u77ed\u7684\u8def\u5f84\u3002<\/p>\n<\/li>\n<li>\n<p>\u66f4\u65b0<img decoding=\"async\" alt=\"$dis[v][1] &#061; new\\\\_dist$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140917-6978c70d282b3.png\" \/>&#xff0c;\u5e76\u52a0\u5165\u961f\u5217\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7b49\u4e8e\u6700\u77ed\u8def&#xff1a;<\/p>\n<p>\u5982\u679c<img decoding=\"async\" alt=\"$new\\\\_dist &#061;&#061; dis[v][0]$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140917-6978c70d3422f.png\" \/>&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u9898\u76ee\u8981\u6c42\u4e25\u683c\u6b21\u77ed&#xff0c;\u6240\u4ee5\u76f8\u7b49\u7684\u8def\u5f84\u5bf9\u6b21\u77ed\u8def\u6ca1\u6709\u8d21\u732e&#xff0c;\u76f4\u63a5\u5ffd\u7565\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<h3>3. \u5b8c\u6574\u4ee3\u7801<\/h3>\n<p>\/\/\u4e25\u683c\u6b21\u77ed\u8def \u90bb\u63a5\u8868&#043;\u5806\u4f18\u5316Dijkstra \u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;O(Rlog N)<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;queue&gt;<br \/>\n#include &lt;algorithm&gt;\/\/\u5bf9\u5e94min\u51fd\u6570<br \/>\nusing namespace std;<br \/>\nconst int maxn&#061;5010;<br \/>\nconst int maxm&#061;200010;<br \/>\nint dis[maxn][2];\/\/dis[i][0]\u4ee3\u8868\u8d77\u70b9\u5230i\u70b9\u7684\u6700\u77ed\u8def dis[i][1]\u4ee3\u8868\u6b21\u77ed\u8def<br \/>\nint h[maxn];\/\/\u5b58\u6240\u6709\u70b9\u7684\u5934\u6307\u9488<br \/>\nint vtex[maxm];\/\/\u5b58\u7b2ci\u6761\u8fb9\u7ec8\u70b9\u4e0b\u6807<br \/>\nint nxt[maxm];\/\/\u4e0e\u7b2ci\u6761\u8fb9\u540c\u8d77\u70b9\u7684\u4e0b\u4e00\u6761\u8fb9\u7684\u7d22\u5f15<br \/>\nint wt[maxm];\/\/\u7b2ci\u6761\u8fb9\u957f\u5ea6<br \/>\nint idx;\/\/\u603b\u8fb9\u6570<br \/>\nint n,r;<br \/>\nint vis[maxn];\/\/\u8bb0\u5f55\u6bcf\u4e2a\u70b9\u662f\u5426\u5df2\u7ecf\u51fa\u961f\u8fc7<br \/>\nstruct node{<br \/>\n    int id;\/\/\u70b9\u7f16\u53f7<br \/>\n    int w;\/\/\u70b9\u5230\u8d77\u70b9\u8ddd\u79bb(\u4e0d\u7ba1\u6700\u77ed\u8fd8\u662f\u6b21\u77ed&#xff09;<br \/>\n    \/\/\u4f18\u5148\u961f\u5217\u9ed8\u8ba4\u6700\u5927\u5806&#xff0c;\u91cd\u8f7d\u8fd0\u7b97\u7b26\u4fee\u6539\u4e3a\u6700\u5c0f\u5806<br \/>\n    friend bool operator &lt;(node a,node b){<br \/>\n        return a.w&gt;b.w;<br \/>\n    }<br \/>\n};<br \/>\npriority_queue&lt;node&gt; q;<br \/>\nvoid addedge(int u,int v,int w){<br \/>\n    vtex[idx]&#061;v;<br \/>\n    nxt[idx]&#061;h[u];<br \/>\n    wt[idx]&#061;w;<br \/>\n    h[u]&#061;idx&#043;&#043;;<br \/>\n}<\/p>\n<p>void dijkstra(){<br \/>\n    dis[1][0]&#061;0;\/\/\u8d77\u70b9\u5230\u81ea\u5df1\u7684\u6700\u77ed\u8ddd\u79bb\u4e3a0<br \/>\n    node tmp;<br \/>\n    tmp.id&#061;1;<br \/>\n    tmp.w&#061;0;<br \/>\n    q.push(tmp);\/\/\u8d77\u70b9\u5165\u961f<br \/>\n    while(!q.empty()){<br \/>\n        tmp&#061;q.top();\/\/\u8bbf\u95ee\u961f\u9996<br \/>\n        q.pop();\/\/\u961f\u9996\u51fa\u961f<br \/>\n        int nid&#061;tmp.id;<br \/>\n        \/\/\u5982\u679c\u51fa\u961f\u7684tmp\u5230\u8d77\u70b9\u7684\u8ddd\u79bb\u7684\u8ddd\u79bb\u5927\u4e8etmp\u5f53\u524d\u5b58\u50a8\u7684\u5230\u8d77\u70b9\u7684\u6b21\u77ed\u8def\u7684\u8ddd\u79bb \u5c31\u8df3\u8fc7<br \/>\n        \/\/\u56e0\u4e3a\u8fd9\u4e2a\u8ddd\u79bb\u4e0d\u53ef\u80fd\u7528\u6765\u66f4\u65b0tmp\u7684\u90bb\u63a5\u70b9<br \/>\n        if(tmp.w&gt;dis[tmp.id][1]) continue;<br \/>\n        int p&#061;h[nid];<br \/>\n        while(p!&#061;-1){\/\/\u904d\u5386tmp\u6240\u6709\u4e34\u63a5\u70b9<br \/>\n            \/\/\u5982\u679c\u90bb\u63a5\u70b9\u5230\u8d77\u70b9\u7684\u6700\u77ed\u8def\u7684\u8ddd\u79bb\u5927\u4e8e\u7ecftmp\u53bb\u5230\u8d77\u70b9\u7684\u8ddd\u79bb(\u60c5\u51b51 \u53d1\u73b0\u6bd4\u6700\u77ed\u8def\u66f4\u77ed\u7684\u8def\u5f84)<br \/>\n            if(dis[vtex[p]][0]&gt;tmp.w&#043;wt[p]){<br \/>\n                \/\/\u5c31\u5148\u628a\u90bb\u63a5\u70b9\u5230\u8d77\u70b9\u7684\u6700\u77ed\u8def\u7684\u8ddd\u79bb\u7ed9\u5230\u6b21\u77ed\u8def<br \/>\n                dis[vtex[p]][1]&#061;dis[vtex[p]][0];<br \/>\n                \/\/\u7136\u540e\u518d\u66f4\u65b0\u90bb\u63a5\u70b9\u5230\u8d77\u70b9\u7684\u6700\u77ed\u8def\u7684\u8ddd\u79bb\u7b49\u4e8e\u7ecftmp\u53bb\u5230\u8d77\u70b9\u7684\u8ddd\u79bb<br \/>\n                dis[vtex[p]][0]&#061;tmp.w&#043;wt[p];<br \/>\n                \/\/\u628a\u6700\u77ed\u8ddd\u79bb\u548c\u6b21\u77ed\u8ddd\u79bb\u90fd\u5165\u961f<br \/>\n                q.push({vtex[p],dis[vtex[p]][1]});<br \/>\n                q.push({vtex[p],dis[vtex[p]][0]});<br \/>\n            }<br \/>\n            \/\/\u5982\u679c\u90bb\u63a5\u70b9\u5230\u8d77\u70b9\u7684\u6700\u77ed\u8def\u8ddd\u79bb\u5c0f\u4e8e\u7ecftmp\u53bb\u5230\u8d77\u70b9\u7684\u8ddd\u79bb \u4f46\u662f\u90bb\u63a5\u70b9\u5230\u8d77\u70b9\u7684\u6b21\u77ed\u8def\u8ddd\u79bb\u5927\u4e8e\u7ecftmp\u53bb\u5230\u8d77\u70b9\u7684\u8ddd\u79bb(\u60c5\u51b52\u53d1\u73b0\u4e25\u683c\u6b21\u77ed\u8def\u5f84 (\u4ecb\u4e8e\u6700\u77ed\u548c\u6b21\u77ed\u4e4b\u95f4))<br \/>\n            else if(dis[vtex[p]][0]&lt;tmp.w&#043;wt[p] &amp;&amp;dis[vtex[p]][1]&gt;tmp.w&#043;wt[p]){<br \/>\n                \/\/\u5c31\u66f4\u65b0\u90bb\u63a5\u70b9\u5230\u8d77\u70b9\u7684\u6b21\u77ed\u8def\u8ddd\u79bb\u7b49\u4e8e\u7ecftmp\u53bb\u5230\u8d77\u70b9\u7684\u8ddd\u79bb<br \/>\n                dis[vtex[p]][1]&#061;tmp.w&#043;wt[p];<br \/>\n                \/\/\u7136\u540e\u5165\u961f<br \/>\n                q.push({vtex[p],dis[vtex[p]][1]});<br \/>\n            }<br \/>\n            p&#061;nxt[p];<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\nint main(){<br \/>\n    cin&gt;&gt;n&gt;&gt;r;\/\/n\u4e2a\u4ea4\u53c9\u8def\u53e3 r\u6761\u8def<br \/>\n    \/\/\u521d\u59cb\u5316\u5934\u6307\u9488\u6570\u7ec4\u4e3a\u7a7a<br \/>\n    memset(h,-1,sizeof(h));<br \/>\n    \/\/\u521d\u59cb\u5316\u6bcf\u4e2a\u70b9\u5230\u5176\u4ed6\u5230\u8d77\u70b9\u7684\u6700\u77ed\u8ddd\u79bb\u548c\u6b21\u77ed\u8ddd\u79bb\u90fd\u662f\u65e0\u7a77<br \/>\n    memset(dis,0x3f,sizeof(dis));<br \/>\n    \/\/\u90bb\u63a5\u8868\u5b58\u56fe<br \/>\n    for(int i&#061;1;i&lt;&#061;r;i&#043;&#043;){<br \/>\n        int u,v,w;<br \/>\n        cin&gt;&gt;u&gt;&gt;v&gt;&gt;w;<br \/>\n        addedge(u,v,w);\/\/\u53cc\u5411\u9053\u8def \u53cc\u5411\u52a0\u8fb9<br \/>\n        addedge(v,u,w);<br \/>\n    }<br \/>\n    dijkstra();<br \/>\n    \/\/\u8f93\u51fa\u8282\u70b91\u548c\u8282\u70b9N\u4e4b\u95f4\u7b2c\u4e8c\u77ed\u8def\u5f84\u7684\u957f\u5ea6<br \/>\n    cout&lt;&lt;dis[n][1];<br \/>\n    return 0;<br \/>\n}<\/p>\n<h3>4. \u6613\u9519\u70b9\u603b\u7ed3<\/h3>\n<li>\n<p>\u4e25\u683c&#xff1a;\u9898\u76ee\u4e2d\u7684\u5b9a\u4e49\u51b3\u5b9a\u4e86\u4ee3\u7801\u903b\u8f91\u3002\u5982\u679c\u662f\u201c\u975e\u4e25\u683c&#xff08;\u5141\u8bb8\u76f8\u7b49&#xff09;\u201d&#xff0c;\u90a3\u4e48 new_dist &gt; dis[v][0] \u5e94\u8be5\u6539\u4e3a new_dist&gt;&#061;dis[v][0] \u6216\u8005\u5728\u964d\u7ea7\u65f6\u903b\u8f91\u4f1a\u6709\u53d8\u5316\u3002\u672c\u9898\u8981\u6c42\u4e25\u683c&#xff0c;\u6240\u4ee5\u76f8\u7b49\u7684\u8ddd\u79bb\u76f4\u63a5\u4e22\u5f03\u3002<\/p>\n<\/li>\n<li>\n<p>\u7a7a\u95f4\u5f00\u9500&#xff1a;\u7531\u4e8e\u662f\u53cc\u5411\u8fb9&#xff0c;addedge \u8c03\u7528\u4e86\u4e24\u6b21&#xff0c;\u6240\u4ee5\u8fb9\u7684\u6570\u7ec4 (vtex, nxt, wt) \u5927\u5c0f\u5fc5\u987b\u662f <img decoding=\"async\" alt=\"$R \\\\times 2$\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140917-6978c70d3fd86.png\" \/>\u3002\u53ea\u5f00100000\u4f1a\u5bfc\u81f4 RE\u3002<\/p>\n<\/li>\n<li>\n<p>\u526a\u679d&#xff1a;if(d&gt;dis[u][1]) continue; \u8fd9\u53e5\u975e\u5e38\u91cd\u8981\u3002\u56e0\u4e3a\u4e00\u4e2a\u70b9\u53ef\u80fd\u4f1a\u88ab\u591a\u6b21\u5165\u961f&#xff0c;\u5982\u679c\u53d6\u51fa\u7684\u8ddd\u79bb\u6bd4\u6211\u4eec\u5df2\u7ecf\u627e\u5230\u7684\u6b21\u77ed\u8def\u8fd8\u957f&#xff0c;\u5b83\u5c31\u4e0d\u53ef\u80fd\u66f4\u65b0\u4efb\u4f55\u70b9\u7684\u6700\u77ed\u6216\u6b21\u77ed\u8def\u4e86\u3002<\/p>\n<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0<br \/>\nBessie \u642c\u5230\u4e86\u4e00\u4e2a\u5c0f\u519c\u573a&#xff0c;\u6709\u65f6\u559c\u6b22\u56de\u53bb\u62dc\u8bbf\u5979\u7684\u4e00\u4e2a\u597d\u670b\u53cb\u3002\u5979\u4e0d\u60f3\u592a\u5feb\u5230\u8fbe\u5979\u7684\u65e7\u5bb6&#xff0c;\u56e0\u4e3a\u5979\u559c\u6b22\u6cbf\u9014\u7684\u98ce\u666f\u3002\u5979\u51b3\u5b9a\u9009\u62e9\u7b2c\u4e8c\u77ed\u7684\u8def\u5f84\u800c\u4e0d\u662f\u6700\u77ed\u7684\u8def\u5f84\u3002\u5979\u77e5\u9053\u4e00\u5b9a\u5b58\u5728\u67d0\u6761\u7b2c\u4e8c\u77ed\u8def\u5f84\u3002<br \/>\n\u4e61\u6751\u7531\u00a0R(1\u2264R\u2264100,000)\u00a0\u6761\u53cc\u5411\u9053\u8def\u7ec4\u6210&#xff0c;\u6bcf\u6761\u9053\u8def\u8fde\u63a5\u00a0N(1\u2264N\u22645000)\u00a0\u4e2a\u4ea4\u53c9\u8def\u53e3\u4e2d\u7684\u4e24\u4e2a&#xff0c;\u8fd9\u4e9b\u4ea4\u53c9\u8def\u53e3\u88ab\u65b9\u4fbf\u5730\u7f16\u53f7\u4e3a\u00a01\u00a0\u5230\u00a0N\u3002Bessie \u4ece\u4ea4\u53c9\u8def\u53e3\u00a01\u00a0\u51fa\u53d1&#xff0c;\u5979\u7684\u670b\u53cb&#xff08;\u76ee<\/p>\n","protected":false},"author":2,"featured_media":66927,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7094,7092,7093,1984,427],"topic":[],"class_list":["post-66940","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-dijkstra","tag-7092","tag-7093","tag-1984","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[USACO06NOV] Roadblocks G\uff08\u6d1b\u8c37P2865\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/66940.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[USACO06NOV] Roadblocks G\uff08\u6d1b\u8c37P2865\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u9898\u76ee\u63cf\u8ff0 Bessie \u642c\u5230\u4e86\u4e00\u4e2a\u5c0f\u519c\u573a&#xff0c;\u6709\u65f6\u559c\u6b22\u56de\u53bb\u62dc\u8bbf\u5979\u7684\u4e00\u4e2a\u597d\u670b\u53cb\u3002\u5979\u4e0d\u60f3\u592a\u5feb\u5230\u8fbe\u5979\u7684\u65e7\u5bb6&#xff0c;\u56e0\u4e3a\u5979\u559c\u6b22\u6cbf\u9014\u7684\u98ce\u666f\u3002\u5979\u51b3\u5b9a\u9009\u62e9\u7b2c\u4e8c\u77ed\u7684\u8def\u5f84\u800c\u4e0d\u662f\u6700\u77ed\u7684\u8def\u5f84\u3002\u5979\u77e5\u9053\u4e00\u5b9a\u5b58\u5728\u67d0\u6761\u7b2c\u4e8c\u77ed\u8def\u5f84\u3002 \u4e61\u6751\u7531\u00a0R(1\u2264R\u2264100,000)\u00a0\u6761\u53cc\u5411\u9053\u8def\u7ec4\u6210&#xff0c;\u6bcf\u6761\u9053\u8def\u8fde\u63a5\u00a0N(1\u2264N\u22645000)\u00a0\u4e2a\u4ea4\u53c9\u8def\u53e3\u4e2d\u7684\u4e24\u4e2a&#xff0c;\u8fd9\u4e9b\u4ea4\u53c9\u8def\u53e3\u88ab\u65b9\u4fbf\u5730\u7f16\u53f7\u4e3a\u00a01\u00a0\u5230\u00a0N\u3002Bessie \u4ece\u4ea4\u53c9\u8def\u53e3\u00a01\u00a0\u51fa\u53d1&#xff0c;\u5979\u7684\u670b\u53cb&#xff08;\u76ee\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/66940.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-27T14:09:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140916-6978c70ca125d.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/66940.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/66940.html\",\"name\":\"[USACO06NOV] Roadblocks G\uff08\u6d1b\u8c37P2865\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-27T14:09:18+00:00\",\"dateModified\":\"2026-01-27T14:09:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/66940.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/66940.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/66940.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[USACO06NOV] Roadblocks G\uff08\u6d1b\u8c37P2865\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[USACO06NOV] Roadblocks G\uff08\u6d1b\u8c37P2865\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/66940.html","og_locale":"zh_CN","og_type":"article","og_title":"[USACO06NOV] Roadblocks G\uff08\u6d1b\u8c37P2865\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u9898\u76ee\u63cf\u8ff0 Bessie \u642c\u5230\u4e86\u4e00\u4e2a\u5c0f\u519c\u573a&#xff0c;\u6709\u65f6\u559c\u6b22\u56de\u53bb\u62dc\u8bbf\u5979\u7684\u4e00\u4e2a\u597d\u670b\u53cb\u3002\u5979\u4e0d\u60f3\u592a\u5feb\u5230\u8fbe\u5979\u7684\u65e7\u5bb6&#xff0c;\u56e0\u4e3a\u5979\u559c\u6b22\u6cbf\u9014\u7684\u98ce\u666f\u3002\u5979\u51b3\u5b9a\u9009\u62e9\u7b2c\u4e8c\u77ed\u7684\u8def\u5f84\u800c\u4e0d\u662f\u6700\u77ed\u7684\u8def\u5f84\u3002\u5979\u77e5\u9053\u4e00\u5b9a\u5b58\u5728\u67d0\u6761\u7b2c\u4e8c\u77ed\u8def\u5f84\u3002 \u4e61\u6751\u7531\u00a0R(1\u2264R\u2264100,000)\u00a0\u6761\u53cc\u5411\u9053\u8def\u7ec4\u6210&#xff0c;\u6bcf\u6761\u9053\u8def\u8fde\u63a5\u00a0N(1\u2264N\u22645000)\u00a0\u4e2a\u4ea4\u53c9\u8def\u53e3\u4e2d\u7684\u4e24\u4e2a&#xff0c;\u8fd9\u4e9b\u4ea4\u53c9\u8def\u53e3\u88ab\u65b9\u4fbf\u5730\u7f16\u53f7\u4e3a\u00a01\u00a0\u5230\u00a0N\u3002Bessie \u4ece\u4ea4\u53c9\u8def\u53e3\u00a01\u00a0\u51fa\u53d1&#xff0c;\u5979\u7684\u670b\u53cb&#xff08;\u76ee","og_url":"https:\/\/www.wsisp.com\/helps\/66940.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-27T14:09:18+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260127140916-6978c70ca125d.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/66940.html","url":"https:\/\/www.wsisp.com\/helps\/66940.html","name":"[USACO06NOV] Roadblocks G\uff08\u6d1b\u8c37P2865\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-27T14:09:18+00:00","dateModified":"2026-01-27T14:09:18+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/66940.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/66940.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/66940.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"[USACO06NOV] Roadblocks G\uff08\u6d1b\u8c37P2865\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/66940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=66940"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/66940\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/66927"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=66940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=66940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=66940"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=66940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}