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


    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    【机器学习】机器学习12个关键经验教训
    【机器学习】24个终极项目提升您的机器学习知识和技能
    2018-12-21-WPF-弹出-popup-里面的-TextBox-无法输入汉字
    2019-10-31-C#-dotnet-获取整个局域网的-ip-地址
    2018-11-26-win10-uwp-获取窗口的坐标和宽度高度
    2019-5-21-dotnet-core-使用-CoreRT-将程序编译为-Native-程序
    2019-5-21-Roslyn-使用-Directory.Build.props-管理多个项目配置
    2019-2-26-SublimeText-快速打开当前文件的文件夹
    2019-2-18-VisualStudio-给项目添加特殊的-Nuget-的链接
    2019-8-31-dotnet-如何在-Mock-模拟-Func-判断调用次数
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9280124.html
Copyright © 2011-2022 走看看