POJ2421Constructing Roads
Constructing Roads
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23343 | Accepted: 10015 |
Description
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
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
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
题目描述:
有N个村庄,从1到N编号,建立一些道路,这样任意两个村庄能连接到对方。我们说两个村庄A和B是连接,当且仅当有A和B之间的道路,或存在C这样的一个村庄之间有一条路A和C,C和B连接。
我们知道,已经有一些道路之间的一些村庄,你的工作是构建道路,所有的村庄都连接和道路建设是最小的长度。
输入描述:
第一行是一个整数N(3 < = N < = 100),这是村庄的数量。然后N行,其中包含N个整数,第i和j N个整数的距离(距离应该是整数在[1000])j村之间我和村庄。
还有一个整数(0 < = Q < = N *(N + 1)/ 2)。然后再问行,每一行包含两个整数a和b(1 < = < b < = N),这意味着路村和村b之间已经建立了。
输出描述:
你应该输出一行包含一个整数,它是所有道路的长度建立连接,这样所有的村庄,这个值是最低。
/*
很简单的最小生成树
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{int x,y,t;}e[];
inline void read(int &x)
{
int f=;x=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
x=x*f;return ;
}
bool cmp(const node &a,const node &b){return a.t<b.t;}
int n,m,x,y,ans=,fa[],sum=;
int find(int x)
{
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void add(int x,int y)
{
int xx=find(x),yy=find(y);
fa[xx]=yy;
}
int main()
{
read(n);
for(int i=;i<=n;i++)fa[i]=i;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
sum++;
read(e[sum].t);
e[sum].x=i;e[sum].y=j;
}
read(m);
for(int i=;i<=m;i++)
{
read(x),read(y);
e[++sum].x=x;
e[sum].y=y;
e[sum].t=;
}
sort(e+,e+sum+,cmp);
m=;
for(int i=;i<=sum;i++)
{
int xx=find(e[i].x),yy=find(e[i].y);
if(xx!=yy)
{
ans+=e[i].t;
add(xx,yy);
m++;
}
if(m==n-)break;
}
printf("%d\n",ans);
return ;
}
POJ2421Constructing Roads的更多相关文章
- POJ-2421-Constructing Roads(最小生成树 普利姆)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26694 Accepted: 11720 Description The ...
- POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Description There are N v ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ1947 Rebuilding Roads[树形背包]
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11495 Accepted: 5276 ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- POJ 1947 Rebuilding Roads
树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...
随机推荐
- go 学习笔记(4) package
package name 尽量与目录名称一样 package name: 表示代码文件所属的包
- Python之TensorFlow的卷积神经网络-5
一.卷积神经网络(Convolutional Neural Networks, CNN)是一类包含卷积计算且具有深度结构的前馈神经网络(Feedforward Neural Networks),是深度 ...
- 并发编程-线程-死锁现象-GIL全局锁-线程池
一堆锁 死锁现象 (重点) 死锁指的是某个资源被占用后,一直得不到释放,导致其他需要这个资源的线程进入阻塞状态. 产生死锁的情况 对同一把互斥锁加了多次 一个共享资源,要访问必须同时具备多把锁,但是这 ...
- VBA 字符串-相关函数(6-12)
Mid()函数 Mid()函数返回给定输入字符串中指定数量的字符. 语法 Mid(String,start[,Length]) 参数 String - 必需的参数.输入从中返回指定数量的字符的字符串. ...
- Keepalived简单理解
Keepalived Keepalived是一个基于VRRP协议来实现的LVS服务高可用方案,可以利用其来避免单点故障.一个LVS服务会有2台服务器运行Keepalived,一台为主服务器(MASTE ...
- js --装饰者模式
定义 装饰者模式能够在补改变对象自身的基础上,在程序运行期间给对象动态的添加职责. 当看到装饰者模式的定义的时候,我们想到的js 的三大特性之一--继承,不也能够实现不改变对象自身的基础上,添加动态的 ...
- TypeScript_泛型
typescript 中很多地方都和 java 和 C# 相似,如果 有 java 和 C# 的同学入手typeScript 会简单很多, 下面这里使用代码来表示和展现泛型的定义和使用 //泛型:在类 ...
- SetCurrentCellAddressCore 函数的可重入调用
绑定数据在线程中 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (Di ...
- MySQL Backup--xtrabackup与Bulk Load for Create Index
场景描述:主从使用MySQL 5.7.19 1.从库上使用xtrabackup进行热备. 2.主库行执行DDL创建索引: ALTER TABLE `tb_xxx` ADD INDEX idx_good ...
- 漫谈五种IO模型(主讲IO多路复用)
首先引用levin的回答让我们理清楚五种IO模型 1.阻塞I/O模型 老李去火车站买票,排队三天买到一张退票. 耗费:在车站吃喝拉撒睡 3天,其他事一件没干. 2.非阻塞I/O模型 老李去火车站买票, ...