这里是kruskal做法 当然prim也可以,至于prim和kruskal的比较: Prim在稠密图中比Kruskal优,Kruskal在稀疏图中比Prim优. #include<bits/stdc++.h> using namespace std; ]; struct edge{ int u; int v; int w; }e[]; int cmp(edge a,edge b) { return a.w<b.w; } int find(int x) { return fa[x]==x?…
include include include include using namespace std; const int maxn = 505000; int n, m, dis[maxn], vis[maxn], ans; struct edge{ int from, to, next, len; }e[maxn<<2]; int head[maxn], cnt; void add(int u, int v, int w) { e[++cnt].from = u; e[cnt].len…
题目链接:https://www.luogu.org/problem/P3366 最小生成树模板题. Kruskal算法 算法思想:给边按边权从小到大排序,然后遍历每一条边,如果边上的两个点不在同一个集合,则选择这条边,并将两个点所在集合合并.直到选择了 \(n-1\) 条边. 实现代码如下: #include <bits/stdc++.h> using namespace std; const int maxn = 200200; int n, m, f[5050], cnt, u[maxn…