• zoj3204 Connect them 最小生成树


    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367

    题目就是简单的最小生成树的模板的应用,不过最小生成树可能不唯一,答案要求输出字典序最小

    代码:

      1 #include<cstdlib>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<iostream>
      5 #include<algorithm>
      6 using namespace std;
      7 #define maxn 110
      8 int n,tol;
      9 int cnt;
     10 class node
     11 {
     12     public:
     13     int from;
     14     int to;
     15     int w;
     16 };
     17 node edge[maxn*maxn];
     18 node ans[maxn*maxn];
     19 int parent[maxn*maxn];
     20 void addedge(int u,int v,int w)
     21 {
     22     edge[tol].from=u;
     23     edge[tol].to=v;
     24     edge[tol].w=w;
     25     tol++;
     26 }
     27 void UFset()
     28 {
     29     for(int i=0;i<maxn*maxn;i++)
     30        parent[i]=-1;
     31 }
     32 int Find( int x)
     33 {
     34    int s;
     35    for(s=x;parent[s]>=0; s=parent[s]);
     36    while(s!=x)
     37    {
     38       int tmp=parent[x];
     39       parent[x]=s;
     40       x=tmp;
     41    }
     42    return s;
     43 }
     44 void Union(int R1, int R2)
     45 {
     46     int root1=Find(R1);
     47     int root2=Find(R2);
     48 
     49     int tmp=parent[root1]+parent[root2];
     50 
     51     if(parent[root1]> parent[root2])
     52      {
     53          parent[root1]=root2;
     54          parent[root2]=tmp;
     55      }
     56      else
     57      {
     58          parent[root2]=root1;
     59          parent[root1]=tmp;
     60      }
     61 }
     62 bool cmp1( node a, node b)
     63 {
     64     if(a.w!=b.w)return a.w<b.w;
     65        else if(a.from!=b.from)return a.from<b.from;
     66           else return a.to<b.to; 
     67 }
     68 bool cmp2(node a, node b)
     69 {
     70   if(a.from!=b.from)return a.from<b.from;
     71      else return a.to<b.to; 
     72 }
     73 void Kruskal()
     74 {
     75    int num=0;
     76    int u,v;
     77    UFset();
     78    cnt=0;
     79 
     80    for(int i=0;i<tol;i++)
     81     {
     82        u=edge[i].from;
     83        v=edge[i].to;
     84        if(Find(u) != Find(v))
     85        {
     86            ans[cnt++]=edge[i];
     87            Union(u,v);
     88        }
     89        
     90     }
     91 }
     92 int main()
     93 {
     94      int t;
     95      scanf("%d",&t);
     96      while(t--)
     97      {
     98         scanf("%d",&n);
     99         tol=0;
    100         int  weight;
    101         for(int i=1;i<=n;i++)
    102           for(int j=1;j<=n;j++)
    103            {
    104                 scanf("%d",&weight);
    105                 if(j<=i) continue;
    106                 if(weight==0 ) continue;
    107                 addedge(i,j,weight);
    108            }
    109          sort(edge,edge+tol,cmp1);
    110            
    111          Kruskal();
    112 
    113          if(cnt!=n-1)
    114               cout<<"-1"<<endl;
    115            else
    116               {
    117                   sort(ans,ans+cnt,cmp2);
    118                   cout<<ans[0].from<<" "<<ans[0].to;
    119                   for(int i=1;i<cnt;i++)
    120                       cout<<" "<<ans[i].from<<" "<<ans[i].to;
    121                       cout<<endl;
    122               }
    123      }
    124      return 0;
    125 }
  • 相关阅读:
    uboot编译配置过程
    APUE-数据库函数库
    值得推荐的C/C++框架和库 (真的很强大)
    ubuntu12.04图形界面与命令行界面切换
    ubuntu14.04 升级gcc的方法
    4. H5+css3
    3,css 内容
    2. 浏览器兼容性问题
    1,http协议
    页面添加水印
  • 原文地址:https://www.cnblogs.com/xiaozhuyang/p/zoj3204.html
Copyright © 2020-2023  润新知