题解 P3366 【【模板】最小生成树】
或成最短题解?(仅供娱乐)
#include<bits/stdc++.h>
#define FOR(a) for(int i=1;i<=(a);i++)
#define input scanf
#define output(a) cout<<(a);
#define Find int find(int x) { if(x==fa[x]) { return x; } return fa[x]=find(fa[x]); }
#define Cmp int cmp(Edge a,Edge b){return a.d<b.d;}
#define EDGE struct Edge{int s,e,d;}edge[200010];
#define Define int n,m,fa[200010],ans;
#define NAMESPACE using namespace std;
#define Kruskal void kruskal(){int total=0;FOR(m){int u=find(edge[i].s),v=find(edge[i].e);if(u!=v){ans+=edge[i].d;fa[u]=v;total++;if(total==n-1){break;}}}}
#define MAIN int main(){input("%d%d",&n,&m);FOR(n){fa[i]=i;}FOR(m){input("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].d);}sort(edge+1,edge+m+1,cmp);kruskal();output(ans);}
#define WORK NAMESPACE Define EDGE Find Cmp Kruskal MAIN
WORK