zoukankan      html  css  js  c++  java
  • POJ 1679 次小生成树

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 30015   Accepted: 10738

    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

    题意:
    n个点,m带权无向条边,问最小生成树是否唯一。
    代码:
    //求次小生成树是否等于最小生成树。
    //次小生成树最多有一条边与最小生成树不同,求出最小生成树后,枚举不在最小生成树中的边u-v,
    //加入这条边会形成环,因此再去掉最小生成树中u-v路径中的权值最大的边。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int inf=0x3f3f3f3f;
    int t,n,m,mp[110][110],vis[110],maxl[110][110],dis[110],pre[110],used[110][110];
    int Prim(){
        memset(maxl,0,sizeof(maxl));
        memset(used,0,sizeof(used));
        for(int i=1;i<=n;i++){
            dis[i]=mp[1][i];
            vis[i]=0;pre[i]=1;
        }
        vis[1]=1;
        int ans=0;
        for(int i=1;i<n;i++){
            int minl=inf,sta=-1;
            for(int j=1;j<=n;j++){
                if(!vis[j]&&dis[j]<minl){
                    minl=dis[j];
                    sta=j;
                }
            }
            if(sta==-1) return -1;
            vis[sta]=1;
            used[sta][pre[sta]]=used[pre[sta]][sta]=1;
            ans+=minl;
            for(int j=1;j<=n;j++){
                if(vis[j]&&j!=sta)
                    maxl[sta][j]=maxl[j][sta]=max(maxl[pre[sta]][j],dis[sta]);
                else if(!vis[j]&&dis[j]>mp[sta][j]){
                    dis[j]=mp[sta][j];
                    pre[j]=sta;
                }
            }
        }
        return ans;
    }
    int Smst(int ans){
        int tmp=inf;
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                if(used[i][j]||mp[i][j]==inf) continue;
                tmp=min(tmp,ans+mp[i][j]-maxl[i][j]);
            }
        }
        if(tmp==inf) return -1;
        return tmp;
    }
    int main()
    {
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&n,&m);
            memset(mp,inf,sizeof(mp));
            for(int i=1;i<=n;i++) mp[i][i]=0;
            int a,b,c;
            for(int i=0;i<m;i++){
                scanf("%d%d%d",&a,&b,&c);
                mp[a][b]=mp[b][a]=min(mp[a][b],c);
            }
            int ans=Prim();
            if(ans==-1){
                printf("Not Unique!
    ");continue;
            }
            int tmp=Smst(ans);
            if(tmp==ans) printf("Not Unique!
    ");
            else printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    linux全方位掌握一个命令--思路比方法更重要
    grep命令详解
    linux中的通配符与正则表达式
    sed命令使用详解归纳
    linux下命令行操作快捷键及技巧
    (原创)发布一个C++版本的ORM库SmartDB(一)
    (原创)C++11改进我们的程序之简化我们的程序(八)
    (原创)C++11改进我们的程序之简化我们的程序(七)
    (原创)C++11改进我们的程序之简化我们的程序(六)
    (原创)C++11改进我们的程序之简化我们的程序(五)
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6752073.html
Copyright © 2011-2022 走看看