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;
    }
    


  • 相关阅读:
    动态联编学习:一、多态性和动态联编
    客户信用评分参考
    top、postop、scrolltop、scrollHeight、offsetHeight
    XPath 简单语法
    系统表
    彩信MO,MT的业务流程
    Short Message Peer to Peer
    解决IIS中网站按浏览没反应问题
    比较详细的win2003 IIS6.0 301重定向带参数的问题解决方法
    Fleaphp函数用法举例
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348167.html
Copyright © 2011-2022 走看看