杭电--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
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.
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.
#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--并查集的更多相关文章
- 杭电1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Rank of Tetris 杭电 拓扑排序加并查集
自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...
- 杭电 4707 pet(并查集求元素大于k的集合)
Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searche ...
- 杭电 5326 Work (并查集求子结点为k的结点数)
Description It’s an interesting experience to move from ICPC to work, end my college life and start ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- 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. ...
随机推荐
- inline-boock的默认间距
1 2 3 4 div{width:900px;} div li{ display:inline-block; width:300px;} <ul> <li></ ...
- 1. ReactNative 基础
/** 1. reactNative 反应式语言 2. 既拥有原生的用户体验,又保留React的开发效率 3. FaceBook研究 H5,Android,iOS 4. BAT的插件化,热修改 2 ...
- 图解MySQL5.5详细安装与配置过程
MySQL是一个开源的关系型数据库管理系统,原由瑞典MySQL AB公司开发,目前属于Oracle公司旗下.MySQL是目前世界上开源数据库中最受欢迎的产品之一,是应用最为广泛的开源数据库.MySQL ...
- 数据库多表连接方式介绍-HASH-JOIN
1.概述 hash join是一种数据库在进行多表连接时的处理算法,对于多表连接还有两种比较常用的方式:sort merge-join 和 nested loop. 为了比较清楚的介绍hash joi ...
- POJ Challenge消失之物
Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. "要使用剩下的 N - 1 物品装满容积为 x ...
- java.nio.file.Path
public interface Path extends Comparable<Path>, Iterable<Path>, Watchable 1. A Path repr ...
- AngularJS select中ngOptions用法详解
AngularJS select中ngOptions用法详解 一.用法 ngOption针对不同类型的数据源有不同的用法,主要体现在数组和对象上. 数组: label for value in a ...
- MySQL wamp密码修改
WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按回车 ...
- IP首部校验和的计算
ip抓包结果:0000: 00 e0 0f 7d 1e ba 00 13 8f 54 3b 70 08 00 45 00 0010: 00 2e be 55 00 00 7a 11 51 ac de ...
- 本地测试Sql
数据库sqlserver2008 编程vs2008,C# 该测试是数据库和程序在一台计算机上,如果不在一台计算机上就不一样了 我的数据库最大链接是127,好像不固定. 一.测试链接对速度的影响 sta ...