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. 我不熟悉的string类

    我不常用的string函数 多的不说,直接上: assign函数 string& assign(const char *s); //把字符串s赋给当前的字符串 string& assi ...

  2. Nginx配置记录【例2】

    B服务器,例: [root@localhost conf.d]# egrep -v "^#|^$" /etc/nginx/nginx.conf user nginx; worker ...

  3. linux系统基础优化及高级操作命令

    Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. ifconfig 查询.设置网卡和 ...

  4. LeetCode124----二叉树中最大路径和

    给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不需要经过根节点. 示例 1: 输入: [1,2,3] 1 / \ 2 ...

  5. MySQL中的exist与not exists

    准备数据 我们先介绍下使用的3个数据表: student数据表: course数据表: sc数据表: EXISTS EXISTS代表存在量词∃.带有EXISTS谓词的子查询不返回任何数据,只产生逻辑真 ...

  6. MQTT协议 Websocket JS客户端

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  7. centos6 yum安装mysql 5.6 (完整版)

    使用源代码编译安装mysql还是比较麻烦,一般来说设备安装时请网络同事临时开通linux上网,通过yum网络实现快速安装,或配置yum仓库进行内网统一安装. 通过网络快速安装过程如下 一.检查系统是否 ...

  8. BOSCH汽车工程手册————自适应巡航速度控制ACC

    驾驶员通过自动速度控制器操纵键,将汽车行驶速度控制在预设的期望速度上. ACC系统则在自动速度控制的基础上检测本车到前面行驶汽车的距离以及相对速度,以及其他车道上的信息. 利用这些数据就能控制两车之间 ...

  9. linux(centOS7)的基本操作(七) 其它

    本地与linux服务器之间的文件传输 本地下载的文件,如果想在远端的linux服务器上执行,需要文件传输.如果本地使用windows系统,则借助XFTP软件的图形界面即可.如果本地使用macOS系统, ...

  10. UI自动化关于图片验证码识别的解决方法

    def __save_screenshot(self): self.driver.save_screenshot('full_snap.png') self.page_snap_obj = Image ...