POJ1679(次小生成树)

Posted vCoders

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1679(次小生成树)相关的知识,希望对你有一定的参考价值。

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27651   Accepted: 9909

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!

求次小生成树步骤:
1)用prime求最小生成树,并记录树中u,v结点路径之间边的权值的最大值dp[u][v]。
2)一次枚举u,v结点不在最小生成树中的边,并将dp[u][v]删除,形成新的树。记录形成所有新树的权值的最小值。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=105;
const int INF=0x3f3f3f3f;
int arc[MAXN][MAXN];
int n,m;
int d[MAXN],vis[MAXN];
int pre[MAXN],dp[MAXN][MAXN];
bool mst[MAXN][MAXN];
int prim(int src)
{
    memset(pre,0,sizeof(pre));
    memset(mst,false,sizeof(mst));
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++)
    {
        vis[i]=0;
        d[i]=INF;
    }
    d[src]=0;
    int t=n;
    int res=0;
    while(t--)
    {
        int mincost=INF;
        int k;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&d[i]<mincost)
            {
                k=i;
                mincost=d[i];
            }    
        }
        vis[k]=1;
        res+=mincost;
        int fa=pre[k];
        mst[fa][k]=true;
        mst[k][fa]=true;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]&&i!=k)
            {
                dp[k][i]=dp[i][k]=max(dp[fa][i],mincost);
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&d[i]>arc[k][i])
            {
                d[i]=arc[k][i];
                pre[i]=k;
            }
        }
    }
    return res;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i==j)    arc[i][j]=0;
                else arc[i][j]=INF;
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            arc[u][v]=w;
            arc[v][u]=w;
        }
        int res=prim(1);
        int nmx=INF;
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(arc[i][j]!=INF&&!mst[i][j])
                {
                    nmx=min(nmx,res+arc[i][j]-dp[i][j]);
                }
            }
        }
        if(nmx==res)
        {
            printf("Not Unique!\n");
        }
        else
        {
            printf("%d\n",res);
        }
    }
    return 0;
}

 

以上是关于POJ1679(次小生成树)的主要内容,如果未能解决你的问题,请参考以下文章

POJ1679(次小生成树)

POJ 1679 次小生成树

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

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

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

次小生成树(POJ1679/CDOJ1959)