zoukankan      html  css  js  c++  java
  • [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
  • 相关阅读:
    Win7。56个进程让我头疼
    bzoj2843极地旅行社
    bzoj2751[HAOI2012]容易题(easy)
    bzoj3442学习小组
    bzoj4423[AMPPZ2013]Bytehattan
    bzoj4591[Shoi2015]超能粒子炮·改
    bzoj2299[HAOI2011]向量
    bzoj3223Tyvj 1729 文艺平衡树
    bzoj2563阿狸和桃子的游戏
    bzoj3673可持久化并查集 by zky&&bzoj3674可持久化并查集加强版
  • 原文地址:https://www.cnblogs.com/hate13/p/4110303.html
Copyright © 2011-2022 走看看