码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-07-08 16:40:47      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:cin   one   repr   ++   div   class   sum   --   mst   

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!


求次小生成树 看与最小生成树是否相同
prime求次小生成树

技术分享图片
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 10010, INF = 0x7fffffff;
typedef long long LL;
int graph[510][510], d[maxn], vis[maxn], maxd[510][510], pre[maxn];
int n, m;

int prime(int s)
{
    int temp, sum = 0;
    mem(vis, 0);
    for(int i=1; i<=n; i++) d[i] = graph[s][i], pre[i] = s;
    vis[s] = 1;
    d[s] = 0;
    for(int i=1; i<n; i++)
    {
        int mincost = INF;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j] && mincost > d[j])
                mincost = d[j], temp = j;
        }
        for(int j=1; j<=n; j++)
            if(vis[j]) maxd[temp][j] = maxd[j][temp] = max(mincost, maxd[pre[temp]][j]);
        vis[temp] = 1;
        sum += mincost;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j] && d[j] > graph[temp][j])
                d[j] = graph[temp][j], pre[j] = temp;
        }
    }
//    for(int i=1; i<=n; i++)
//        sum += d[i];
    return sum;
}


int main()
{
    int T;
    cin>> T;
    while(T--)
    {
        cin>> 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] = graph[j][i] = INF;
        for(int i=0; i<m; i++)
        {
            int u, v, w;
            cin>> u >> v >> w;
            graph[u][v] = graph[v][u] = w;
        }
        int sum = prime(1);
        int lsum = INF;
        for(int i=1; i<=n; i++)
            for(int j=i+1; j<=n; j++)
            {
            if(i != pre[j] && j != pre[i]  && graph[i][j] != INF)
                if(sum - maxd[i][j] + graph[i][j] < lsum)
                    lsum = sum - maxd[i][j] + graph[i][j];
            }

        if(lsum == sum)
            cout<< "Not Unique!" <<endl;
        else
            cout<< sum <<endl;

    }



    return 0;
}
View Code

 



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

标签:cin   one   repr   ++   div   class   sum   --   mst   

原文地址:https://www.cnblogs.com/WTSRUVF/p/9280124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!