zoukankan      html  css  js  c++  java
  • The Unique MST(POJ

    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!

    判断最小生成树的是否唯一的prim算法的变形

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    int a[105][105];
    int vis[105];
    int low[105];
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int ans=0;
            int flag=0;
            memset(a,0x3f,sizeof(a));
            memset(vis,0,sizeof(vis));
            int n,m;
            scanf("%d%d",&n,&m);
            for(int i=1;i<=m;i++)
            {
                int temp1,temp2,temp3;
                scanf("%d%d%d",&temp1,&temp2,&temp3);
                a[temp1][temp2]=temp3;
                a[temp2][temp1]=temp3;
            }
            //prim
            for(int i=1;i<=n;i++)
            {
                low[i]=a[1][i];
            }
            int MIN=1e9;
            int k=0;
            vis[1]=1;
            for(int i=1;i<n;i++)
            {
                k=0;
                MIN=1e9;
                for(int j=1;j<=n;j++)
                {
                    if(low[j]<MIN&&vis[j]==0)
                    {
                        MIN=low[j];
                        k=j;
                    }
                }
                if(k==0) break;
                int cnt=0;
                for(int j=1;j<=n;j++)
                {
                    if(vis[j]&&a[k][j]==MIN)
                    {
                        cnt++;
                        //cout<<"*"<<j<<endl;
                        if(cnt>=2) flag=1;
                    }
                }
                if(flag==1) break;
                vis[k]=1;
                ans+=MIN;
                for(int j=1;j<=n;j++)
                {
                    if(a[k][j]<low[j]&&vis[j]==0)
                    {
                        low[j]=a[k][j];
                    }
                }
            }
            if(flag==0)
            {
                printf("%d
    ",ans);
            }
            else
            {
                printf("Not Unique!
    ");
            }
        }
        return 0;
    }
    
  • 相关阅读:
    SpringMvc---Ant通配符
    mybatis 数据库语句
    shiro 静态页面资源不显示 解决方案
    http错误汇总
    关于代码质量与逻辑
    shiro 过滤属性的意义
    java思维导图
    E
    LCIS HDU
    E
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852297.html
Copyright © 2011-2022 走看看