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. freemaker时间格式转换,精确到毫秒

    在开发中,需要将时间以 2016-09-20 12:00:01:723 的形式里用freemaker展示在页面上,找了好久,终于找到了答案. "createTime":" ...

  2. C#解决一个奇怪的,命名空间“XXX”中不存在类型或命名空间名称“xxx”的问题

    最近做项目时,引用了一个第三方的程序集,代码层面没有任何语法错误,编译提示:命名空间"System.Net"中不存在类型或命名空间名称"FtpClient".是 ...

  3. RSA5、RSA6

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. asp.net天网代码

    自己整理的asp.net中国天气网的城市代码 完整下载 case "北京": Code = "101010100"; break; case "海淀& ...

  5. VirtualBox piix4_smbus Error

    VirtualBox piix4_smbus Error   VirtualBox 3.2.10 gives me the following error message when booting U ...

  6. Java方法trim()小记

    我们一般用trim()方法的主要作用,是为了去除字符串的首尾空格.然而根据我个人的实践经验发现,trim()这个方法只能去除部分的空格或空白符,比如半角空格:对于全角空格的话,用trim()并不能去除 ...

  7. pinpoint 安装部署

    .markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(171, 178 ...

  8. FlexSlider jQuery滑动切换插件 参数

    demo:http://www.sucaihuo.com/jquery/0/6/demo/ FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所 ...

  9. angularjs指令系统系列课程(2):优先级priority,模板template,模板页templateUrl

    今天我们先对 priority,template,templateUrl进行学习 1.priority 可取值:int 作用:优先级 一般priority默认为0,数值越大,优先级越高.当一个dom元 ...

  10. eclipse maven web环境搭建

    选择创建new project 勾选跳过创建类型选择(让eclipse创建标准maven项目) 填写组织ID,唯一ID,注意:如果选择打包类型为war包时,会生成web类型的maven工程 修改JRE ...