• [POJ 1679] The Unique MST


    The Unique MST
     

    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!
    

    Source

    POJ Monthly--2004.06.27 srbga@POJ
     
    次小生成树模板、、、
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define N 110
    
    int n,m;
    int closest[N];
    int lowcost[N];
    int mpt[N][N];
    int mxd[N][N];
    bool vis[N];
    bool connect[N][N];
    int ans1,ans2;
    
    void prim(int s)
    {
        int fk,k,i,j,minc;
        for(i=1;i<=n;i++)
        {
            closest[i]=s;
            lowcost[i]=mpt[s][i];
        }
        vis[s]=1;
        for(i=1;i<n;i++)
        {
            minc=INF;
            for(j=1;j<=n;j++)
            {
                if(!vis[j] && lowcost[j]<minc)
                {
                    k=j;
                    minc=lowcost[j];
                }
            }
            if(minc==INF) break;
            vis[k]=1;
            ans1+=minc;
            fk=closest[k];
            connect[fk][k]=1;
            connect[k][fk]=1;
            for(j=1;j<=n;j++)
            {
                if(vis[j] && j!=k) mxd[k][j]=mxd[j][k]=max(mxd[j][fk],lowcost[k]);
                if(!vis[j] && mpt[k][j]<lowcost[j])
                {
                    lowcost[j]=mpt[k][j];
                    closest[j]=k;
                }
            }
        }
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            ans1=0;
            ans2=INF;
            memset(mxd,0,sizeof(mxd));
            memset(mpt,INF,sizeof(mpt));
            memset(vis,0,sizeof(vis));
            memset(connect,0,sizeof(connect));
    
            while(m--)
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                mpt[a][b]=c;
                mpt[b][a]=c;
            }
            prim(1);
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(!connect[i][j])
                    {
                        ans2=min(ans2,ans1-mxd[i][j]+mpt[i][j]);
                    }
                }
            }
            if(ans1!=ans2)
                printf("%d
    ",ans1);
            else
                printf("Not Unique!
    ");
        }
        return 0;
    }
    趁着还有梦想、将AC进行到底~~~by 452181625
  • 相关阅读:
    面向对象的六大原则
    系统整体框架介绍
    键盘控制div上下左右移动 (转)
    逆向wireshark学习SSL协议算法(转)
    在CentOS下安装配置MySQL(转)
    ps 专题
    用Linux/Unix命令把十六进制转换成十进制(转)
    2014由于在myeclipse5.5.1许可证
    美国地名索引(在美国的英文名市、中国)
    Memcache存储大量数据的问题
  • 原文地址:https://www.cnblogs.com/hate13/p/4110303.html
Copyright © 2020-2023  润新知