Civil and Evil Engineer

本文探讨了如何通过计算最小生成树和最大生成树来确定连接房屋到电力站的最佳和最差方案的成本,并通过示例输入输出展示了算法实现的具体过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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、付费专栏及课程。

余额充值