Description
A Civil Engineer is given a task to connect n houses with the main electric power station directly or indirectly. The Govt has given him permission to connect exactly n wires to connect all of them. Each of the wires connects either two houses, or a house and the power station. The costs for connecting each of the wires are given.
Since the Civil Engineer is clever enough and tries to make some profit, he made a plan. His plan is to find the best possible connection scheme and the worst possible connection scheme. Then he will report the average of the costs.
Now you are given the task to check whether the Civil Engineer is evil or not. That's why you want to calculate the average before he reports to the Govt.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of houses. You can assume that the houses are numbered from 1to n and the power station is numbered 0. Each of the next lines will contain three integers in the form u v w (0 ≤ u, v ≤ n, 0 < w ≤ 10000, u ≠ v)meaning that you can connect u and v with a wire and the cost will be w. A line containing three zeroes denotes the end of the case. You may safely assume that the data is given such that it will always be possible to connect all of them. You may also assume that there will not be more than 12000lines for a case.
Output
For each case, print the case number and the average as described. If the average is not an integer then print it in p/q form. Where p is the numerator of the result and q is the denominator of the result; p and q are relatively-prime. Otherwise print the integer average.
Sample Input
3
1
0 1 10
0 1 20
0 0 0
3
0 1 99
0 2 10
1 2 30
2 3 30
0 0 0
2
0 1 10
0 2 5
0 0 0
Sample Output
Case 1: 15
Case 2: 229/2
Case 3: 15
求最大生成树和最小生成树。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 #define INF 999999999 5 int map[101][101],visit[101],dis[101],N,mapb[101][101]; 6 int prim() 7 { 8 for(int i=1;i<=N;i++) 9 { 10 visit[i] = 0; 11 dis[i]=map[i][0]; 12 } 13 visit[0] = 1; 14 int sum = 0; 15 for(int i=0;i<=N-1; i++) 16 { 17 int temp=INF,pos; 18 for(int j=0;j<=N;j++) 19 { 20 if(!visit[j]&&dis[j]<temp) 21 { 22 temp=dis[j]; 23 pos=j; 24 } 25 } 26 visit[pos] = 1; 27 sum+=dis[pos]; 28 for(int j=0;j<=N;j++) 29 { 30 if(!visit[j]&&dis[j]>map[pos][j]) 31 dis[j]=map[pos][j]; 32 } 33 } 34 return sum; 35 } 36 int prim1() 37 { 38 for(int i=1;i<=N; i++) 39 { 40 visit[i] = 0; 41 dis[i]=mapb[i][0]; 42 } 43 visit[0]=1; 44 int sum=0; 45 for(int i=0;i<=N-1; i++) 46 { 47 int temp=-100,pos; 48 for(int j=0;j<=N;j++) 49 { 50 if(!visit[j]&&dis[j]>temp) 51 { 52 temp=dis[j]; 53 pos=j; 54 } 55 } 56 visit[pos]=1; 57 sum+=dis[pos]; 58 for(int j=0;j<=N;j++) 59 { 60 if(!visit[j]&&dis[j]<mapb[pos][j]) 61 dis[j]=mapb[pos][j]; 62 } 63 } 64 return sum; 65 } 66 int main() 67 { 68 int T,s,ss,d=1,i,j; 69 scanf("%d",&T); 70 while(T--) 71 { 72 int u,v,c; 73 scanf("%d",&N); 74 for(int i=0;i<=N;i++) 75 dis[i]=INF; 76 for(i=0;i<=N;i++) 77 { 78 for(j=0;j<=N;j++) 79 { 80 map[i][j]=INF; 81 mapb[i][j]=0; 82 } 83 } 84 while(scanf("%d%d%d",&u,&v,&c)!=EOF&&(u!=0||v!=0||c!=0)) 85 { 86 if(map[u][v]>c)map[u][v]=map[v][u]=c; 87 if(mapb[u][v]<c)mapb[u][v]=mapb[v][u]=c; 88 } 89 s=prim(); 90 ss=prim1(); 91 if((s+ss)%2==0) 92 printf("Case %d: %d\n",d++,(s+ss)/2); 93 else 94 printf("Case %d: %d/2\n",d++,(s+ss)); 95 } 96 }