zoukankan      html  css  js  c++  java
  • [ An Ac a Day ^_^ ][kuangbin带你飞]专题八 生成树 POJ 1679 The Unique MST

    求最小生成树是否唯一

    求一遍最小生成树再求一遍次小生成树 看看值是否相等就可以

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<string>
     7 #define cl(a,b) memset(a,b,sizeof(a))
     8 #define debug(x) cerr<<#x<<"=="<<(x)<<endl
     9 using namespace std;
    10 typedef long long ll;
    11 
    12 const int maxn=1e2+10;
    13 const int inf=0x3f3f3f3f;
    14 
    15 int n,m;
    16 int cost[maxn][maxn];
    17 int pre[maxn],lowc[maxn];
    18 int Max[maxn][maxn];
    19 bool vis[maxn],used[maxn][maxn];
    20 
    21 int mst()
    22 {
    23     int ans=0;
    24     cl(vis,false),cl(Max,0),cl(used,false);
    25     vis[1]=true,pre[1]=-1,lowc[1]=0;
    26     for(int i=2;i<=n;i++)
    27     {
    28         lowc[i]=cost[1][i];
    29         pre[i]=1;
    30     }
    31     for(int i=2;i<=n;i++)
    32     {
    33         int minc=inf;
    34         int p=-1;
    35         for(int j=1;j<=n;j++)
    36         {
    37             if(!vis[j]&&minc>lowc[j])
    38             {
    39                 minc=lowc[j];
    40                 p=j;
    41             }
    42         }
    43 //        if(minc==inf) return -1;
    44         ans+=minc;
    45         vis[p]=used[p][pre[p]]=used[pre[p]][p]=true;
    46         for(int j=1;j<=n;j++)
    47         {
    48             if(vis[j])
    49             {
    50                 Max[j][p]=max(Max[j][pre[p]],lowc[p]);
    51             }
    52             if(!vis[j]&&lowc[j]>cost[p][j])
    53             {
    54                 lowc[j]=cost[p][j];
    55                 pre[j]=p;
    56             }
    57         }
    58     }
    59     return ans;
    60 }
    61 
    62 void solve()
    63 {
    64     int ans=mst();
    65     int Min=inf;
    66     for(int i=1;i<=n;i++)
    67     {
    68         for(int j=i+1;j<=n;j++)
    69         {
    70             if(cost[i][j]!=inf&&!used[i][j])
    71             {
    72                 Min=min(Min,ans+cost[i][j]-Max[i][j]);
    73             }
    74         }
    75     }
    76     if(ans==Min) printf("Not Unique!
    ");
    77     else printf("%d
    ",ans);
    78 }
    79 
    80 int main()
    81 {
    82     int T;
    83     scanf("%d",&T);
    84     while(T--)
    85     {
    86         cl(cost,inf);
    87         scanf("%d%d",&n,&m);
    88         for(int i=0;i<m;i++)
    89         {
    90             int u,v,w;
    91             scanf("%d%d%d",&u,&v,&w);
    92             cost[u][v]=cost[v][u]=w;
    93         }
    94         solve();
    95     }
    96     return 0;
    97 }
  • 相关阅读:
    re模块详解
    PythonS12-day4学习笔记
    Python-day3作业-haproxy配置文件管理脚本
    Python——Day3知识点——文件操作
    Python-Day3知识点——深浅拷贝、函数基本定义、内置函数
    collections系列
    set集合
    服务器三大体系SMP、NUMA、MPP介绍
    CPU指令集
    PS与TOP详解
  • 原文地址:https://www.cnblogs.com/general10/p/6277136.html
Copyright © 2011-2022 走看看