poj2421
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 22048 | Accepted: 9389 |
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
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
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
Source
题解:
有n个村庄,编号为1 ,2 ,3 ,,,n 应该建造道路使他们互相可达
对输入数据
3
0 990 692
990 0 179
692 179 0
1
1 2
意思有3个村庄,
0 990 692
990 0 179
692 179 0
意思是1号到1,2,3的距离分别为0 990 692
1
1 2
意思是有一条道路已经接通,就是1号与2号间的道路

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 110
struct node{
int x,y,v;
}e[N*(N+)];
int n,m,k,tot,cnt,fa[N];
bool cmp(const node &a,const node &b){
return a.v<b.v;
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
int x;
scanf("%d",&x);
if(x){
e[++cnt].x=i;e[cnt].y=j;e[cnt].v=x;
}
}
}
scanf("%d",&m);
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
int fx=find(x),fy=find(y);
if(fx!=fy) fa[fy]=fx;
}
sort(e+,e+cnt+,cmp);
for(int i=;i<=cnt;i++){
int fx=find(e[i].x),fy=find(e[i].y);
if(fx!=fy){
fa[fy]=fx;
tot+=e[i].v;
k++;
}
//if(k==n-m-1) break;不能这样写(并查集原理理解错了)
if(k==n-) break;//是mst,这里就不能改
}
printf("%d\n",tot);
return ;
}
poj2421的更多相关文章
- POJ-2421 Constructing Roads---确定部分边的MST
题目链接: https://vjudge.net/problem/POJ-2421 题目大意: 还是给你n个点,然后求最小生成树.特殊之处在于有一些点之间已经连上了边. 思路: 和POJ-1751一样 ...
- Agri Net POJ1258 && Constructing Roads POJ2421
题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include <cstdio> #include <algorithm> #include <cstring ...
- POJ2421 & HDU1102 Constructing Roads(最小生成树)
嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree? orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...
- POJ2421 Constructing Roads【最小生成树】
题意: 有N个点,有些点已经连接了,然后求出所有点的连接的最短路径是多少. 思路: 最小生成树的变形,有的点已经连接了,就直接把他们的权值赋为0,一样的就做最小生成树. 代码: prime: #inc ...
- HDU1102&&POJ2421 Constructing Roads 2017-04-12 19:09 44人阅读 评论(0) 收藏
Constructing Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- 【kruscal】【最小生成树】poj2421 Constructing Roads
SB题,求最小生成树,其中有些边已经给您建好啦. 随意暴力即可. #include<cstdio> #include<algorithm> #include<cstrin ...
- poj2421【MST-prim+Kruskal】
水过~~~~打好基础/~~ ------prim #include <iostream> #include <stdio.h> #include <string.h> ...
- POJ2421 Constructing Roads
Constructing Roads 这道题很水,就是一个裸的最小生成树,最不过把已经连接的节点的值再设为0. 代码: #include<cstdio> #include<cstri ...
- POJ2421 Constructing Roads 最小生成树
修路 时限: 2000MS 内存限制: 65536K 提交总数: 31810 接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...
随机推荐
- fabricjs 自定义类型
https://stackoverflow.com/questions/36660108/how-to-create-custom-fabricjs-object I have to create a ...
- Eclipse web项目导入Intellij 并且部署
一.导入自己的web项目 步骤:File->New->Project from Existing Source... 二.选择项目的所在位置,点击"OK";接着如下图所 ...
- mac os x+paralles使用source insight
将Mac OS X下的目录共享到Paralles后,source insight创建工程.但是当再次打开时却打开失败.提示:there was an error opening project 网上对 ...
- SQL 关键字 'USER' 附近有语法错误怎么办
如下图所示,我想要访问我的Database1.mdf的user这张表,提示如下错误 user在SQL Server中是系统保留字,将user修改为[user]就可以了.但是直接在VS中是无法修改的 ...
- RFS 理解
1.背景 网卡接收一个数据包的情况下,会经过三个阶段: - 网卡产生硬件中断通知CPU有包到达 - 通过软中断处理此数据包 - 在用户态程序处理此数据包 在SMP体系下,这三个阶段有可能在3个 ...
- nginx的buffered to a temporary警告
nginx日志报a client request body is buffered to a temporary file 这个意思是客户全请求的文件超过了nginx的缓存区大小,nginx将内容写入 ...
- 【Excle数据透视表】如何按照地区交替填充背景颜色
现存在如下数据透视表 需要根据地区填充不同的背景颜色 步骤 选定数值区域→开始→条件格式→新建规则,出现如下窗口: 公式:=MOD(COUNT(1/(MATCH($A$4:$A4,$A$4:$A4,) ...
- UIView的endEditing:方法
当视图收到endEditing:消息时,如果视图(或者其下的人和子视图)是当前的第一响应对象,就会取消自己的第一响应对象状态, 而且虚拟键盘也会消失(传入的参数代表是否需要强制取消第一响应对象状态.有 ...
- HTML5中两种方法实现客户端存储数据
HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前,这些都是由 coo ...
- asp.net core mvc视频A:笔记3-6.视图数据共享之session/cache
第一部分:Session讲解 会话级别的,怎么区分呢?以浏览器为单位,比如IE,搜狗等等,都算一个单独的会话 修改3.5项目 控制器代码 前端显示 startup.cs中配置并启用Session 运行 ...