zoukankan      html  css  js  c++  java
  • POJ 1679 The Unique MST 确定MST是否唯一

                        The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 22817   Accepted: 8114

    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

     
     
     
    题意:给出一个图,若图中的MST图只有一个,输入最小代价,若不唯一,输入Not Unique!
     
     
    O(n^3)
     
    先求出一个MST图,最小代价ans,并且记录MST图的边,然后枚举每一条MST图的边
    若去掉该条边后,该图的最小代价还是ans,说明MST图不止一个,则break
    最后若枚举每一条边后,新的最小代价总是大于ans
    说明该图的MST图只有一个。
     
     
     
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 
      5 using namespace std;
      6 
      7 const int MAXN=103;
      8 const int INF=0x3f3f3f3f;
      9 
     10 int cost[MAXN][MAXN];
     11 int pre[MAXN];
     12 int lowc[MAXN];
     13 bool vis[MAXN];
     14 
     15 void init(int N)
     16 {
     17     for(int i=1;i<=N;i++)
     18     {
     19         for(int j=1;j<=N;j++)
     20         {
     21             if(i==j)
     22                 cost[i][j]=0;
     23             else
     24                 cost[i][j]=INF;
     25         }
     26     }
     27 }
     28 
     29 int prim(int N,int cnt)
     30 {
     31     memset(vis,false,sizeof(vis));
     32     if(cnt)
     33         memset(pre,-1,sizeof(pre));
     34     vis[1]=true;
     35     int ans=0;
     36     for(int i=1;i<=N;i++)
     37     {
     38         lowc[i]=cost[1][i];
     39         if(cnt)
     40             pre[i]=1;
     41     }
     42 
     43     for(int j=1;j<N;j++)
     44     {
     45         int p=-1;
     46         int minc=INF;
     47         for(int i=1;i<=N;i++)
     48         {
     49             if(!vis[i]&&lowc[i]<minc)
     50             {
     51                 minc=lowc[i];
     52                 p=i;
     53             }
     54         }
     55         if(p==-1)
     56             return -1;
     57         ans+=minc;
     58         vis[p]=true;
     59         for(int i=1;i<=N;i++)
     60         {
     61             if(!vis[i]&&cost[p][i]<lowc[i])
     62             {
     63                 lowc[i]=cost[p][i];
     64                 if(cnt)
     65                     pre[i]=p;
     66             }
     67         }
     68     }
     69     return ans;
     70 }
     71 
     72 int main()
     73 {
     74     int test;
     75     scanf("%d",&test);
     76     while(test--)
     77     {
     78         int N,M;
     79         scanf("%d%d",&N,&M);
     80         init(N);
     81 
     82         for(int i=0;i<M;i++)
     83         {
     84             int u,v,w;
     85             scanf("%d%d%d",&u,&v,&w);
     86             cost[u][v]=cost[v][u]=w;
     87         }
     88 
     89         int ans=prim(N,1);
     90 
     91         for(int i=2;i<=N;i++)
     92         {
     93             int v=pre[i];
     94             int tmp=cost[i][v];
     95             cost[i][v]=cost[v][i]=INF;
     96             int sum=prim(N,0);
     97             if(ans==sum)
     98             {
     99                 ans=-1;
    100                 break;
    101             }
    102             cost[i][v]=cost[v][i]=tmp;
    103         }
    104         if(ans==-1)
    105             printf("Not Unique!
    ");
    106         else
    107             printf("%d
    ",ans);
    108     }
    109     return 0;
    110 }
    View Code
     
     
     
     
     
  • 相关阅读:
    U盘文件被隐藏的解决办法
    Regex 符号说明
    HTML特殊字符编码对照表
    Regex 常用
    String decryption with de4dot
    使用Topshelf创建Windows服务[转载]
    de4dot Deobfuscator for .NET
    [原创]extjs htmleditor增加截图快速粘贴功能 插件
    de4dot FAQ
    Cache缓存
  • 原文地址:https://www.cnblogs.com/-maybe/p/4606481.html
Copyright © 2011-2022 走看看