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

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 21358   Accepted: 7560

    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!
    

    Source

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<string>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    #define INF 1<<30
    int n,m,a[110][110],low[110],vis[110],pre[110];
    int maxx[110][110],connect[110][110],mst;
    int prime()
    {
          int minn,pos,fa,ans=0;
          vis[1]=1,pos=1;
          for(int i=1;i<=n;i++)
                if(i!=pos)
                {
                      low[i]=a[pos][i];
                      pre[i]=pos;
                }
          for(int i=1;i<n;i++)
          {
                minn=INF;
                for(int j=1;j<=n;j++)
                {
                      if(!vis[j]&&minn>low[j])
                      {
                            pos=j;
                            minn=low[j];
                      }
                }
                fa=pre[pos];
                ans+=minn;
                vis[pos]=1;
                connect[fa][pos]=connect[pos][fa]=1;
                maxx[fa][pos]=maxx[pos][fa]=minn;
                for(int j=1;j<=n;j++)
                      maxx[pos][j]=maxx[j][pos]=max(maxx[pos][fa],maxx[fa][j]);
                for(int j=1;j<=n;j++)
                {
                      if(!vis[j]&&low[j]>a[pos][j])
                      {
                            low[j]=a[pos][j];
                            pre[j]=pos;
                      }
                }
          }
          return ans;
    }
    int main()
    {
          int tt;
          scanf("%d",&tt);
          while(tt--)
          {
                bool flag=false;
                memset(vis,0,sizeof(vis));
                memset(connect,0,sizeof(connect));
                memset(maxx,0,sizeof(maxx));
                scanf("%d%d",&n,&m);
                for(int i=1;i<=n;i++)
                      for(int j=1;j<=n;j++)
                            a[i][j]=INF;
                for(int i=1;i<=m;i++)
                {
                      int x,y,z;
                      scanf("%d%d%d",&x,&y,&z);
                      a[x][y]=z,a[y][x]=z;
                }
                mst=prime();
                for(int i=1;i<=n;i++)
                      for(int j=1;j<=n;j++)
                      {
                            if(connect[i][j]||a[i][j]==INF)
                                  continue;
                            if(a[i][j]==maxx[i][j])
                            {
                                  flag=true;
                                  break;
                            }
                      }
                if(flag)
                      printf("Not Unique!
    ");
                else
                      printf("%d
    ",mst);
          }
          return 0;
    }
    

      

  • 相关阅读:
    文科妹子都会用 GitHub,你这个工科生还等什么
    阿里巴巴开发手册强制使用SLF4J作为门面担当的秘密,我搞清楚了
    天啦撸!打印日志竟然只晓得 Log4j?
    老板下了死命令,要把日志系统切换到Logback
    根号x的导数,求导方法
    Java内存模型
    loadrunner截取变量的字符串
    loadrunner11回放日志中文乱码解决办法
    软件性能测试的几个主要术语
    什么是自动化测试框架
  • 原文地址:https://www.cnblogs.com/a972290869/p/4228918.html
Copyright © 2011-2022 走看看