首页 > 代码库 > POJ1679(次小生成树)

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

23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2

Sample Output

3Not 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(次小生成树)