Civil and Evil Engineer

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

这个题目说实话不简单啊,求出最小生成树和最大生成树再求和,关键是下标要对应好,而且要对最小生成树理解非常深刻的前提下才能快速的写出自己的代码,找到最小生成树与最大生成树的区别,关键是相反的思路,恰好求出最小生成树之后复制代码,修改成最大生成树,这个题目调了好长时间,而且在“子路”的帮助下才写对的,当时比赛时看的是最小生成树的问题,但是感觉成功的可能性太小所以一直没去写,思路没有完善好。此题过后我对最小生成树有了深刻的理解,想变通题目是一件非常困难的事情。

<pre class="html" name="code">#include <stdio.h>
#include <iostream>
#include <string.h>

const int inf = 1e6;

using namespace std;

int map[1000][1000],Map[1000][1000];
int dis[1000],vist[1000];
int MIN = 0;
int MAX = 0;
int n;

void prim()
{
    for(int i = 0; i<=n; i++)
    {
        vist[i]=0;
        dis[i] = map[0][i];
    }
    vist[0] = 1 ;
    for(int i = 1; i<=n; i++)
    {
        int min = inf;
        int pos = 0;
        for(int j = 0; j<=n; j++)
        {
            if(!vist[j] && dis[j] < min)
            {
                min = dis[j];
                pos = j;
            }
        }
        if(min==inf)
        {
            break;
        }
        MIN += min;
        vist[pos] = 1 ;
        for(int j = 0; j<=n; j++)
        {
            if(!vist[j]  && dis[j] > map[pos][j])
            {
                dis[j] = map[pos][j];
            }
        }
    }
}

void prim1()
{
    for(int i = 0; i<=n; i++)
    {
        vist[i]=0;
        dis[i] = Map[0][i];
    }
    vist[0] = 1 ;
    for(int i = 1; i<=n; i++)
    {
        int min = 0;
        int pos = 0;
        for(int j = 0; j<=n; j++)
        {
            if(!vist[j] && dis[j] > min)
            {
                min = dis[j];
                pos = j;
            }
        }
        if(min==0)
        {
            break;
        }
        MAX += min;
        vist[pos] = 1 ;
        for(int j = 0; j<=n; j++)
        {
            if(!vist[j]   && dis[j] < Map[pos][j])
            {
                dis[j] = Map[pos][j];
            }
        }
    }
}

int main()
{
    int a,b,c,t;
    int k = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(Map,0,sizeof(Map));
        for(int i=0; i<=n; i++)
        {
            for(int j=0; j<=n; j++)
            {
                map[i][j]=inf;
            }
        }
        while(~scanf("%d%d%d",&a,&b,&c))
        {
            if(a==0 &&b==0 &&c==0)
                break;
            if(c<map[a][b])
                map[a][b] = map[b][a] = c;
            if(c>Map[a][b])
                Map[a][b] = Map[b][a] = c;
        }
        MIN = 0;
        MAX = 0;
        prim();
        prim1();

        // printf ("%d %d\n",MIN,MAX);
        printf ("Case %d: ",k);
        k++;
        if ((MIN + MAX) % 2 == 0)
            printf ("%d\n",(MIN + MAX)/2);
        else printf ("%d/2\n",MAX+MIN);
    }
    return 0;
}

 


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值