F. Constructing Roads

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。现需要在这N个村庄之间修路,使得任何两个村庄之间都可以连通。称A、B两个村庄是连通的,
当且仅当A与B有路直接连接,或者存在村庄C,使得A和C两村庄之间有路连接,且C和B之间有路连接。已知某些村庄之间已经有
路直接连接了,试修建一些路使得所有村庄都是连通的、且修路总长度最短。
#include <cstdio>
#include <iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=;
int p[MAXN];
bool sum[MAXN];
int m[MAXN][MAXN];
struct node
{
int x,y,l;
}a[];
bool cmp(node a,node b)
{
return a.l<b.l;
}
int Find(int x)
{
return x==p[x]?x:(p[x]=Find(p[x]));
}
int Union(int R1,int R2)
{ int r1=Find(R1);
int r2=Find(R2);
if(r1!=r2)
{
p[r1]=r2;
return ;
}
else return ;
}
int main()
{
int n;
int cnt=,i,j;
while(~scanf("%d",&n))
{
cnt =;
memset(sum,,sizeof(sum));
for(i=;i<=n;i++)
p[i]=i;
for(i=;i<=n;i++)
for( j=;j<=n;j++)
scanf("%d",&m[i][j]);
int t,c,b;
scanf("%d",&t);
while(t--) //将已经修好的路长度清零
{
scanf("%d%d",&c,&b);
m[c][b]=m[b][c]=;
}
int k=;
for(i=;i<=n;i++)
{
for(j=+i;j<=n;j++)
{
a[k].x=i;
a[k].y=j;
a[k].l=m[i][j];
k++;
}
}
sort(a,a+k,cmp); for(i=;i<k;i++)
{
if(Union(a[i].x,a[i].y)==)
cnt+=a[i].l;
}
printf("%d\n",cnt);
}
return ;
}

Constructing Roads——F的更多相关文章

  1. hdu 1025:Constructing Roads In JGShining's Kingdom(DP + 二分优化)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

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

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

  3. [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  4. HDU 1102 Constructing Roads

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

  5. Constructing Roads (MST)

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

  6. HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  7. hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

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

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

  9. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

随机推荐

  1. 跟随 Web 标准探究DOM -- Node 与 Element 的遍历

    写在前面 这篇没有什么 WebKit 代码的分析,因为……没啥好分析的,在实现里无非就是树的(先序DFS)遍历而已,囧哈哈哈……在WebCore/dom/Node.h , WebCore/dom/Co ...

  2. 关于requirejs

    24718-12042010 00001h6wzKLpfo3gmjJ8xoTPw5mQvY YA8vwka9tH!vibaUKS4FIDIkUfy!!f 3C"rQCIRbShpSlDcFT ...

  3. C和指针 第五章 位数组

    5.4的习题:编写一组函数,实现维数组,函数原型如下: //指定位设置为1void set_bit(char bit_array[], unsigned bit_number); //指定位清零 vo ...

  4. ASP.NET Core--基于授权的资源

    翻译如下: 通常授权取决于正在访问的资源. 例如,文档可以具有作者属性. 将只允许文档作者对其进行更新,因此必须在进行授权评估之前从文档存储库加载资源. 这不能使用Authorize属性来完成,因为属 ...

  5. 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面

    详细错误:HTTP 错误 404.2 - Not Found. 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面. 出现环境:win7 + IIS7.0 解决办法 ...

  6. Mysql数据库的使用总结之Innodb简介(一)

       最近在对开发的软件的服务器部分制作安装包,但服务器部分需要有mysql数据库的支持.因此,采用免安装版的mysql策略:将mysql数据库需要的文件在安装程序中进行设置和打包即可.但也遇到了很多 ...

  7. 我常用的grep命令

    查找包含某个字符的行并保存在文件 grep -rn 'test' ./*.sql >test.sql -r 是递归查找 -n 是显示行号 在当前目录下的.sql结尾的文件中查找包含 test 字 ...

  8. Python: Win7下使用 pip install lxml 无法安装lxml?

    1.在网址 http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 下,搜索lxml,下载Python对应的lxml版本.如下图: 2.打开cmd,进入到lxm ...

  9. [转] CC 命令

    1.最基本的操作cleartool co -nc xxx.cppcleartool ci -nc xxx.cpp 2.查看自己总共co了多少文件cleartool lscheckout -cview ...

  10. MSSQL 查询表空间

    1. exec sp_spaceused '表名'            (SQL统计数据,大量事务操作后可能不准) 2. exec sp_spaceused '表名', true       (更新 ...