【题解】The Unique MST POJ - 1679 ⭐⭐⭐⭐ 【次小生成树】

The Unique MST POJ - 1679

从前有两只猫。它们实在不知道该出什么题了。

于是它们放弃了治疗,开始玩一个游戏:从乡镇地图中已有的道路里面删除一些道路,并且删除完毕后图仍然是连通的。在所有方案中,删除道路总长度最大的方案为最优方案。

两只猫同时完成了这个游戏。它们都坚信自己是赢家。已知它们的完成方式不同,请判断有没有可能它们的实现方案都是最优的。

Input

第一行是一个整数 t (1 <= t <= 20), 测试用例的数量。每个用例代表一张图,第一行是n和m (1 <= n <= 100), 分别为城镇数和道路数。接下来m行为m个三元组 (xi, yi, wi),表示编号为xi和yi的城镇被长度为wi的道路连接。两个城镇之间最多被一条道路连接。

Output

对于每个用例,如果答案为否定(即不可能都是最优方案),输出最优方案剩余的(注意不是删除的)道路总长度。否则输出字符串 ‘Not Unique!’(不带引号)。

Examples

Sample Input
2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2
Sample Output
3
Not Unique!

Hint




题意:

判断最小生成树和次小生成树是否相等, 相等即输出Not Unique!

题解:

首先说一下求解次小生成树的思路
首先求出最小生成树,我们枚举每条不在最小生成树上的边,并把这条边放到最小生成树上面,然后就一定会形成环,那么我们在这条环路中取出一条最长的路(除了新加入的那一条边)。最终我们得到的权值就是次小生成树的权值。

通过改写Prim和Kruskal都可以实现, 这里我是对Prim进行改写, 需要定义几个数组
mst[i][j] : 表示该边是否已在MST中
pre[i] : 表示 i 的父节点
maxd[i][j] : 表示MST中从i -> j 的最大值

这里的重点是如何求出maxd, 这里用到了动态规划的思想
maxD[u][v] = max(maxD[pre[u]][u], d[u])

而判断最小生成树是否等于次小生成树, 枚举所有未使用的边, 判断该边<i, j> 权值是否等于maxd[i][j]即可
删的这条边必须是原来i,j两点的最大边,才有可能次小,如果求出来的要删的次小边与新添的边长度一样,那么就说明最小生成树不止一个。

经验小结:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int inf = 1<<30;
const LL maxn = 110;

int N, M, w[maxn][maxn];
int d[maxn];
bool used[maxn];
int maxD[maxn][maxn];   //MST中从i->j的最大权值
int pre[maxn];          //某一点父节点
bool mst[maxn][maxn];   //该点是否已经在MST中
typedef pair<int, int> P;
int Prim(int s){
    fill(d, d+maxn, inf); ms(maxD, 0);
    ms(used, 0); ms(mst, 0);
    fill(pre, pre+maxn, s);
    priority_queue<P, vector<P>, greater<P> > q;
    q.push(P(d[s]=0, s));
    int res = 0;
    while(!q.empty()){
        P cur = q.top();
        q.pop();
        int u = cur.second;
        if(used[u]) continue;
        used[u] = true, res += d[u];
        mst[u][pre[u]] = mst[pre[u]][u] = true; //加入到MST中
        for(int v = 1; v <= N; ++v){
            if(used[v] && w[u][v]<inf)          //只更新MST中的
                maxD[u][v] = maxD[v][u] = max(maxD[pre[u]][u], d[u]);
            if(w[u][v] < d[v]){
                d[v] = w[u][v];
                pre[v] = u;                     //更新父节点
                q.push(P(d[v], v));
            }
        }
    }
    return res;
}

int main()
{
    int T, a, b, c;
    cin >> T;
    while(T--){
        fill(w[0], w[0]+maxn*maxn, inf);
        cin >> N >> M;
        while(M--){
            cin >> a >> b >> c;
            w[a][b] = w[b][a] = c;
        }
        int ans = Prim(1);
        bool flag = false;  //次小生成树是否等于最小生成树
        for(int u = 1; u <= N && !flag; ++u){
            for(int v = 1; v <= N; ++v){
                if(mst[u][v] || w[u][v]==inf)
                    continue;
                if(w[u][v] == maxD[u][v]){
                    flag = true;
                    break;
                }
            }
        }
        if(flag) cout << "Not Unique!\n";
        else cout << ans << endl;
    }
	return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值