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
  • 相关阅读:
    在java中使用ffmpeg将amr格式的语音转为mp3格式
    keras实现不同形态的模型
    TF版本的Word2Vec和余弦相似度的计算
    TensorFlow中的两种conv2d方法和kernel_initializer
    CNN中的padding
    记一次sqoop同步到mysql
    理解HDFS
    TensorFlow中的优化算法
    使用TensorFlow实现分类
    使用TensorFlow实现回归预测
  • 原文地址:https://www.cnblogs.com/hate13/p/4110303.html
Copyright © 2011-2022 走看看