Constructing Roads

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23343   Accepted: 10015

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

题目描述:

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

  1. POJ-2421-Constructing Roads(最小生成树 普利姆)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26694   Accepted: 11720 Description The ...

  2. POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K               Description There are N v ...

  3. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  4. Jungle Roads[HDU1301]

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  6. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  7. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  8. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  9. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

随机推荐

  1. Selenium_css 元素定位

    1.通过id定位       # 代表id driver.find_element_by_css_selector("#kw").send_keys("hao" ...

  2. 玩转Spring全家桶笔记 03 Spring的JDBC操作以及SQL批处理的实现

    1 spring-jdbc core JdbcTemplate 等相关核心接口和类(核心) datesource 数据源相关的辅助类(内嵌数据源的初始化) object 将基本的JDBC操作封装成对象 ...

  3. java之struts2之ServletAPI

    在之前的学习中struts2已经可以处理大部分问题了.但是如果要将用户登录数据存入session中,可以有两种方式开存入ServletAPI. 一种解耦合方式,一种耦合方式. 1. 解耦合方式 解耦合 ...

  4. 9.如何让一个div 上下左右居中?【CS

      方法1:[绝对定位50%-本身50%]              position:absolute; left:50%; top:50%;              transform: tra ...

  5. UCOSIII系统内部任务

    1. 空闲任务 空闲任务是UCOSIII创建的第一个任务 空闲任务是UCOSIII必须创建的 空闲任务优先级总是为OS_CFG_PRIO_MAK-1 空闲任务中不能调用任何可使空闲任务进入等待态的函数 ...

  6. js 数组 去重 算法(转载)

    以下内容可能有重复部分,项目有用上,但还没来得急整理和验证. 一:https://www.cnblogs.com/jiayuexuan/p/7527055.html 1.遍历数组法 它是最简单的数组去 ...

  7. Unable to guess the mime type as no guessers are available 2 9

    做了一个上传图片的功能,在本地上传通过,服务器报 bug Unable to guess the mime type as no guessers are available(Did you enab ...

  8. Crypto模块中的签名算法

    因为支付宝当中需要自行实现签名,所以就用到了SHA265和RSA2,将拼接好的信息用私钥进行签名,并进行Base64编码,然后解密就用支付宝回传给用户的公钥解密就ok了,所以我就使用Crypto模块, ...

  9. phpmyadmin教程

    phpmyadmin教程 管理页进入phpmyadmin 打开C:\wamp\apps\phpmyadmin3.5.1下的配置文件:config.inc 修改密码创建与修改数据库.数据表 字段类型 I ...

  10. Linux 绑定 ttyUSBn 串口方法。

    Linux 绑定 ttyUSBn 串口方法. 在linux下, 使用usb转串口, 经常会碰到一个问题: 如果有多个串口, 以不同顺序插入的时候, /dev/ttyUSB0 /dev/ttyUSB1的 ...