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. 图解说明——究竟什么是Windows句柄

    图解说明——究竟什么是Windows句柄 参考资料:http://blog.csdn.net/newjerryj/article/details/4383701 http://www.cnblogs. ...

  2. 遗传算法在JobShop中的应用研究(part 6:结果显示)

    def FormatSolution(s, C, I): T = [0 for j in xrange(I.n)] S = [[0 for t in I[j]] for j in xrange(I.n ...

  3. 让Xcode 8.x能够调试iOS 7.x真机

    首先需要 Xcode 7.x.可以去 苹果开发者官网 下载. 打开Finder, 进入文件夹: Xcode .x.app/Contents/Developer/Platforms/iPhoneOS.p ...

  4. Codeforces 722D. Generating Sets

    D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. IOS判断设备是否已越狱

    转自:http://www.cnblogs.com/supercheng/archive/2012/12/05/2804166.html - (BOOL)isJailbroken { BOOL jai ...

  6. 分布式消息队列 Kafka

    分布式消息队列 Kafka 2016-02-25 杜亦舒 Kafka是一个高吞吐量的.分布式的消息系统,由Linkedin开发,开发语言为scala具有高吞吐.可扩展.分布式等特点 适用场景 活动数据 ...

  7. 解决在CAPSLOCK开启情况下sendkeys大小写异常的问题

    http://files.cnblogs.com/files/liuzhaoyzz/sendkeys%E5%A4%A7%E5%B0%8F%E5%86%99.rar 首先利用GetKeyState(vb ...

  8. Security » Authorization » 基于资源的授权

    Resource Based Authorization¶ 基于资源的授权 68 of 73 people found this helpful Often authorization depends ...

  9. InventSumDelta表的作用

    https://groups.google.com/forum/#!topic/microsoft.public.axapta.programming/rRfbJo9M0dk The purpose ...

  10. 自己写的一个SqlHelper,感觉使用起来挺方便的

    自己写的一个SqlHelper,感觉使用起来挺方便的 using System; using System.Data; using System.Collections.Generic; using ...