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. 【OpenGL4.0】GLSL渲染语言入门与VBO、VAO使用:绘制一个三角形 【转】

    http://blog.csdn.net/xiajun07061225/article/details/7628146 以前都是用Cg的,现在改用GLSL,又要重新学,不过两种语言很多都是相通的. 下 ...

  2. SWIG 多语言接口变换 【转】

    一.             SWIG 是Simple Wrapper and Interface Generator的缩写,是一个帮助使用C或者C++编写的软件创建其他编语言的API的工具.例如,我 ...

  3. IDETalk

    IDETalk插件下载地址 IDETalk是由JetBrains的工程师开发的一款代码级的协同工具,主要是为一个团队在进行相关项目开发时提供代码协同.当前IDETalk只能运行在IDEA下,你可以通过 ...

  4. ECSHOP热门搜索关键词随机显示

    实现ECSHOP热门搜索关键词随机显示,需要修改ECSHOP模板和ECShOP程序,按照步骤修改即可. 一.打开 include/lib_main.php 文件,找到下面这段代码,删除之(大概在165 ...

  5. 百度地图 Android SDK - 标注(Marker)的基本使用

    标注(Marker)是开发人员最常使用的地图覆盖物志一.今天就来向大家介绍一些标注(Marker)的最基本用法! 实现目标: 1.构建基础地图页面: 2.在地图的中心点处加入 Marker: 3.实现 ...

  6. 文本文件打印类库(C#)

    我写了一个打印文本文件的类库,功能包含:打印预览.打印.打印时能够选择打印机.能够指定页码范围. 调用方法很easy: TextFilePrinter p = new TextFilePrinter( ...

  7. 原来,多年以来,我一直是个curl/CRUD程序员

    curl,就是create,update,remove,list的首字母简写.说是CRUD似乎更流行些,不过无所谓,知道是一个意思就好. curl程序员,就是增改删查程序员,中文说增删改查更加顺口. ...

  8. 【HTML 元素】嵌入图像

    img元素允许我们在HTML文档里嵌入图像. 要嵌入一张图像需要使用src和alt属性,代码如下: <img src="../img/example/img-map.jpg" ...

  9. Laravel5.1之表单验证

    一.生成一个验证类 1.生成 artisan make:request TestRequest 2.生成的文件在项目Http下的Requests文件夹下 3.默认生成的文件如下 class TestR ...

  10. python——操作符重载(重要)

    类可以重载python的操作符   旧认识:__X__的名字 是系统定义的名字:是python特殊方法专用标识.   操作符重载使我们的对象与内置的一样.__X__的名字的方法是特殊的挂钩(hook) ...