zoukankan      html  css  js  c++  java
  • POJ

    The Unique MST

    Time Limit: 1000MS Memory Limit: 10000K

    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!

    题意:判断最小生成树是不是唯一。
    题解:首先判断能不能形成最小生成树,然后寻找次小生成树,判断是否等于最小生成树,如果不相等,则说明最小生成树唯一。

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    #include <stack>
    #include <map>
    
    using namespace std;
    
    const int maxn = 105;
    const int INF = 1e9+7;
    
    int s[maxn][maxn],n,m;
    
    int prim()
    {
        int i,j,MIN,k,sum = 0;
        int dis[maxn],f[maxn],pre[maxn],maxx[maxn][maxn];/*maxx用来存储最小生成树的 i 到 j 的最大边权值*/
        int use[maxn][maxn];
        for(i=1;i<=n;i++)
        {
            dis[i] = s[1][i];
            pre[i] = 1;
            f[i] = 0;
        }
        memset(maxx,0,sizeof(maxx));
        memset(use,0,sizeof(use));
        f[1] = 1;
        for(i=1;i<n;i++)
        {
            MIN = INF;
            k = -1;
            for(j=1;j<=n;j++)
                if(!f[j]&&MIN>dis[j])
                {
                    MIN = dis[j];
                    k = j;
                }
            if(MIN==INF)
                return -1;
            //printf("MIN %d
    ",MIN);
            f[k] = 1;
            sum += MIN;
            for(j=1;j<=n;j++)
            {
                if(f[j])
                    maxx[k][j] = maxx[j][k] = max(maxx[pre[k]][j],dis[k]);/*(发现直接等于dis[k]也行)*/
                else
                {
                    if(dis[j]>s[k][j])
                    {
                        dis[j] = s[k][j];
                        pre[j] = k;
                    }
                }
            }
        }
        //printf("%d
    ",sum);
        MIN = INF;
        for(i=1;i<=n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                if(i!=pre[j]&&j!=pre[i]&&s[i][j]!=INF)
                    MIN = min(MIN,sum-maxx[i][j]+s[i][j]);
            }
        }
        //printf("%d
    ",MIN);
        if(MIN==sum)
            return -1;
        else
            return sum;
    }
    
    int main()
    {
        int t,i,j,a,b,c;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            for(i=1;i<=n;i++)
                for(j=1;j<=n;j++)
                    s[i][j] = i==j?0:INF;
            for(i=0;i<m;i++)
            {
                scanf("%d%d%d",&a,&b,&c);
                if(s[a][b]>c)
                    s[a][b] = s[b][a] = c;
            }
            a = prim();
            if(a==-1)
                printf("Not Unique!
    ");
            else
                printf("%d
    ",a);
        }
        return 0;
    }
    
  • 相关阅读:
    golang常用
    防火墙企业案例2-部署企业及IDC机房上网网关
    PHP实现opentracing jaeger链路追踪
    在CentOS 8中,使用awk+sort+uniq进行Apache访问日志分析
    iptables企业案例
    iptables详解
    Docker的持久化存储和数据共享
    Linux网络tcp连接大量CLOSE_WAIT和TIME_WAIT状态的出现和解决方法
    mobile开发备忘
    jenkins 获取当前上传的分支的commit + url跳转
  • 原文地址:https://www.cnblogs.com/luoxiaoyi/p/9899550.html
Copyright © 2011-2022 走看看