Lightoj 1029 Civil and Evil engineer minimum spanning tree and maximum spanning tree

Source: Internet
Author: User

F-civil and Evil Engineer Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%LLD &%llusubmit Status Practice Lightoj 1029

Description

A civil Engineer are given a task to connect n houses with the main electric power station directly or indirectly. The Govt have given him permission to connect exactly n wires to connect all of them. Each of the wires connects either the houses, or a house and the power station. The costs for connecting each of the wires is given.

Since The civil Engineer was clever enough and tries to make some profit, he made a plan. The best possible connection scheme and the worst possible connection scheme of his plan are to find. Then he'll report the average of the costs.

Now you are given the task to check whether the civil Engineer was 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 be numbered from 1to N and the power station is numbered 0. Each of the next lines would contain three integers in the form u v W (0≤u, v≤n, 0 < w≤10000, u≠v)Meani ng that you can connect u and v with a wire and the cost would be W. A line containing three zeroes denotes the end of the case. Safely assume that the data are given such that it'll always be possible to connect all of them. Also assume that there'll is not being more than 12000lines for a case.

Output

For each case, print the case number and the average as described. If The average is not a 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

Maximum spanning tree and minimum spanning tree.

1#include <iostream>2#include <cstdio>3 using namespacestd;4 #defineINF 9999999995 intmap[101][101],visit[101],dis[101],n,mapb[101][101];6 intPrim ()7 {8      for(intI=1; i<=n;i++)9     {TenVisit[i] =0; Onedis[i]=map[i][0]; A     } -visit[0] =1; -     intsum =0; the      for(intI=0; i<=n-1; i++) -     { -         inttemp=Inf,pos; -          for(intj=0; j<=n;j++) +         { -             if(!visit[j]&&dis[j]<temp) +             { Atemp=Dis[j]; atpos=J; -             } -         } -Visit[pos] =1; -sum+=Dis[pos]; -          for(intj=0; j<=n;j++) in         { -             if(!visit[j]&&dis[j]>Map[pos][j]) todis[j]=Map[pos][j]; +         } -     } the     returnsum; * } $ intprim1 ()Panax Notoginseng { -      for(intI=1;i<=n; i++) the     { +Visit[i] =0; Adis[i]=mapb[i][0]; the     } +visit[0]=1; -     intsum=0; $      for(intI=0; i<=n-1; i++) $     { -         inttemp=- -, POS; -          for(intj=0; j<=n;j++) the         { -             if(!visit[j]&&dis[j]>temp)Wuyi             { thetemp=Dis[j]; -pos=J; Wu             } -         } Aboutvisit[pos]=1; $sum+=Dis[pos]; -          for(intj=0; j<=n;j++) -         { -             if(!visit[j]&&dis[j]<Mapb[pos][j]) Adis[j]=Mapb[pos][j]; +         } the     } -     returnsum; $ } the intMain () the { the     intT,s,ss,d=1, I,j; thescanf"%d",&T); -      while(t--) in     { the         intu,v,c; thescanf"%d",&N); About          for(intI=0; i<=n;i++) thedis[i]=INF; the              for(i=0; i<=n;i++) the             { +                  for(j=0; j<=n;j++) -                 { themap[i][j]=INF;Bayimapb[i][j]=0; the                 } the             } -          while(SCANF ("%d%d%d", &u,&v,&c)!=eof&& (u!=0|| v!=0|| c!=0)) -         { the             if(map[u][v]>c) map[u][v]=map[v][u]=C; the             if(mapb[u][v]<c) mapb[u][v]=mapb[v][u]=C; the         } thes=Prim (); -ss=prim1 (); the         if((S+SS)%2==0) theprintf"Case %d:%d\n", d++, (S+SS)/2); the         Else94printf"Case %d:%d/2\n", d++, (s+ss)); the     } the}
View Code

Lightoj 1029 Civil and Evil engineer minimum spanning tree and maximum spanning tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.