• 杭电--1102--Constructing Roads--并查集


    Constructing Roads

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 10651    Accepted Submission(s): 3967


    Problem Description
    There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

    We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
     


     

    Input
    The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

    Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
     


     

    Output
    You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
     


     

    Sample Input
    3 0 990 692 990 0 179 692 179 0 1 1 2
     


     

    Sample Output
    179
     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 int father[111],s;
     5 struct ssss
     6 {
     7     int a,b,x;
     8 }ss[5000];
     9 int Find(int a)
    10 {
    11     return a==father[a]?a:father[a]=Find(father[a]);
    12 }
    13 void Union(int i,int x,int y)
    14 {
    15     x=Find(x),y=Find(y);
    16     if(x!=y)
    17  {
    18   father[x]=y;
    19   if(i>=0)s+=ss[i].x;  //用输入进来的i来控制距离同时用来判断是已存在的还是要修的路
    20  }
    21 }
    22 bool cmp(const ssss &a,const ssss &b) //按照距离从小到大排序
    23 {
    24     return a.x<b.x;
    25 }
    26 int main (void)
    27 {
    28     int n,q,i,j,k,l,a[111][111],x,y;
    29     while(cin>>n)
    30     {
    31         for(i=0;i<111;i++)father[i]=i; //father数组初始化
    32         for(i=0;i<111;i++)
    33             for(j=0;j<111;j++)
    34                 a[i][j]=1111; //因为所有俩村间距离不大于1000,所以初始化大于1000用来通过比较选出输入两村距离的最小值
    35             for(i=0;i<n;i++)
    36                 for(j=0;j<n;j++)
    37                 {
    38                     cin>>a[i][j];
    39                     a[i][j]=a[j][i]=a[i][j]>a[j][i]?a[j][i]:a[i][j]; //把输入的用来比较,因为会输入(1,2)和(2,1),两个值可能不同
    40                 }
    41                 for(i=l=0;i<n;i++)
    42                     for(j=i+1;j<n;j++)
    43                         ss[l].a=i+1,ss[l].b=j+1,ss[l++].x=a[i][j]; //比较得到俩村距离最小值
    44                     sort(ss,ss+l,cmp);
    45                     cin>>q;
    46                     while(q--&&cin>>x>>y)Union(-1,x,y);  //把已经存在的路并起来
    47      for(i=s=0;i<l;i++)Union(i,ss[i].a,ss[i].b); //把没修的路修好
    48                     cout<<s<<endl;
    49     }
    50     return 0;
    51 }
    AC代码
  • 相关阅读:
    Dll版本管理
    线程池ThreadPool
    关于sitemesh和freemark在struts2中的一些问题总结
    Google 怎么搜索
    android 设计模式
    android webview
    ios 基础数据类型
    android 常用
    android Handler vs Timer
    网站
  • 原文地址:https://www.cnblogs.com/jingdianITnan/p/3228283.html
Copyright © 2020-2023  润新知