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;
    }
    
  • 相关阅读:
    # 单调栈 ——Acwing 152. 城市游戏
    # 队列 ——Blah数集(OpenJ_Bailian
    # 队列 ——机器 51Nod
    # 栈 # 队列 ——HDU
    openlayers 为元素添加点击和鼠标悬停事件
    wfs请求filter中判断字段为null的写法
    ArcMap去掉Tiff栅格图层外围的黑色边框
    ArcMap给栅格数据配准并设置坐标系
    ArcMap将线按它的每个节点打散和按照线的相交点打断
    使用turf.js和openlayers配合生成等值面
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852297.html
Copyright © 2011-2022 走看看