zoukankan      html  css  js  c++  java
  • 次小生成树 判断 unique MST

    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<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<iomanip>
    #include<iostream>
    using namespace std;
    #define MAXN 101
    #define INF 0x3f3f3f3f
    /*
    判断最小生成树是否唯一。
    求次小生成树,若两个权值相等说明not unique
    次小生成树算法,在prim()算法求解的时候,求出MST中u到v最大边权值
    ,然后用不在MST中的边依次枚举取最小值
    */
    int g[MAXN][MAXN],Max[MAXN][MAXN],lowcost[MAXN],pre[MAXN],n,m,t;
    bool used[MAXN][MAXN],been[MAXN];
    int Prim()
    {
        int ret = 0;
        memset(been,false,sizeof(been));
        memset(Max,0,sizeof(Max));
        memset(used,false,sizeof(used));
        been[1] = true;
        pre[1] = -1;
        for(int i=2;i<=n;i++)
        {
            pre[i] = 1;
            lowcost[i] = g[1][i];
        }
        lowcost[1] = 0;
        for(int i=1;i<n;i++)
        {
            int minc = INF,k =- 1;
            for(int j=1;j<=n;j++)
            {
                if(!been[j]&&lowcost[j]<minc)
                {
                    minc = lowcost[j];
                    k = j;
                }
            }
            if(k==-1) return -1;
            been[k] = true;
            ret+=minc;
            used[k][pre[k]] = used[pre[k]][k] = true;
            for(int j=1;j<=n;j++)
            {
                if(been[j])
                    Max[j][k] = Max[k][j] = max(Max[j][pre[k]],lowcost[k]);
                if(!been[j]&&lowcost[j]>g[k][j])
                {
                    lowcost[j] = g[k][j];
                    pre[j] = k;
                }
            }
        }
        return ret;
    }
    int cixiao(int ans)
    {
        int tmp = INF;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                if(!used[i][j]&&g[i][j]!=INF)
                    tmp = min(tmp,ans-Max[i][j]+g[i][j]);
            }
        if(tmp==INF)
            return -1;
        return tmp;
    }
    int main()
    {
        cin>>t;
        while(t--)
        {
            cin>>n>>m;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                    g[i][j] = INF;
            }
            int x,y,d;
            for(int t=0;t<m;t++)
            {
                cin>>x>>y>>d;
                g[x][y] = g[y][x] = d;
            }
            int ans = Prim();
            int tmp = cixiao(ans);
            if(tmp==ans||ans==-1)
                cout<<"Not Unique!
    ";
            else
                cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    A
    N
    M
    L
    K
    J
    sass
    通过ps给透明通道的图片添加灰度(适用于需要兼容IE7,效果很好)
    CSS十一问——好奇心+刨根问底=CSSer
    清除浮动的7种方法
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6593470.html
Copyright © 2011-2022 走看看