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
 #include <iostream>
#include <algorithm>
using namespace std;
int father[],s;
struct ssss
{
int a,b,x;
}ss[];
int Find(int a)
{
return a==father[a]?a:father[a]=Find(father[a]);
}
void Union(int i,int x,int y)
{
x=Find(x),y=Find(y);
if(x!=y)
{
father[x]=y;
if(i>=)s+=ss[i].x; //用输入进来的i来控制距离同时用来判断是已存在的还是要修的路
}
}
bool cmp(const ssss &a,const ssss &b) //按照距离从小到大排序
{
return a.x<b.x;
}
int main (void)
{
int n,q,i,j,k,l,a[][],x,y;
while(cin>>n)
{
for(i=;i<;i++)father[i]=i; //father数组初始化
for(i=;i<;i++)
for(j=;j<;j++)
a[i][j]=; //因为所有俩村间距离不大于1000,所以初始化大于1000用来通过比较选出输入两村距离的最小值
for(i=;i<n;i++)
for(j=;j<n;j++)
{
cin>>a[i][j];
a[i][j]=a[j][i]=a[i][j]>a[j][i]?a[j][i]:a[i][j]; //把输入的用来比较,因为会输入(1,2)和(2,1),两个值可能不同
}
for(i=l=;i<n;i++)
for(j=i+;j<n;j++)
ss[l].a=i+,ss[l].b=j+,ss[l++].x=a[i][j]; //比较得到俩村距离最小值
sort(ss,ss+l,cmp);
cin>>q;
while(q--&&cin>>x>>y)Union(-,x,y); //把已经存在的路并起来
for(i=s=;i<l;i++)Union(i,ss[i].a,ss[i].b); //把没修的路修好
cout<<s<<endl;
}
return ;
}

AC代码

杭电--1102--Constructing Roads--并查集的更多相关文章

  1. 杭电1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. Rank of Tetris 杭电 拓扑排序加并查集

    自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...

  3. 杭电 4707 pet(并查集求元素大于k的集合)

    Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searche ...

  4. 杭电 5326 Work (并查集求子结点为k的结点数)

    Description It’s an interesting experience to move from ICPC to work, end my college life and start ...

  5. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

  6. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  7. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  8. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  9. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  10. Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads 并查集+优先队列

    D. Fools and Foolproof Roads   You must have heard all about the Foolland on your Geography lessons. ...

随机推荐

  1. CSS学习心得2

    CSS样式 链接 a:link - 普通的.未被访问的链接 a:visited - 用户已访问的链接 a:hover - 鼠标指针位于链接的上方 a:active - 链接被点击的时刻 当为链接的不同 ...

  2. JS对象深刻理解 - 1

    JavaScript创建对象   JavaScript 有Date.Array.String等这样的内置对象,功能强大使用简单,人见人爱,但在处理一些复杂的逻辑的时候,内置对象就很无力了,往往需要开发 ...

  3. centos7 docker zookeeper

    docker run --name=zookeepertmp -i -t centos7/jdk7 /bin/bash cd /home wget http://apache.fayea.com/zo ...

  4. validate插件深入学习-02 常用方法和validate对象的方法

    ①检查表单元素是否有效 valid() 在表单内添加<button id="check">检查</button> $('#check').click(fun ...

  5. 解决iphone5s,iphone6不能使用luajit及luac的问题

    做手游有小段时间了,感觉坤哥给这么多的机会.一直都比较忙项目的事,比较没时间去写点东西做点记录.想想还是写点开发问题记录比较好,可以很简短,也可以很有用. 我们项目采用的cocos2d-x的引擎,之前 ...

  6. 【转】Python3.x和Python2.x的区别

    这个星期开始学习Python了,因为看的书都是基于Python2.x,而且我安装的是Python3.1,所以书上写的地方好多都不适用于Python3.1,特意在Google上search了一下3.x和 ...

  7. 《C++ Primer》学习笔记【第二部分 C++标准库】

    第8章 IO库 IO对象不能复制,即1.IO对象不能存储在vector或其他容器中   2.如果需要传递或返回IO对象,必须传递或返回指向该对象的指针或引用. 一般情况下,如果要传递IO对象以便对它进 ...

  8. winform 记住密码功能

      //登录        private void btn_Login_Click(object sender, EventArgs e)        {            //记住密码    ...

  9. maven引入本地jar

    mvn install:install-file -Dfile=***.jar -DgroupId=**.***.** -DartifactId=* -Dversion=0.8.11 -Dpackag ...

  10. test 2016-12-28

    // dpm(variable_get('node_submitted_page'));// //0// dpm(variable_get('language_count'));// //i3 = i ...