Constructing Roads
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22048   Accepted: 9389

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

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的更多相关文章

  1. POJ-2421 Constructing Roads---确定部分边的MST

    题目链接: https://vjudge.net/problem/POJ-2421 题目大意: 还是给你n个点,然后求最小生成树.特殊之处在于有一些点之间已经连上了边. 思路: 和POJ-1751一样 ...

  2. Agri Net POJ1258 && Constructing Roads POJ2421

    题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include <cstdio> #include <algorithm> #include <cstring ...

  3. POJ2421 & HDU1102 Constructing Roads(最小生成树)

    嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...

  4. POJ2421 Constructing Roads【最小生成树】

    题意: 有N个点,有些点已经连接了,然后求出所有点的连接的最短路径是多少. 思路: 最小生成树的变形,有的点已经连接了,就直接把他们的权值赋为0,一样的就做最小生成树. 代码: prime: #inc ...

  5. 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) ...

  6. 【kruscal】【最小生成树】poj2421 Constructing Roads

    SB题,求最小生成树,其中有些边已经给您建好啦. 随意暴力即可. #include<cstdio> #include<algorithm> #include<cstrin ...

  7. poj2421【MST-prim+Kruskal】

    水过~~~~打好基础/~~ ------prim #include <iostream> #include <stdio.h> #include <string.h> ...

  8. POJ2421 Constructing Roads

    Constructing Roads 这道题很水,就是一个裸的最小生成树,最不过把已经连接的节点的值再设为0. 代码: #include<cstdio> #include<cstri ...

  9. POJ2421 Constructing Roads 最小生成树

    修路 时限: 2000MS   内存限制: 65536K 提交总数: 31810   接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...

随机推荐

  1. python中pickle简介

    2017-04-10 pickle是用来加工数据的,可以用来存取结构化数据. 例如: 一个字典a = {'name':'Timmy','age':26},用pickle.dump存到本地文件,所存数据 ...

  2. 转: Java 应用一般架构

    http://mp.weixin.qq.com/s?__biz=MzAwMzI3Njc1MA==&mid=2650192186&idx=1&sn=bd08fd3a89f9089 ...

  3. EffectiveJava(2)应对多个构造函数应当使用构建器

    ** 应对多个构造函数应当使用构建器 ** 静态工厂和构造器都不能很好的扩展到大量的可选参数,遇到大量参数有大量可选域时,只能重复生成可选参数递增的构造方法,这种构造模式叫做重叠构造器模式 javaB ...

  4. postman里面的mockserver使用方法

    转载:http://blog.csdn.net/Cloud_Huan/article/details/78326159 首先说下mockserver是干啥的,从英文翻译理解就是模拟一个服务器,通俗点说 ...

  5. Solidworks草图或者特征无法删除怎么办

      单击重新建模之后即可删除.

  6. 模式识别:利用MATLAB生成模式类

    近期開始了模式识别的学习,在此之前须要对模式和模式类的概念有一个了解,这里使用MATLAB实现一些模式类的生成.在此之前,引用百科上对于模式识别和模式类的定义.也算加深以下了解: 模式识别(Patte ...

  7. react-native 常见操作 及 git 补充

    一. react-native 常见操作 1.创建项目 react-native init Market(项目名称,首字母大写) 2.安装常用插件 npm install react-native-t ...

  8. view无限旋转

    - (void) showRefreshAnimation { [UIView animateWithDuration: options:UIViewAnimationOptionCurveLinea ...

  9. Docker镜像Export导出和Import导入

    在使用Docker时最头痛的无非无法获取仓库镜像,我们可以通过Export导出镜像备份,通过import导入镜像.导出镜像是通过容器进行导出,下面来看镜像对应的容器: root@default:~# ...

  10. 从两张Excel表所想到的

    从两张Excel表所想到的 前几日,客服妹子发过来几张表,让我给她做下匹配,然后做了,想了,便有了这篇博文,不由感慨,看似简简单单的两张Excel表其实藏着好多东西,记叙如下,与君共勉. 最初的需求: ...