{"id":323,"date":"2020-04-18T23:08:20","date_gmt":"2020-04-18T15:08:20","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=323"},"modified":"2020-04-21T22:05:39","modified_gmt":"2020-04-21T14:05:39","slug":"%e6%9c%80%e5%b0%8f%e7%94%9f%e6%88%90%e6%a0%91%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/04\/18\/%e6%9c%80%e5%b0%8f%e7%94%9f%e6%88%90%e6%a0%91%e5%8e%9f%e7%90%86\/","title":{"rendered":"\u6700\u5c0f\u751f\u6210\u6811\u539f\u7406"},"content":{"rendered":"<h2>\u56fe\u7684\u6700\u5c0f\u751f\u6210\u6811<\/h2>\n<p><strong>\u5728\u56fe\u8bba\u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u8fde\u901a\u7684\u5b50\u56fe\u5305\u542b\u56fe\u7684\u6240\u6709\u7ed3\u70b9\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5b50\u56fe\u79f0\u4e3a\u56fe\u7684\u751f\u6210\u6811\u3002\u751f\u6210\u6811\u5305\u542b\u6240\u6709\u7ed3\u70b9\u7684\u8fde\u901a\u5b50\u56fe\u4e2d\u7684\u6781\u5c0f\u8fde\u901a\u56fe\u3002\u56fe\u7684\u751f\u6210\u6811\u4e0d\u552f\u4e00\u3002\u4ece\u4efb\u610f\u4e00\u4e2a\u9876\u70b9\u5f00\u59cb\u904d\u5386(dfs,bfs\u90fd\u53ef)\uff0c\u90fd\u4f1a\u5f97\u5230\u4e00\u4e2a\u4e0d\u540c\u7684\u751f\u6210\u6811\u3002<\/strong><br \/>\n\u5728\u5e26\u6743\u56fe\u7684\u6240\u6709\u751f\u6210\u6811\u4e2d\uff0c\u6743\u503c\u4e4b\u548c\u6700\u5c0f\u7684\u79f0\u4e3a<strong>\u6700\u5c0f\u751f\u6210\u6811<\/strong>\u3002<br \/>\n\u6700\u5c0f\u751f\u6210\u6811\u7684\u51e0\u4e2a\u6027\u8d28\uff1a<\/p>\n<blockquote>\n<p>\u6700\u5c0f\u751f\u6210\u6811\u662f\u4e00\u4e2a\u8fde\u901a\u56fe<br \/>\n\u6700\u5c0f\u751f\u6210\u6811\u662f\u4e00\u68f5\u65e0\u73af\u6811\uff0c\u56e0\u6b64\u6709n-1\u6761\u8fb9\uff0c\u6811\u4e0a\u4efb\u610f\u4e24\u8282\u70b9\u4e4b\u95f4\u8def\u5f84\u552f\u4e00\u3002<\/p>\n<\/blockquote>\n<p>\u5e38\u7528\u7684\u751f\u6210\u6811\u7b97\u6cd5\u6709DFS\u751f\u6210\u6811\u3001BFS\u751f\u6210\u6811\u3001Prim \u6700\u5c0f\u751f\u6210\u6811\u548cKruskal\u6700\u5c0f\u751f\u6210\u6811\u7b97\u6cd5<br \/>\n\u6c42\u4e00\u4e2a\u56fe\u7684\u6700\u5c0f\u751f\u6210\u6811\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1aKruskal\u7b97\u6cd5\u548cPrim\u7b97\u6cd5\u3002<\/p>\n<h4>Kruskal\u7b97\u6cd5<\/h4>\n<p>Krusakl\u7b97\u6cd5\u6c42\u6700\u5c0f\u751f\u6210\u6811\uff0c\u662f\u501f\u52a9\u4e8e\u5e76\u67e5\u96c6\u5b8c\u6210\u7684\u4e00\u4e2a\u6c42\u6700\u5c0f\u751f\u6210\u6811\u7b80\u5355\u6613\u5b9e\u73b0\u7684\u7b97\u6cd5\u3002<br \/>\n\u9996\u5148\u8981\u77e5\u9053\u51e0\u4e2a\u6027\u8d28\uff1a<\/p>\n<blockquote>\n<p><strong>\u4e00\u4e2a\u8fde\u901a\u5757\u53ef\u4ee5\u770b\u6210\u4e00\u4e2a\u96c6\u5408\uff0c\u8fd9\u5c31\u662f\u5f15\u5165\u5e76\u67e5\u96c6\u7684\u5730\u65b9<br \/>\n\u4e24\u4e2a\u8fde\u901a\u5757\u4e4b\u95f4\u7684\u6240\u6709\u8fb9\u4e2d\uff0c\u6743\u503c\u6700\u5c0f\u7684\u90a3\u6761\u8fb9\u4e00\u5b9a\u53c2\u4e0e\u6784\u6210\u6700\u5c0f\u751f\u6210\u6811\u3002<\/strong><\/p>\n<\/blockquote>\n<p><strong>\u5728\u4e00\u4e2a\u5e26\u6743\u7684\u65e0\u5411\u8fde\u901a\u56fe\u4e2d\uff0c\u5148\u5bf9\u6240\u6709\u7684\u8fb9\u7684\u6743\u503c\u8fdb\u884c\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u7136\u540e\uff0c\u904d\u5386\u8fd9\u4e9b\u8fb9\uff0c\u5982\u679c\u8fd9\u6761\u8fb9\u7684\u4e24\u4e2a\u7aef\u70b9\u5728\u540c\u4e00\u4e2a\u96c6\u5408\u5185\uff0c\u5373\u5728\u540c\u4e00\u4e2a\u8fde\u901a\u5757\u4e2d\uff0c\u90a3\u4e48\u8fd9\u6761\u8fb9\u5c31\u4e0d\u5728\u6700\u5c0f\u751f\u6210\u6811\u4e2d\uff1b\u5426\u5219\uff0c\u8fd9\u6761\u8fb9\u5728\u6700\u5c0f\u751f\u6210\u6811\u4e2d\uff0c\u7136\u540e\u5c06\u8fd9\u6761\u8fb9\u7684\u4e24\u4e2a\u70b9\u6240\u5728\u7684\u8fde\u901a\u5757\u5408\u5e76\uff0c\u5373\u5408\u5e76\u4e24\u4e2a\u96c6\u5408\u5373\u53ef\uff1b\u5f53\u5df2\u9009\u7684\u8fb9\u7684\u6570\u76ee\u4e3a\u603b\u7ed3\u70b9\u7684\u6570\u76ee-1(n-1)\u65f6\uff0c\u8bf4\u660e\u6700\u5c0f\u751f\u6210\u6811\u5df2\u7ecf\u6784\u6210\u3002<\/strong>\u524d\u4e24\u4e2a\u64cd\u4f5c\u6070\u597d\u5bf9\u5e94\u4e8e\u5e76\u67e5\u96c6\u7684\u67e5\u8be2\u4e24\u4e2a\u5143\u7d20\u662f\u5426\u5728\u540c\u4e00\u96c6\u5408\u5185\u548c\u5408\u5e76\u4e24\u4e2a\u96c6\u5408\uff0c\u6240\u4ee5\u7528\u5e76\u67e5\u96c6\u521a\u597d\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<br \/>\n<a href=\"https:\/\/www.luogu.com.cn\/problem\/P3366\">\u6700\u5c0f\u751f\u6210\u6811\u6a21\u677f \u6d1b\u8c37P3366<\/a><\/p>\n<pre><code class=\"language-c++\">#include &lt;algorithm&gt;\n#include &lt;iostream&gt;\nusing namespace std;\nconst int maxn = 2e5+5;\ntypedef struct edge{\n    int u,v,w;\n}Edge;\nint n,m;\nint fa[5050];\nEdge e[maxn];\ninline bool cmp(Edge a,Edge b){\n    return a.w &lt; b.w;\n}\ninline int find(int x){\n    while (x!=fa[x])    x = fa[x] = fa[fa[x]];\n    return x;\n}\nint main(){\n    std::ios::sync_with_stdio(false);\n    int ans,x,y,now;\n    cin &gt;&gt; n &gt;&gt; m;\n    for (int i=1;i&lt;=m;i++)\n        cin &gt;&gt; e[i].u &gt;&gt; e[i].v &gt;&gt; e[i].w;\n    sort (e+1,e+m+1,cmp);          \/\/\u6309\u6743\u503c\u6392\u5e8f\n    ans = now = 0;\n    for (int i=1;i&lt;=n;i++)  fa[i] = i;\n    for (int i=1;i&lt;=m;i++){\n        x=find(e[i].u), y=find(e[i].v);\n        if (x==y)   continue;          \/\/\u4e24\u4e2a\u7ed3\u70b9\u5c5e\u4e8e\u540c\u4e00\u8fde\u901a\u5757\u65f6\n        ans += e[i].w;\n        fa[x] = y;\n        if (++now==n-1) break;\n    }\n    cout &lt;&lt; ans;\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u56fe\u7684\u6700\u5c0f\u751f\u6210\u6811 \u5728\u56fe\u8bba\u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u8fde\u901a\u7684\u5b50\u56fe\u5305\u542b\u56fe\u7684\u6240\u6709\u7ed3\u70b9\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5b50\u56fe\u79f0\u4e3a\u56fe\u7684\u751f\u6210\u6811\u3002\u751f\u6210\u6811\u5305\u542b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/323"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":8,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":344,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/323\/revisions\/344"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}