次小生成树 POJ 1679

来源:互联网 发布:金融软件开发商 编辑:程序博客网 时间:2024/05/18 00:23
The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 18034 Accepted: 6251

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!

Source

POJ Monthly--2004.06.27 srbga@POJ

次小生成树的题,是国家集训队汪汀写的《最小生成树问题的扩展》里面的算法,复杂度是O(V2+ElogE),利用了动态规划;
另外《图论及应用》书里里面有示例代码
代码如下:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <fstream>using namespace std;const int maxn = 100+5;const int maxe = 100*(100-1)/2+5;const int inf = 1<<30;typedef struct edge{    int a,b;    int w;}edge;typedef struct link{    int to;    int next;}link;edge e[maxe];link lnk[maxn];int n,m,p[maxn],head[maxn],end[maxn];int length[maxn][maxn];bool select[maxe];int find(int x){    if(p[x]<0)        return x;    else        return p[x]=find(p[x]);}bool cmp(edge c,edge d) { return c.w<d.w; }int kruskal(){    int ans=0;    memset(p,-1,sizeof(int)*(n+1));    memset(select,0,sizeof(bool)*(n+1));    for(int j=1;j<=n;j++)    {        lnk[j].to=j;        lnk[j].next=-1;        head[j]=j;        end[j]=j;    }    sort(e+1,e+m+1,cmp);    for(int i=1;i<=m;i++)    {        int x = find(e[i].a);        int y = find(e[i].b);        if(x!=y)        {            for(int u=head[x];u!=-1;u=lnk[u].next)                for(int v=head[y];v!=-1;v=lnk[v].next)                    length[lnk[u].to][lnk[v].to]=length[lnk[v].to][lnk[u].to]=e[i].w;            lnk[end[x]].next=head[y];            end[x]=end[y];            head[y]=head[x];            select[i]=true;            p[y]=x;            ans+=e[i].w;        }    }    return ans;}int main(){    //freopen("in.txt","r",stdin);    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        for(int i=1;i<=m;i++)            scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].w);        int mst = kruskal();        int secmst = inf;        int temp;        for(int i=1;i<=m;i++)        {            if(!select[i])            {                temp=mst+e[i].w-length[e[i].a][e[i].b];                if(secmst>temp)                    secmst=temp;            }        }        if(secmst==mst)            printf("Not Unique!\n");        else            printf("%d\n",mst);    }}