标签:
HDU 2007-10 Programming Contest_WarmUp
题目大意:给你N个点(编号为0~N-1),M条路,问最小生成树是多少,如果不能生成最
小生成树,则输出impossible
思路:用Kruskal来做,如果最后得不到N-1条路,就输出impossible,否则就输出结果。
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; const int MAXN = 1100; const int MAXM = 10010; int N,M,father[MAXN]; int find(int x) { if(x != father[x]) father[x] = find(father[x]); return father[x]; } struct EdgeNode { int from; int to; int w; }Edges[MAXM]; int cmp(EdgeNode a, EdgeNode b) { return a.w < b.w; } void Kruskal() { int ans = 0; int Count = 0; for(int i = 0; i < M; ++i) { int u = find(Edges[i].from); int v = find(Edges[i].to); if(u != v) { ans += Edges[i].w; father[v] = u; Count++; if(Count == N-1) break; } } if(Count == N-1) printf("%d\n\n",ans); else printf("impossible\n\n"); } int main() { while(~scanf("%d%d",&N,&M)) { memset(Edges,0,sizeof(Edges)); for(int i = 0; i <= N; ++i) father[i] = i; for(int i = 0; i < M; ++i) { scanf("%d%d%d",&Edges[i].from,&Edges[i].to,&Edges[i].w); } sort(Edges,Edges+M,cmp); Kruskal(); } return 0; }
HDU2122 Ice_cream’s world III【Kruskal】
标签:
原文地址:http://blog.csdn.net/lianai911/article/details/42887245