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. 【转】段错误调试神器 - Core Dump详解

    from:http://www.embeddedlinux.org.cn/html/jishuzixun/201307/08-2594.html 段错误调试神器 - Core Dump详解 来源:互联 ...

  2. 为什么为 const 变量重新赋值不是个静态错误

    const 和 let 的唯一区别就是用 const 声明的变量不能被重新赋值(只读变量),比如像下面这样就会报错: const foo = 1 foo = 2 // TypeError: Assig ...

  3. tyvj1189 盖房子

    描述 永恒の灵魂最近得到了面积为n*m的一大块土地(高兴ING^_^),他想在这块土地上建造一所房子,这个房子必须是正方形的.但是,这块土地并非十全十美,上面有很多不平坦的地方(也可以叫瑕疵).这些瑕 ...

  4. js中解决函数中使用外部函数局部变量的问题(闭包问题)

    如果要取得外部for循环中i的值则必须使用闭包才能解决 如果不使用闭包,直接使用 变量 i 的值是无效的,因为 i 已经在函数调用之前被回收了,所以你是调用不到它的!

  5. 小知识点总结HTML、CSS、JavaScript(一)

    1.给元素同时设置背景色和背景图的时候,当背景色写在背景图后面,背景色会覆盖背景图未覆盖的位置 如background:url(); background-color:red; 2.当需求一段文字右对 ...

  6. 按Enter键执行表单验证

    document.onkeydown = function(evt){ var evt = window.event?window.event:evt; if (evt.keyCode==13) { ...

  7. 字符串匹配:KMP算法

    一.原理: KMP算法是由Knuth,Morris,Pratt共同提出的模式匹配算法,其对于任何模式和目标序列,都可以在线性时间内完成匹配查找,而不会发生退化,是一个非常优秀的模式匹配算法.朴素算法( ...

  8. jstl core 库 之 out set remove

    jstl 核心库 out标签 out:输出的标签 * value :输出的值 * default :默认值 * escapeXml :是否转移 默认为true(转义) 代码: <!-- 输出常量 ...

  9. Deep Learning入门视频(下)之关于《感受神经网络》两节中的代码解释

    代码1如下: #深度学习入门课程之感受神经网络(上)代码解释: import numpy as np import matplotlib.pyplot as plt #matplotlib是一个库,p ...

  10. 基于STM32和W5500的Modbus TCP通讯

    在最近的一个项目中需要实现Modbus TCP通讯,而选用的硬件平台则是STM32F103和W5500,软件平台则选用IAR EWAR6.4来实现. 1.移植千的准备工作 为了实现Modbus TCP ...