Civil and Evil Engineer(最小生成树+最大生成树)

来源:互联网 发布:菜鸟网络物流公司官网 编辑:程序博客网 时间:2024/05/22 15:44

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 1 to 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 12000 lines 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 inp/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

求最小生成树和最大生成树然后取平均值

#include <stdio.h>#include <string.h>#include <stdlib.h>#define inf 9999999999int map1[110][110];int map2[110][110];int vis[110];int dis[110];int prim_max(int n){    int i,j,max,k;    int sum1=0;    for(i=0;i<=n;i++)    {        dis[i]=map1[i][0];        vis[i]=0;    }    vis[0]=0;    for(i=1;i<=n;i++)    {        max=-1;        for(j=1;j<=n;j++)            if(max<dis[j]&&!vis[j])            {                max=dis[j];                k=j;            }        if(max==-1)            break;        vis[k] = 1;        sum1+=max;        for(j=1;j<=n;j++)            if(dis[j]<map1[k][j]&&!vis[j])                dis[j]=map1[k][j];    }    return sum1;}int prim_min(int n){    int i,j,min,k;    int sum2=0;    for(i=0;i<=n;i++)    {        dis[i]=map2[i][0];        vis[i]=0;    }    vis[0] = 0;    for(i=1;i<=n;i++)    {        min=inf;        for(j=1;j<=n;j++)            if(min>dis[j]&&!vis[j])                {                min=dis[j];                k=j;                }        if(min==inf)            break;        vis[k]=1;        sum2+=min;        for(j=1;j<=n;j++)            if(dis[j]>map2[k][j]&&!vis[j])                dis[j] =map2[k][j];    }    return sum2;}int main(){    int T,n,i,j;    int u,v,w;    int sum1=0,sum2=0;    int count=1;    scanf("%d",&T);    while(T--)        {        scanf("%d",&n);        memset(map1,0,sizeof(map1));        memset(map2,0,sizeof(map2));        for(i=0;i<=n;i++)            for(j=0;j<=n;j++)            {                map1[i][j]=0;                map2[i][j]=inf;            }        while(~scanf("%d %d %d",&u,&v,&w))        {            if(u==0&&v==0&&w==0)                 break;            if(!map1[u][v])                map1[u][v]=map1[v][u]=w;            if(map1[u][v]<w)                map1[u][v]=map1[v][u]=w;            if(map2[u][v]>w)                map2[u][v]=map2[v][u]=w;        }        sum1=prim_max(n);        sum2=prim_min(n);        printf("Case %d: ",count);        if((sum1+sum2)%2==0)            {                printf("%d\n",(sum1+sum2)/2);            }        else            {            printf("%d/2\n",sum1+sum2);        }        count++;    }    return 0;}




0 0
原创粉丝点击