求助

P3366 【模板】最小生成树

@[海阔天空818](/space/show?uid=154412) # 嗯 ## 也谢谢您帮我改题
by ☆La™塩鱻© @ 2019-07-25 15:21:34


@[海阔天空818](/space/show?uid=154412) #### 不是的呢
by ☆La™塩鱻© @ 2019-07-25 15:22:00


@[☆La™塩鱻©](/space/show?uid=64454) 这。。。不要转移话题QAQ(笑哭
by Gary818 @ 2019-07-25 15:22:11


@[☆La™塩鱻©](/space/show?uid=64454) 行吧,dalao
by Gary818 @ 2019-07-25 15:22:30


我真的不是小姐姐
by ☆La™塩鱻© @ 2019-07-25 15:22:45


@[☆La™塩鱻©](/space/show?uid=64454) 那好吧,溜了溜了~~
by Gary818 @ 2019-07-25 15:23:22


@[海阔天空818](/space/show?uid=154412) ```cpp en ```
by ☆La™塩鱻© @ 2019-07-25 15:23:56


```cpp #include<cstdio> #include<algorithm> using namespace std; struct la { int u,v,w; }e[200005]; int n,m; int father[200005]; int ans,cnt; inline int find(int x) { if (father[x] == x) return x; return father[x] = find(father[x]); } inline void kruskal() { for (int i = 1;i <= n;i++) father[i] = i; for (int i = 1;i <= m;i++) { int u = e[i].u; int v = e[i].v; int fa1 = find(u); int fa2 = find(v); if (fa1 != fa2) { ans += e[i].w; father[fa1] = fa2; if (cnt++ == n - 1) break; } } if (cnt < n - 1) printf("orz\n"); else printf("%d\n",ans); } bool cmp(la a,la b) { return a.w < b.w; } int main() { scanf("%d%d",&n,&m); ans = cnt = 0; for (int i = 1;i <= m;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); sort(e + 1,e + m + 1,cmp); kruskal(); return 0; } ```
by ☆La™塩鱻© @ 2019-07-25 15:24:16


上一页 |