zoukankan      html  css  js  c++  java
  • The Unique MST POJ

    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


    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    代码搭建记事本框架(一)
    代码搭建记事本框架(二)
    ios中图片拉伸用法
    ios启动载入启动图片
    Top-k test
    leetcode : jump game
    leetcode : Top k frequent elements
    一个月没有更新了
    leetcode : Reverse Linked List II [two pointers]
    leetcode : reverse linked list [基本功,闭着眼也要写出来]
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9280124.html
Copyright © 2011-2022 走看看