POJ 1679 The Unique MST(次小生成树之一)

28 篇文章 0 订阅

Description

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!

题目大意
对于一个无向图,判断其最小生成树是否唯一,若唯一,输出最小生成树,若不唯一,输出Not Unique!。
解题思路
次小生成数求解,对于无向图,求次小生成树,判断其值是否与最小生成树相等,若相等,则最小生成树不唯一;相反,则唯一。
次小生成树求解思想:
1、先求出最小生成树,在这一过程中,标记出最小生成树包含的边,并且保存在最小生成树中从节点i到节点j路径中的最大边权Max[i][j];
2、选取不在最小生成树中的一条边,添加进去,那么一定会构成一个环,然后除去这个环中边(除了刚添加的边)的最大值(即Max[i][j]),当前仍是无向图的一个生成树且该生成树有可能是次小生成树;
3、重复步骤2,依次将不在最小生成树中的边遍历完之后,得到的最小值便是次小生成树。
这里写图片描述
代码实现

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 207
#define INF 0x3f3f3f3f
int cost[maxn][maxn],used[maxn][maxn];
int pre[maxn],lowcost[maxn];
int Max[maxn][maxn];  //Max[i][j]表示在最小生成树中从i到j的路径中的最大边权
bool vis[maxn];
int n,m,ans;
int Prim()
{
    memset(vis,0,sizeof(vis));
    memset(lowcost,INF,sizeof(lowcost));
    memset(Max,0,sizeof(Max));
    memset(used,0,sizeof(used));
    vis[0]=1;                      //先将节点0添加到最小生成树中
    pre[0]=-1;
    int an=0;
    for(int i=1; i<n; i++)
    {
        lowcost[i]=cost[0][i];
        pre[i]=0;
    }
    for(int i=1; i<n; i++)
    {
        int Min=INF;
        int p=-1;
        for(int j=0; j<n; j++)         //寻找最短的边添加到最小生成树中
            if(!vis[j]&&Min>lowcost[j])
            {
                Min=lowcost[j];
                p=j;
            }
        an+=Min;             //最小生成树的值
        vis[p]=1;             //将节点p添加到最小生成树中
        used[p][pre[p]]=used[pre[p]][p]=1;         //标记最小生成树包含的边
        for(int j=0; j<n; j++)
        {
            if(vis[j])
                Max[j][p]=Max[p][j]=max(Max[j][pre[p]],lowcost[p]);
            else
            {
                if(lowcost[j]>cost[j][p])  //更新还未添加到最小生成树中的节点与现有的最小生成树的最短距离
                {
                    lowcost[j]=cost[j][p];
                    pre[j]=p;
                }
            }

        }
    }
    return an;
}
int smst()
{
    int cnt=INF;
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            if(cost[i][j]!=INF&&!used[i][j])       //添加不在最小生成树的边,减去环中的最大边权,更新次小生成树
            {
                cnt=min(cnt,ans+cost[i][j]-Max[i][j]);
            }
        }
    }
    return cnt;
}
int main()
{
    int T;
    int u,v,l;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        for(int i=0; i<n; i++)       //初始化
            for(int j=0; j<n; j++)
            {
                if(i==j) cost[i][j]=0;
                else cost[i][j]=INF;
            }
        for(int i=0; i<m; i++)
        {
            scanf("%d %d %d",&u,&v,&l);
            u--,v--;                         //节点从0开始
            cost[u][v]=cost[v][u]=l;
        }
        ans=Prim();           //求最小生成树
        if(ans==smst())       //求次小生成树进行比较
            printf("Not Unique!\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值