Civil and Evil Engineer

                                                                   Civil and Evil Engineer

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

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 from1 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 than12000 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 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

#include <iostream>

<span style="font-size:18px;">#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;
int map1[1010][1010],map2[1010][1010];
int vis[1010],d[1010];
#define Max 0x3f3f3f3f
int prime1(int n)
{
    int i,j,he,wei=0;
    memset(vis,0,sizeof(vis));
    for(i=0; i<=n; i++)
        d[i]=map1[0][i];
    he=0;
    vis[0]=1;
    for(i=0; i<n; i++)
    {
        int min=10000000;
        for(j=1; j<=n; j++)
        {
            if(vis[j]==0&&d[j]<min)
            {
                wei=j;
                min=d[j];
            }
        }
        vis[wei]=1;
        he+=min;
        for(j=1; j<=n; j++)
        {
            if(vis[j]==0&&map1[wei][j]<d[j]&&wei!=j )
            {
                d[j]=map1[wei][j];
            }
        }
    }
    return he;
}
int prime2(int n)
{
    int i,j,he,wei;
    memset(vis,0,sizeof(vis));
    for(i=0; i<=n; i++)
        d[i]=map2[0][i];
    he=0;
    vis[0]=1;
    for(i=0; i<n; i++)
    {
        int min=0;
        for(j=1; j<=n; j++)
        {
            if(vis[j]==0&&d[j]>min)
            {
                wei=j;
                min=d[j];
            }
        }
        vis[wei]=1;
        he+=min;
        for(j=1; j<=n; j++)
        {
            if(vis[j]==0&&map2[wei][j]>d[j]&&wei!=j )
            {
                d[j]=map2[wei][j];
            }
        }
    }
    return he;
}
int main()
{
    int n,i,j,t,a,b,cost,x,y;
    int k=1;
    scanf("%d",&t);
    while(t--)
    {

        scanf("%d",&n);
        memset(map1,Max,sizeof(map1));
        memset(map2,0,sizeof(map2));
        memset(vis,0,sizeof(vis));
        while(scanf("%d%d%d",&a,&b,&cost))
        {
            if(a==0&&b==0&&cost==0)
            {
                break;
            }
            if(cost<map1[a][b])
                map1[a][b]=map1[b][a]=cost;
            if(cost>map2[a][b])
                map2[a][b]=map2[b][a]=cost;

        }
        x=prime1(n);
        y=prime2(n);
        //cout<<x<<' '<<y<<endl;
        //printf("%d\n",prime1(n));
        if((x+y)%2==0)
            printf("Case %d: %d\n",k++,(x+y)/2);
        else printf("Case %d: %d/2\n",k++,x+y);
    }
}



</span></span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我很荣幸能够与大家分享一下我在“计算机辅助的土木和基础设施工程”期刊投稿的经验。我在这个领域已经从事多年,因此对于这个期刊投稿有着一些体会和心得。 首先,我认为投稿前要充分了解期刊的发表要求和范围,特别是对于“计算机辅助的土木和基础设施工程”这样的专业期刊,对于文章内容和形式都有着严格的要求。因此在投稿之前,我会仔细阅读期刊的投稿指南,确保自己的文章符合要求。 其次,我觉得在写作过程中,要注重论文的创新性和实用性。期刊的编辑和审稿人更加关注那些具有创新理念和实际应用意义的研究成果。所以,我在写作过程中会力求深入挖掘问题,提出创新的解决方案,并且充分展现研究结果的实际应用性。 最后,我认为与期刊编辑和审稿人的互动和沟通非常重要。在投稿过程中,我会积极主动地与编辑和审稿人保持联系,及时回复他们的意见和建议,以确保论文能够顺利发表。 总的来说,我觉得“计算机辅助的土木和基础设施工程”期刊是一个非常优秀的学术平台,它为广大研究者提供了一个很好的交流和展示研究成果的机会。通过我多次投稿的经验,我已经进一步提高了自己的学术写作水平和研究能力。希望未来能够有更多的学者和研究者能够通过这个期刊分享他们的研究成果,共同推动学术研究的进步。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值