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. ORACLE常用SQL优化hint语句

    在SQL语句优化过程中,我们经常会用到hint,现总结一下在SQL优化过程中常见Oracle HINT的用法: 1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量, ...

  2. Win7下Doxygen配置与使用

    1.  下载与安装 1.1 下载 Doxygen官方安装程序及其手册下载地址,目前使用版本为1.8.8. 安装程序:http://www.stack.nl/~dimitri/doxygen/downl ...

  3. js 定义方法的集中方式

    1:调用关键字function来构造 如:    function  distance(x1,x2,y1,y2)    {    var  dx = x2 - x1;    var  dy = y2  ...

  4. 给博客添加Flash时钟

    依稀记得在cnblogs很多大牛的博客里见到过大牛的新闻公告栏里那种动感十足的Flash时钟控件,先上图: 作为一名刚的接触博客菜鸟,自然免不了一番好奇.博客设置选项里翻来覆去找(以为是cnblogs ...

  5. Openfire用户密码加密解密

    需求要求审核过程中都用匿名进行用户注册登录,注册用户审核通过后才使用openfire内置表 如何做到用户密码统一 Openfire是通过org.jivesoftware.util.Blowfish.j ...

  6. DOM对象与jquery对象有什么不同

    jQuery对象和DOM对象使用说明,需要的朋友可以参考下.1.jQuery对象和DOM对象第一次学习jQuery,经常分辨不清哪些是jQuery对象,哪些是 DOM对象,因此需要重点了解jQuery ...

  7. 推荐相关学习 & 典型算法、典型特征、典型推荐系统框架

    总的来说,信息爆炸,产生了信息过载.解决的方法主要有两类:检索和推荐.检索是主动的有目的的.意图明确,推荐是非主动的.意图不明确. 推荐方面最经典的,就是协同过滤推荐了.我博客这里有两篇,一篇偏理论, ...

  8. ssh启动报错:org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect

    ssh项目启动报错: org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection t ...

  9. 【High-Speed and Accurate Laser Scan Matching Using Classified Features】

    所谓的"分类特征",就是把特征分成 1. 旋转特征:用直线表示 2. 平移特征,用撕裂点和临界点表示 最大的创新点 应该就是下面的分组吧 匹配的时候,用RANSAC 或者动态规划, ...

  10. django站点管理

    一.启动django站点管理功能 1.关于django.contrib包   包含了django自带的众多附加组件,主要包括:   1)管理工具: django.contrib.admin   2)用 ...