{"id":64,"date":"2023-02-11T14:57:46","date_gmt":"2023-02-11T06:57:46","guid":{"rendered":"https:\/\/solost.cn\/?p=64"},"modified":"2023-02-11T14:57:46","modified_gmt":"2023-02-11T06:57:46","slug":"149","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=64","title":{"rendered":"2023\u725b\u5ba2\u5bd2\u5047\u7b97\u6cd5\u57fa\u7840\u96c6\u8bad\u84256-I-\u963f\u5b81\u524d\u5f80\u6c99\u57ce"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image-4.png\" alt=\"image.png\" title=\"image.png\"><\/p>\n<p><a href=\"https:\/\/ac.nowcoder.com\/acm\/contest\/46814\/I\">\u9898\u76ee\u94fe\u63a5<\/a><\/p>\n<hr>\n<h2>solution<\/h2>\n<p>\u8fd9\u9053\u9898\u76ee\u7528\u7684\u662fdijkstra\uff0c\u5982\u679c\u8fb9\u6570\u5927\u4e8e\u6700\u77ed\u8def\u5f84\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u8ba4\u4e3a\u6211\u4eec\u7684\u6700\u77ed\u8def\u5f84\u5c31\u662fdist[n]\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u6311\u9009\u4e00\u6761\u4e0d\u7528\u7684\u8def\u6467\u6bc1\uff0c\u7136\u540e\u6bcf\u7ecf\u8fc7\u4ee5\u540e\u6467\u6bc1\u8d70\u8fc7\u7684\u8def\u5c31\u597d\uff0c\u6240\u4ee5dist[n]\u5c31\u662f\u6700\u77ed1-n\u7ecf\u8fc7\u6700\u5c11\u6570\u76ee\u7684\u70b9\uff0c\u56e0\u4e3a\u6743\u503c\u662f1\uff0c\u5982\u679c\u5c0f\u4e8e\u7b49\u4e8e\u7684\u8bdd\uff0c\u90a3\u4e48\u6211\u4eec\u5728\u7b2c\u4e00\u6b21\u8d70\u7684\u65f6\u5019\u5c31\u4e0d\u80fd\u6467\u6bc1\u4efb\u4f55\u4e00\u6761\u8def\uff0c\u6211\u4eec\u53ef\u4ee5\u8d70\u8fc7\u7b2c\u4e00\u6761\u8def\u5230\u8fbe2\uff0c\u7136\u540e\u6bcf\u6b21\u6467\u6bc1\u8d70\u8fc7\u7684\u8def\uff0c\u90a3\u4e48\u503c\u5c31\u662fdist[n] - 1 + c,c\u662f1-2\u90a3\u6761\u8def\u7684\u6743\u503c\uff0c\u51cf\u53bb1\u662f\u628a\u7b2c\u4e00\u6761\u8def\u76841\u6743\u503c\u5220\u53bb\uff0c\u52a0\u4e0a\u672c\u6765\u7684\u6743\u503c\uff0c\u56e0\u4e3a\u7b2c\u4e00\u6b21\u7b2c\u4e00\u6761\u8def\u662f\u4e0d\u80fd\u5148\u6467\u6bc1\u7684\u3002<\/p>\n<hr>\n<p>\u7528vector\u6a21\u62df\u90bb\u63a5\u8868\u7684\u64cd\u4f5c<\/p>\n<pre><code class=\"lang-cpp\">vector&lt;vector&lt;PII&gt;&gt; h[N] \/\/\u8fd9\u4e2a\u64cd\u4f5c\u5c31\u662f\u5f00\u4e86\u5927\u5c0f\u4e3aN\u7684vector\u7684vector\uff0c\u53ef\u4ee5\u505a\u5230\u7b2c\u4e00\u7ef4\u5ea6\u662f\u5934\uff0c\n\u4f8b\u5982\uff0c1-2\uff0c1-3\uff0c2-3\uff0c\u90a3\u4e48h[2] = {2\uff0c 3}\uff0ch[2] = {3}<\/code><\/pre>\n<hr>\n<pre><code>#include &lt;algorithm&gt;\n#include &lt;bitset&gt;\n#include &lt;cmath&gt;\n#include &lt;cstdio&gt;\n#include &lt;cstdlib&gt;\n#include &lt;cstring&gt;\n#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;queue&gt;\n#include &lt;set&gt;\n#include &lt;stack&gt;\n#include &lt;unordered_map&gt;\n#include &lt;vector&gt;\n\nusing namespace std;\n#define x first\n#define y second\n#define endl &#039;\\n&#039;\n#define IOS                       \\\n    ios_base::sync_with_stdio(0); \\\n    cin.tie(0);                   \\\n    cout.tie(0);\ntypedef long long ll;\ntypedef pair&lt;int, int&gt; PII;\ntypedef unsigned long long ull;\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e5 + 10;\n\nint n, m;\nint dist[N], st[N];\n\nvector&lt;PII&gt; h[N];\n\nvoid dijkstra()\n{\n    memset(dist, 0x3f, sizeof dist);\n    dist[1] = 0;\n    priority_queue&lt;PII, vector&lt;PII&gt;, greater&lt;PII&gt;&gt; heap;\n    heap.push({ 0, 1 }); \/\/ \u524d\u4e00\u4e2a\u662f\u8ddd\u79bb\uff0c\u540e\u4e00\u4e2a\u662f\n\n    while (heap.size()) {\n        auto t = heap.top();\n        heap.pop();\n\n        int ver = t.y, distance = t.x;\n\n        if (st[ver])\n            continue;\n        st[ver] = true;\n\n        for (int i = 0; i &lt; (int)h[ver].size(); i++) {\n            int j = h[ver][i].x;\n            int d = h[ver][i].y;\n            d = 1;\n            \/\/ if(h[ver].size() &gt; 2) d = 1;\n            if (dist[j] &gt; dist[ver] + d) {\n                dist[j] = dist[ver] + d;\n                heap.push({ dist[j], j });\n            }\n        }\n    }\n\n    \/\/ if (dist[n] == 0x3f3f3f3f) return -1;\n    \/\/ return dist[n];\n}\n\nint main()\n{\n    scanf(&quot;%d%d&quot;, &amp;n, &amp;m);\n    int t = m;\n    while (t--) {\n        int a, b, c;\n        scanf(&quot;%d%d%d&quot;, &amp;a, &amp;b, &amp;c);\n        if (a != b) {\n            h[a].push_back({ b, c });\n            h[b].push_back({ a, c });\n        }\n    }\n    dijkstra();\n    if (m &gt; dist[n])\n        cout &lt;&lt; dist[n];\n    else\n        cout &lt;&lt; h[1][0].y + dist[n] - 1;\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u94fe\u63a5 solution \u8fd9\u9053\u9898\u76ee\u7528\u7684\u662fdijkstra\uff0c\u5982\u679c\u8fb9\u6570\u5927\u4e8e\u6700\u77ed\u8def\u5f84\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u8ba4\u4e3a\u6211\u4eec\u7684\u6700\u77ed\u8def\u5f84\u5c31\u662fdist[n]\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u6311\u9009\u4e00\u6761\u4e0d\u7528\u7684\u8def\u6467\u6bc1\uff0c\u7136\u540e\u6bcf\u7ecf\u8fc7\u4ee5\u540e\u6467\u6bc1\u8d70\u8fc7\u7684\u8def\u5c31\u597d\uff0c\u6240\u4ee5dist[n]\u5c31\u662f\u6700\u77ed1-n\u7ecf\u8fc7\u6700\u5c11\u6570\u76ee\u7684\u70b9\uff0c\u56e0\u4e3a\u6743\u503c\u662f1\uff0c\u5982\u679c\u5c0f\u4e8e\u7b49\u4e8e\u7684\u8bdd\uff0c\u90a3\u4e48\u6211\u4eec\u5728\u7b2c\u4e00\u6b21\u8d70\u7684\u65f6\u5019\u5c31\u4e0d\u80fd\u6467\u6bc1\u4efb\u4f55\u4e00\u6761\u8def\uff0c\u6211\u4eec\u53ef\u4ee5\u8d70\u8fc7\u7b2c\u4e00\u6761\u8def\u5230\u8fbe2\uff0c\u7136\u540e\u6bcf\u6b21\u6467\u6bc1\u8d70\u8fc7\u7684\u8def\uff0c\u90a3\u4e48\u503c\u5c31\u662fdist[n] - 1 + c,c\u662f1-2\u90a3\u6761\u8def\u7684\u6743\u503c\uff0c\u51cf\u53bb1\u662f\u628a\u7b2c\u4e00\u6761\u8def\u76841\u6743\u503c\u5220\u53bb\uff0c\u52a0\u4e0a\u672c\u6765\u7684\u6743\u503c\uff0c\u56e0\u4e3a\u7b2c\u4e00\u6b21\u7b2c\u4e00\u6761\u8def\u662f\u4e0d\u80fd\u5148\u6467\u6bc1\u7684\u3002 \u7528vector\u6a21\u62df\u90bb\u63a5\u8868\u7684\u64cd\u4f5c vector&lt;vector&lt;PII&gt;&gt; h[N] \/\/\u8fd9\u4e2a\u64cd\u4f5c\u5c31\u662f\u5f00\u4e86\u5927\u5c0f\u4e3aN\u7684vector\u7684vector\uff0c\u53ef\u4ee5\u505a\u5230\u7b2c\u4e00\u7ef4\u5ea6\u662f\u5934\uff0c \u4f8b\u5982\uff0c1-2\uff0c1-3\uff0c2-3\uff0c\u90a3\u4e48h[2] = {2\uff0c 3}\uff0ch[2] = {3} #include &lt;algorithm&gt; #include &lt;bitset&gt; #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;map&gt; #include &lt;queue&gt; #include &lt;set&gt; #include &lt;stack&gt; #include &lt;unordered_map&gt; #include &lt;vector&gt; using namespace std; #define x first #define y second #define [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[26,27,9,7,28],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-solution","tag-dijkstra","tag-27","tag-9","tag-7","tag-28"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=64"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}