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,表示n个村庄。接着输入n*n的矩阵,然后输入一个q 接下来的q行

每行包含两个数a,b,表示a、b这条边联通,就是已经有公路不用修了,要让所有村庄联通在一起问:修路最小代价?

最小生成树的变形,有的村庄已经连接了,就直接把他们的权值赋为0,一样的做最小生成树,Prim算法。

代码如下:

 #include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std; int N;
int dis[][];
bool vis[][];
struct edge{
int u;
int v;
int w;
}e[];
int pre[];
int find(int x)
{
return (x==pre[x])?x:pre[x] = find(pre[x]);
}
bool cmp(edge a , edge b)
{
return a.w<b.w;
}
int Q;
int main()
{
scanf("%d",&N);
for(int i = ; i <= N;i++)
{
pre[i] = i;
}
for(int i = ; i <= N ;i++)
{
for(int j = ; j <= N ;j++)
{
scanf("%d",&dis[i][j]);
}
} int x , y;
scanf("%d",&Q);
int edge_num = ;
while(Q--)
{
scanf("%d%d",&x,&y);
dis[x][y] = ;
dis[y][x] = ;
}
for(int i = ;i <= N ;i++)
{
for(int j = ; j <= N; j++)
{ e[edge_num].u = i;
e[edge_num].v = j;
e[edge_num].w = dis[i][j];
edge_num++;
}
} sort(e,e+edge_num,cmp);
int u , v , w;
int fx ,fy;
int ans = ;
for(int i = ; i < edge_num ;i++)
{
u = e[i].u;
v = e[i].v;
w = e[i].w; fx = find(u);
fy = find(v); if(fx!=fy)
{
pre[fx] = fy;
ans += w;
}else
{
continue;
}
}
printf("%d\n",ans);
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 (最小生成树)

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

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

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

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

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

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

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

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

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

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

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

  8. POJ - 2421 Constructing Roads(最小生成树&并查集

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

  9. poj 2421 Constructing Roads 解题报告

    题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...

随机推荐

  1. NHibernate-NativeSQL

    一.调用方式 1.创建查询 var sql = session.CreateSQLQuery("SELECT * FROM sns_User WHERE UserName LIKE :use ...

  2. debian下使用dig/nslookup

    debian默认没有安装dig/nslookup命令,使用下面命令安装: apt-get install dnsutils red-hat系列使用: yum install bind-utils ho ...

  3. [转]字符集、字符编码、XML中的中文编码

    字符集.字符编码.XML中的中文编码 作为程序员的你是不是对于ASCII .UNICODE.GB2321.UTF-7.UTF-8等等不时出现在你面前的这些有着奇怪意义的词感到很讨厌呢,是不是总觉得好象 ...

  4. Chrome谷歌浏览器屏蔽百度搜索右侧广告推荐方法

    先上图百度广告,其实屏蔽广告很简单 主要分成以下三步: 下载Adblock Plus插件 安装Adblock Plus插件 开启屏蔽 一.下载Adblock Plus插件(官网离线版) 二.安装Adb ...

  5. Lunix7 开放指定端口

    systemctl unmask firewalld.service 此时可以正常启动Firewall了. 接下来顺便讲述一下Firewall的安装,及一些简单配置.   查看状态,看电脑上是否已经安 ...

  6. Java 依赖注入标准(JSR-330)简介

    作者:88250 ,Vanessa 时间:2009 年 11 月 19 日      Java 依赖注入标准(JSR-330,Dependency Injection for Java)1.0 规范已 ...

  7. 相机IMU融合四部曲(二):误差状态四元数详细解读

    相机IMU融合四部曲(二):误差状态四元数详细解读 极品巧克力 前言 上一篇文章,<D-LG-EKF详细解读>中,讲了理论上的SE3上相机和IMU融合的思想.但是,还没有涉及到实际的操作, ...

  8. RTX Server SDK跨服务器如何调用

    1.   确认安装RTX Server SDK在开发的机器上必须确认已经安装了RTX Server SDK,并且与RTX Server的版本要一致.该计算机后面我们简称SDK计算机. 2.   步骤2 ...

  9. CF 662C Binary Table

    用FWT优化计算. 首先发现行数很小,想到一个暴力的方法,就是以一个二进制位$0$表示这一行不翻转而二进制位$1$表示这一行翻转,然后$2^n$枚举出所有行的翻转情况,再$O(m)$计算所有的结果. ...

  10. 斐波那契数列—java实现

    最近在面试的时候被问到了斐波那契数列,而且有不同的实现方式,就在这里记录一下. 定义 斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...