UVA 10462 Is There A Second Way Left?

本文介绍了一个涉及最小生成树(MST)及其次优解的问题。NASA的一位程序员希望找到连接网络中邻居节点所需的电缆成本最小方案,并且还需要知道第二优的成本方案。文章提供了使用Kruskal算法解决此问题的方法及代码实现。

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

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 相当于求出最小生成树之后,删去生成树的某条边再求最小生成树

代码

//============================================================================
// Name        : tree.cpp
// Author      : Qihan
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define Lowbit(x) (x & (-x))
#define lson l,mid,rt << 1
#define rson mid + 1,r,rt << 1 | 1
#define inf 0x3f3f3f3f
#define maxn (100 + 10)
typedef long long int LLI;
typedef pair<int,int> PII;

struct Edge {
    int u,v;
    int w;
    Edge() {}
    Edge(int _u,int _v,int _w):u(_u),v(_v),w(_w) {}
} edge[maxn * maxn];

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

int Map[maxn];
int e[maxn];


int find(int x) {
    if(Map[x] == -1)    return x;
    return Map[x] = find(Map[x]);
}

int Kruskal(int n,int m,int p) {
    memset(Map,-1,sizeof(Map));
//    sort(edge,edge + m,cmp);
    int cnt = 0;
    int ans = 0;
    for(int i = 0; i < m; i ++) {
        if(i == p)  continue;
        int u = edge[i].u;
        int v = edge[i].v;
        int w = edge[i].w;
        u = find(u);
        v = find(v);
        if(u != v) {
            ans += w;
            Map[u] = v;
            if(p == -1) e[cnt] = i;
            cnt ++;
        }
        if(cnt == n - 1)    break;
    }
    if(cnt < n - 1) return -1;
    return ans;
}


int main() {
//    freopen("/home/qihan/Documents/in","r",stdin);
    int t;
    scanf("%d",&t);
    for(int ca = 1; ca <= t; ca ++) {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i = 0; i < m; i ++) {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            edge[i] = Edge(x,y,z);
        }
        sort(edge,edge + m,cmp);
        int ans = Kruskal(n,m,-1);
        int mins = -1;
        for(int i = 0; i < n - 1; i ++) {
            int temp = Kruskal(n,m,e[i]);
            if(temp == -1)  continue;
            if(mins == -1)  mins = temp;
            else            mins = min(mins,temp);
        }
        if(ans == -1) {
            printf("Case #%d : No way\n",ca);
        } else if(mins == -1) {
            printf("Case #%d : No second way\n",ca);
        } else {
            printf("Case #%d : %d\n",ca,mins);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值