首页 > 其他 > 详细

poj 1679(次小生成树)

时间:2016-05-10 18:34:23      阅读:246      评论:0      收藏:0      [点我收藏+]
The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26454   Accepted: 9457

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!
题意:判断最小生成树是否唯一
题解:直接求次小生成树。
次小生成树求法戳这里:http://blog.csdn.net/niushuai666/article/details/6925258
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = 505;
const int INF = 99999999;
int graph[N][N];
int n,m;
int path[N][N],pre[N],low[N]; ///path[i][j]用于记录i到j路径上的权值最大的边
bool vis[N],used[N][N];
int prim(int pos,int n){
    memset(used,false,sizeof(used));
    memset(vis,false,sizeof(vis));
    memset(path,0,sizeof(path));
    vis[pos]=true;
    int cost = 0;
    for(int i=1;i<=n;i++){
        low[i]= graph[pos][i];
        pre[i]=1;
    }
    low[pos]=1;
    for(int i=1;i<n;i++){
        int Min = INF;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&low[j]<Min){
                pos = j;
                Min = low[j];
            }
        }
        used[pre[pos]][pos] = used[pos][pre[pos]] = true;
        cost+= Min;
        vis[pos] = true;
        for(int j=1;j<=n;j++){
            if(vis[j]&&j!=pos){ ///求从pos - j路径上最大权的边
                path[pos][j] = path[j][pos] = max(low[pos],path[j][pre[pos]]);
            }
            if(!vis[j]&&low[j]>graph[pos][j]){
                low[j]=graph[pos][j];
                pre[j] = pos;
            }
        }
    }
    return cost;
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++) {
                if(i==j) graph[i][j]=0;
                else graph[i][j] = INF;
            }
        }
        for(int i=0;i<m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            graph[a][b] = graph[b][a] = c;
        }
        int cost = prim(1,n);
        int res = INF;
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                if(!used[i][j]) res = min(res,cost+graph[i][j]-path[i][j]);
            }
        }
        if(res==cost) printf("Not Unique!\n");
        else printf("%d\n",cost);
    }
}

 

poj 1679(次小生成树)

原文:http://www.cnblogs.com/liyinggang/p/5478600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!