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
#include<iostream>
#include<algorithm>
using namespace std;
int ans=,tot=;int f[],a[][];
const int N = 1e5;
struct ac{
int v,u,w;
}edge[N];
bool cmp(ac a,ac b){
return a.w<b.w;
}
inline int find(int x){
if(x!=f[x])f[x]=find(f[x]);return f[x];
}
inline int join(int x,int y,int w){
int fx=find(x),fy=find(y);
if(fx!=fy){
f[fx]=fy;ans+=w;tot++;
}
}
int main()
{
int n;int m,w,cnt=,u,v;
cin>>n;
for(int i = ;i <= n;++i)f[i]=i;
for(int i = ;i <= n;++i){
for(int j = ;j <= n;++j)
{
cin>>a[i][j];
edge[cnt].u=i,edge[cnt].v=j,edge[cnt].w=a[i][j];
cnt++;
}
}
sort(edge,edge+cnt,cmp);
cin>>m;
while(m--){
cin>>u>>v;
join(u,v,);
}
for(int i = ;i < cnt;++i){
join(edge[i].u,edge[i].v,edge[i].w);
if(tot==n-)break;
}
cout<<ans<<endl;
return ;
}

POJ - 2421 Constructing Roads(最小生成树&并查集的更多相关文章

  1. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  2. POJ - 2421 Constructing Roads (最小生成树)

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  3. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

  4. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

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

  5. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  6. POJ 2421 Constructing Roads(Kruskal算法)

    题意:给出n个村庄之间的距离,再给出已经连通起来了的村庄.求把所有的村庄都连通要修路的长度的最小值. 思路:Kruskal算法 课本代码: //Kruskal算法 #include<iostre ...

  7. POJ 2421 Constructing Roads(最小生成树)

    Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...

  8. [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads

    给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...

  9. Poj 2421 Constructing Roads(Prim 最小生成树)

    题意:有几个村庄,要修最短的路,使得这几个村庄连通.但是现在已经有了几条路,求在已有路径上还要修至少多长的路. 分析:用Prim求最小生成树,将已有路径的长度置为0,由于0是最小的长度,所以一定会被P ...

随机推荐

  1. python 数据库错误处理

    DB API中定义了一些数据库操作的错误及异常,下表列出了这些错误和异常: 异常 描述 Warning 当有严重警告时触发,例如插入数据是被截断等等.必须是 StandardError 的子类. Er ...

  2. #415 Div2 Problem C Do you want a data? (math && 前后缀和 && 快速幂)

    题意: 首先定义集合的F值为  这个集合里面最大值和最小值的差. 现给出一个拥有n个数的集合(没有相同的元素), 要求求出这个集合内所有子集的F的值的和.例如: {4.7}这个集合里面有子集{4}.{ ...

  3. python爬取智联招聘职位信息(单进程)

    我们先通过百度搜索智联招聘,进入智联招聘官网,一看,傻眼了,需要登录才能查看招聘信息 没办法,用账号登录进去,登录后的网页如下: 输入职位名称点击搜索,显示如下网页: 把这个URL:https://s ...

  4. [洛谷P1501] [国家集训队]Tree II(LCT模板)

    传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...

  5. 解决idea无法显示中文候选框问题

    第一:先找到idea安装目录,找到文件jre64,把它命名为“jre642”,也可以随意命名 第二步:找到Javaan安装目录下的jre  ,把它复制到idea安装目录下,命名为“jre64”. 第三 ...

  6. Java中for each与正常for循环效率对比

    循环ArrayList时,普通for循环比foreach循环花费的时间要少一点:循环LinkList时,普通for循环比foreach循环花费的时间要多很多. 当我将循环次数提升到一百万次的时候,循环 ...

  7. C++入门经典-例6.1-一维数组元素

    1:代码如下: // 6.1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...

  8. 【学习】SpringBoot之自定义拦截器

    /** * 自定义拦截器 **/ @Configuration//声明这是一个拦截器 public class MyInterceptor extends WebMvcConfigurerAdapte ...

  9. C# 下载文件的心得

    下载文件最常用的有两种: 第一种:直接使用A标签,对应着文件的地址. 第二种:将文件写成流,然后在回传给客户端. 第一种,使用起来方便,但是有个坏处,如果你的文件是可编辑的文件(比如Excel wor ...

  10. SQL Server AlwaysOn原理简介

    SQL Server2012所支持的AlwaysOn技术集中了故障转移群集.数据库镜像和日志传送三者的优点,但又不相同.故障转移群集的单位是SQL实例,数据库镜像和日志传送的单位是单个用户数据库,而A ...