hdu1879(最小生成树)
2014-04-24 10:56
253 查看
此题用到kruskal算法,其中有用到了并查集
代码如下:
代码如下:
#include<iostream> #include<algorithm> #include<string> #include<stack> #include<queue> #include<set> #include<map> #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<time.h> #include<math.h> #define eps 1e-9 #define N 10000 #define pi acos(-1.0) #define P system("pause") using namespace std; struct node { int a,b; int cost,flag; }s ; int f ; bool cmp(node A,node B) { if(A.flag != B.flag) return A.flag > B.flag; return A.cost < B.cost; } int find(int x) { return x == f[x] ? x:f[x] = find(f[x]); } int kruskal(int n) { int x,y,sum = 0; for(int i = 0; i < n; i++) { x = find(s[i].a); y = find(s[i].b); if(x != y) { f[x] = y; if(s[i].flag == 0) sum += s[i].cost; } } return sum; } int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int n; while(scanf("%d",&n) && n) { int i,j; int m = n*(n-1)/2; for(i = 0; i < m; i++) scanf("%d%d%d%d",&s[i].a,&s[i].b,&s[i].cost,&s[i].flag); sort(s,s+m,cmp); for(i = 1; i <= n; i++) f[i] = i; printf("%d\n",kruskal(m)); } // P; return 0; }
相关文章推荐
- 最小生成树 克丽丝卡尔算法 hdu1879 继续畅通工程
- ACM-最小生成树之继续畅通工程——hdu1879
- 继续畅通工程--hdu1879(最小生成树 模板题)
- hdu1879继续畅通工程(最小生成树kru算法)
- HDU1879最小生成树 通畅工程
- hdu1879及最小生成树模板
- Hdu1879 - 继续畅通工程 - 最小生成树
- 最小生成树 HDU1879
- HDU1879(最小生成树)
- 最小生成树几道模版题(hdu1301 hdu1879)
- hdu1879 继续畅通工程 (kruskal求最小生成树)
- hdu1879(最小生成树)
- hdu1879 继续畅通工程(最小生成树)
- HDU1879--继续通畅工程--最小生成树--并查集
- ACM-最小生成树之继续畅通project——hdu1879
- 树:最小生成树-两种算法
- 《算法导论》学习总结 — XX.第23章 最小生成树
- 【数据结构】最小生成树
- codeforces 76A Gift 最小生成树
- 最小生成树,继续水~