http://acm.hdu.edu.cn/showproblem.php?pid=1879
prime算法

1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 #define INF 0x3f3f3f3f 5 using namespace std; 6 int w[101][101],visit[101],low[101],sum = 0; 7 void prime(int n) 8 { 9 int i,j,k,m; 10 memset(visit,0,sizeof(visit)); 11 visit[1] = 1; 12 for(i = 2 ;i <= n ; i++) 13 low[i] = w[1][i]; 14 for(i = 1 ; i <= n ;i++) 15 { 16 m = INF; 17 for(j = 1; j <= n ; j++) 18 if(!visit[j]&&m>low[j]) 19 m = low[k=j]; 20 if(m == INF) 21 break; 22 sum+=m; 23 visit[k] = 1; 24 for(j = 1; j <= n ; j++) 25 if(!visit[j]&&low[j]>w[k][j]) 26 low[j] = w[k][j]; 27 } 28 } 29 int main() 30 { 31 int i,j,k,n,m,a,b,c,d; 32 while(scanf("%d",&n)&&n) 33 { 34 k = n*(n-1)/2; 35 memset(w,INF,sizeof(w)); 36 sum = 0; 37 while(k--) 38 { 39 scanf("%d%d%d%d",&a,&b,&c,&d); 40 if(d==1) 41 { 42 w[a][b] = 0; 43 w[b][a] = 0; 44 } 45 else 46 { 47 if(w[a][b]>c) 48 { 49 w[a][b] = c; 50 w[b][a] = c; 51 } 52 } 53 } 54 prime(n); 55 printf("%d\n",sum); 56 } 57 return 0; 58 }
Kruskal

1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 #include<algorithm> 5 struct node 6 { 7 int u,v,w; 8 }q[5001]; 9 int father[101]; 10 using namespace std; 11 bool cmpp(struct node a,struct node b) 12 { 13 return a.w<b.w; 14 } 15 int find(int x) 16 { 17 if(x!=father[x]) 18 father[x] = find(father[x]); 19 return father[x]; 20 } 21 int main() 22 { 23 int i,j,k,f,n,m,o; 24 while(scanf("%d", &n)&&n) 25 { 26 for(i = 1; i <= n ; i++) 27 father[i] = i; 28 int num = 0; 29 int sum = 0; 30 for(i = 1; i <= n*(n-1)/2 ; i++) 31 { 32 scanf("%d%d%d%d",&q[i].u,&q[i].v,&q[i].w,&o); 33 if(o==1) 34 { 35 if(find(q[i].u)!=find(q[i].v)) 36 { 37 father[find(q[i].u)] = find(q[i].v); 38 num++; 39 } 40 } 41 } 42 sort(q+1,q+n*(n-1)/2,cmpp); 43 for(i = 1; i <= n*(n-1)/2&&num<n ; i++) 44 if(find(q[i].u)!=find(q[i].v)) 45 { 46 father[find(q[i].u)] = find(q[i].v); 47 sum+=q[i].w; 48 num++; 49 } 50 printf("%d\n",sum); 51 } 52 return 0; 53 }
http://acm.hdu.edu.cn/showproblem.php?pid=1301
Kruskal

1 #include <iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<string.h> 5 using namespace std; 6 struct node 7 { 8 int x,y,w; 9 }y[100]; 10 int father[100],num,sum; 11 bool cmpp(struct node a,struct node b) 12 { 13 return a.w<b.w; 14 } 15 int find(int x) 16 { 17 if(x!=father[x]) 18 father[x] = find(father[x]); 19 return father[x]; 20 } 21 void union1(int x,int y,int z) 22 { 23 father[x] = y; 24 sum+=z; 25 num++; 26 } 27 int main() 28 { 29 int n,m,i,j,k,g,co[30]; 30 char c1,c2; 31 while(scanf("%d",&n)&&n) 32 { 33 g = 0; 34 num = 0; 35 sum = 0; 36 int x = 0; 37 memset(co,0,sizeof(co)); 38 x = n; 39 n--; 40 while(n--) 41 { 42 scanf("%*c%c %d",&c1,&m); 43 co[c1-'A']++; 44 while(m--) 45 { 46 g++; 47 scanf("%*c%c %d",&c2,&k); 48 co[c2-'A']++; 49 y[g].x = c1-'A'; 50 y[g].y = c2-'A'; 51 y[g].w = k; 52 } 53 } 54 sort(y+1,y+g+1,cmpp); 55 for(i = 0 ; i < x ; i++) 56 father[i] = i; 57 for(i = 1; i <= g&&num<x ; i++) 58 { 59 if(find(y[i].x)!=find(y[i].y)) 60 { 61 union1(find(y[i].x),find(y[i].y),y[i].w); 62 } 63 } 64 printf("%d\n",sum); 65 } 66 return 0; 67 }