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

      

  • 相关阅读:
    c# 设计模式 之:装饰模式
    c# 设计模式 之:抽象工厂
    c# 设计模式 之:简单工厂、工厂方法、抽象工厂之小结、区别
    c# 设计模式 之:工厂模式之---工厂模式
    c# 设计模式 之:工厂模式之---简单工厂
    uml
    ASP.NET应用程序生命周期
    C语言可变参数个数
    软件开发过程中的视角
    UML类图与类的关系详解
  • 原文地址:https://www.cnblogs.com/a972290869/p/4228918.html
Copyright © 2011-2022 走看看