ZOJ 1542//POJ1861这题给的样例有点坑爹啊!木有按照样例输出,居然过了。啥破题啊!样例与实际的题意不符,这是误导了好多孩纸啊!

题目的本意应该是叫你求出最小生成树的最大边,和边的个数,最后把每条边的连接情况打印出来。

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<math.h>
 5 #define N 15001
 6 
 7 int father[1001];//按照模板敲的
 8 int  p[N], n, m;
 9 typedef struct 
10 {
11    int x, y;
12    int w; 
13 }edge;
14 edge e[N];
15 
16 int cmp(const void *a,const void *b)
17 {
18     return (*(edge *)a).w > (*(edge *)b).w ? 1 : -1;
19 }   
20     
21 int Make_set(int x)
22 {
23     father[x] = x;    
24 }    
25     
26 int Find_set(int x)
27 {
28     if(x != father[x] )
29       father[x] = Find_set(father[x]);    
30     return father[x];
31 } 
32  
33 void Kruskal()
34 {    
35    int i,a,b,count,max;  
36    qsort(e+1,m,sizeof(edge),cmp);  
37    for(i=1; i<=n; i++)
38     Make_set(i);
39    max = -1; count = 0;
40    for(i=1; i<=m; i++)  
41    {  
42       a = Find_set(e[i].x); 
43       b = Find_set(e[i].y);
44       if(a != b)  
45       {
46         p[++count] = i;  
47         father[a] = b;
48         if(e[i].w > max)
49           max = e[i].w;
50       }
51     } 
52    printf("%d\n%d\n",max,count);  
53    for(i=1; i<=count; i++)
54    {
55       printf("%d %d\n",e[ p[i] ].x,e[ p[i] ].y);       
56    } 
57 }     
58 
59 int main()
60 {
61    int i;
62      
63   while(scanf("%d%d", &n, &m) !=  EOF)
64   { 
65      for(i=1; i<=m; i++)
66      { 
67        scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
68      } 
69      Kruskal(); 
70    }    
71   // system("pause");
72    return 0;
73 }
posted @ 2012-04-19 23:29  zhongya  阅读(175)  评论(0编辑  收藏  举报