次小生成树-------K - The Unique MST

Given a connected undirected graph, tell if its minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V’, E’), with the following properties:

  1. V’ = V.
  2. T is connected and acyclic.

Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E’) of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E’.
Input
The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.
Output
For each input, if the MST is unique, print the total cost of it, or otherwise print the string ‘Not Unique!’.
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!

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

算法实现
prim算法实现:
我们在求解最小生成树的时候我们要使用一个二位数组maxd[i][j]表示最小生成树中i点到j点的最大边权,我们使用动态规划的思想来计算这个数组,比如当前节点为x,他的父亲节点为per[x],以及根节点root,那么
maxd[root][x] = max(maxd[root][per[x]] , maxd[per[x]][x]);

我们就会得到最终的结果数组
我们还需要数组:connect[i][j]表示最小生成树中这条边有没有被用到,剩下的就是我们要去模拟算法解释里所说的删边以及添边的操作了

这道题,算出最小生成树与次小生成树,如果相等,就存在多条。。。。

就当总结个次小生成树的模板

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 105;

int lowcost[N];
int mk[N][N];
bool vis[N];
int pre[N];
bool use[N][N];
int MAX[N][N];
int n,m;

int prim()
{
    memset(vis,false,sizeof(vis));
    memset(MAX,0,sizeof(MAX));
    memset(use,false,sizeof(use));
    for(int i = 1;i <= n;++i){
        lowcost[i] = mk[1][i];
        pre[i] = 1;
    }
    lowcost[1] = 0;
    vis[1] = true;
    pre[1] = -1;
    int ans = 0;
    for(int i = 0;i < n - 1;++i)
    {
        int MIN = inf;
        int k = 0;
        for(int j = 1;j <= n;++j){
            if(!vis[j] && MIN > lowcost[j]){
                MIN = lowcost[j];
                k = j;
            }
        }
        if(MIN == inf){
            return -1;
        }
        vis[k] = true;
        ans += MIN;
        use[k][pre[k]] = use[pre[k]][k] = true;
        for(int j = 1;j <= n;++j){
            //这里的j!=k一定要加,MAX[j][j]一直在变,会影响MAX[j][k],像求最小生成树中的相邻的两个点的MAX值
            if(vis[j] && j != k) MAX[j][k] = MAX[k][j] = max(MAX[j][pre[k]],MIN);
            if(!vis[j] && lowcost[j] > mk[k][j]){
                lowcost[j] = mk[k][j];
                pre[j] = k;
            }
        }
    }
    return ans;
}

int ans;
int smst()
{
    int MIN = inf;
    for(int i = 1;i <= n;++i){
        for(int j = i + 1;j <= n;++j){
            if(!use[i][j] && mk[i][j] != inf){
                MIN = min(MIN,ans - MAX[i][j] + mk[i][j]);
            }
        }
    }
    //不存在第二棵生成树
    if(MIN == inf) return -1;
    return MIN;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        memset(mk,inf,sizeof(mk));
//        for(int i = 1;i <= n;++i){
//            printf("%s\n",s[i]);
//        }
        for(int i = 0;i < m;++i){
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            if(mk[u][v] > w){
                mk[u][v] = mk[v][u] = w;
            }
        }
        ans = prim();
        if(ans == -1){
            printf("Not Unique!\n");
            continue;
        }
        if(ans == smst()){
            printf("Not Unique!\n");
        }else{
            printf("%d\n",ans);
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值