zoukankan      html  css  js  c++  java
  • 次小生成树(poj1679)

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 20737   Accepted: 7281

    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!
    题意:判断最小生成树是否唯一;
    分析:最小生成树的值和次小生成树的值相等则不唯一;
    程序:
    #include"stdio.h"
    #include"string.h"
    #define inf 100000000
    #define M 1111
    int G[M][M],maxd[M][M],use[M],dis[M],pre[M],vis[M][M];
    int max(int a,int b)
    {
        return a>b?a:b;
    }
    int min(int a,int b)
    {
        return a<b?a:b;
    }
    int dij(int u,int n)
    {
        int ans=0,i,j;
        memset(use,0,sizeof(use));
        memset(maxd,0,sizeof(maxd));//记录不在任意两点在在生成树的路径中的最长边
        memset(vis,0,sizeof(vis));//标记边是否在生成树里面
        for(i=1;i<=n;i++)
        {
            dis[i]=G[u][i];
            pre[i]=u;//记录父节点
        }
        dis[u]=0;
        use[u]=1;
        for(i=1;i<n;i++)
        {
            int mini=inf;
            int tep=-1;
            for(j=1;j<=n;j++)
            {
                if(!use[j]&&dis[j]<mini)
                {
                    mini=dis[j];
                    tep=j;
                }
            }
            if(tep==-1)break;
            use[tep]=1;
            vis[tep][pre[tep]]=vis[pre[tep]][tep]=1;
            ans+=mini;
            for(j=1;j<=n;j++)
            {
                if(!use[j]&&dis[j]>G[tep][j])
                {
                    dis[j]=G[tep][j];
                    pre[j]=tep;
                }
                if(j!=tep)
                maxd[tep][j]=maxd[j][tep]=max(mini,maxd[pre[tep]][j]);//更新
            }
        }
        return ans;
    }
    int main()
    {
        int T,m,n,i,j;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            for(i=1;i<=n;i++)
                for(j=1;j<=n;j++)
                G[i][j]=inf;
            while(m--)
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                if(G[a][b]>c)
                    G[a][b]=G[b][a]=c;
            }
            int ans=dij(1,n);
            int cnt=inf;
            for(i=1;i<=n;i++)
            {
                for(j=i+1;j<=n;j++)
                {
                    if(G[i][j]<inf&&vis[i][j]==0)
                    {
                        cnt=min(cnt,ans+G[i][j]-maxd[i][j]);
                    }
                }
            }
            if(ans==cnt)
                printf("Not Unique!
    ");
            else
                printf("%d
    ",ans);
        }
        return 0;
    }
    


  • 相关阅读:
    {转}每次从vss获取文件都是只读
    點擊按鈕后彈出新頁面導致原頁面CSS失效
    Mschat控件示例升级错误处理方法
    一个简单的使用EVP框架的加密过程 aes 128 ecb
    centos7的syslog知识点
    pam模块使用syslog日志调试
    Linux系统上的popen()库函数
    linux C语言 用openssl进行签名验签 --- 亲测2 sha256 sha512
    linux C语言 用openssl进行签名验签 --- 亲测 sha256 sha512
    k8s简介
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348167.html
Copyright © 2011-2022 走看看