uva10462(次小生成树)

题目:

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

分析:利用克鲁斯卡尔算法求出最小生成树,然后试探删除加在最小生成树的n-1条边的每一条,来决定最小值。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 205;
const int maxm = 105;
int pre[maxm];
int vis[maxn];

struct Edge
{
    int u;
    int v;
    int cost;
}edge[maxn];

int Find(int x)
{
    return pre[x] == -1 ? x : x = Find(pre[x]);
}

bool cmp(Edge a,Edge b)
{
    return a.cost < b.cost;
}

int Kruskal(int n,int m,int point)
{
    int ans = 0;
    int cnt = 0;
    memset(pre,-1,sizeof (pre));
    for(int i = 1;i <= m;i ++)
    {
        if(i == point) continue; // 该条边是生成树的边
        int u = edge[i].u;
        int v = edge[i].v;
        int w = edge[i].cost;
        int tu = Find(u);
        int tv = Find(v);
        if(tu != tv)
        {
            pre[tu] = tv;
            ans += w;
            ++ cnt;
            if(point == -1) vis[cnt] = i;//是求最小生成树,就将这个点加进去。
            if(cnt == n - 1)return ans;
        }
    }
    if(cnt < n - 1) return INF;
    else return ans;
}

int main()
{
    int t;
    int n,m;
    scanf("%d",&t);
    int Case = 0;
    while(t --)
    {
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= m;i ++)
        {
            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].cost);
        }
        sort(edge + 1,edge + 1 + m,cmp);
        int tmp = INF,ans = INF;
        tmp = Kruskal(n,m,-1);
        for(int i = 1;i <= n - 1;i ++)
        {
            int mm = Kruskal(n,m,vis[i]);
            ans = min(ans,mm);
        }
        if(tmp == INF)//应该先判断tmp,因为如果tmp == INF , ans === INF 这样应该是
        printf("Case #%d : No way\n",++ Case);//没有路,如果将ans放在前就是 no second way;

        else if(ans == INF) printf("Case #%d : No second way\n",++ Case);
        else printf("Case #%d : %d\n",++ Case,ans);
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值