UVa 10462 Is There A Second Way Left?

Is There A Second Way Left ?

Input: standard input

Output: standard output

Time Limit: 3 seconds

 

Nasa, being the most talented programmer of his time, can‘t think things to be so simple. Recently all his neighbors have decided to connect themselves over a network (actually all of them want to share a broadband internet connection :-)). But he wants to minimize the total cost of cable required as he is a bit fastidious about the expenditure of the project. For some unknown reasons, he also wants a second way left. I mean, he wants to know the second best cost (if there is any which may be same as the best cost) for the project. I am sure, he is capable of solving the problem. But he is very busy with his private affairs(?) and he will remain so. So, it is your turn to prove yourself a good programmer. Take the challenge (if you are brave enough)..............

 

Input

Input starts with an integer t<=1000 which denotes the number of test cases to handle. Then follows t datasets where every dataset starts with a pair of integers v(1<=v<=100) and e(0<=e<=200)v denotes the number of neighbors and e denotes the number of allowed direct connections among them. The following e lines contain the description of the allowed direct connections where each line is of the form "start end cost", where start and end are the two ends of the connection and cost is the cost for the connection. All connections are bi-directional and there may be multiple connections between two ends.


Output

There may be three cases in the output - 1. no way to complete the task, 2. There is only one way to complete the task, 3. There are more than one way. Output "No way" for the first case, "No second way" for the second case and an integer c for the third case where c is the second best cost. Output for a case should start in a new line.

 

Sample Input

4
5 4
1 2 5
3 2 5
4 2 5
5 4 5
5 3
1 2 5
3 2 5
5 4 5
5 5
1 2 5
3 2 5
4 2 5
5 4 5
4 5 6
1 0

Sample Output

Case #1 : No second way
Case #2 : No way
Case #3 : 21

Case #4 : No second way


思路:这是一题带重边的次小生成树,用kruskal算法即可。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1005;
int n,m;
struct node
{
    int x,y,z;
}e[N];
int p[N];
int id[N];
bool cmp(node a,node b)
{
    return a.z<b.z;
}
int findx(int x)
{
    if(x==p[x])return x;
    return p[x]=findx(p[x]);
}
int kr()
{
    int num=0;
    int i;
    for(i=1;i<=n;i++)
    {
        p[i]=i;
    }
    int ans=0,k=0;
    for(i=0;i<m;i++)
    {
        int a=findx(e[i].x);
        int b=findx(e[i].y);
        if(a!=b)
        {
            id[k++]=i;
            p[a]=b;
            ans+=e[i].z;
        }
    }
    for(i=1;i<=n;i++)
    {
        if(i==findx(i))
        {
            num++;
        }
    }
    if(num>1)return inf;
    else return ans;
}
int kr1(int x)
{
    int num=0;
    int k=0,ans=0,i;
    for(i=1;i<=n;i++)
    {
        p[i]=i;
    }
    for(i=0;i<m;i++)
    {
        if(i==x)continue;
        int a=findx(e[i].x);
        int b=findx(e[i].y);
        if(a!=b)
        {
            p[a]=b;
            ans+=e[i].z;
        }
    }
    for(i=1;i<=n;i++)
    {
        if(i==findx(i))
        {
            num++;
        }
    }
    if(num>1)return inf;
    else return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    int k=1;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        int i;

        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z);
        }
        sort(e,e+m,cmp);
        int ans=kr();
        int ans1=inf;
        printf("Case #%d : ",k++);
        if(ans==inf)
        {
            printf("No way\n");
            continue;
        }
        for(i=0;i<n-1;i++)
        {
            int x=id[i];
            ans1=min(ans1,kr1(x));
        }
        if(ans1==inf)printf("No second way\n");
        else printf("%d\n",ans1);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值