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 <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 1000000 int n,ans;
int dis[],vis[],mp[][]; void prim()
{
memset(vis,,sizeof(vis));
memset(dis,INF,sizeof(dis));
dis[]=;ans=;dis[]=INF;
while(true){
int m=;
for(int i=; i<=n; i++){
if(!vis[i] && (dis[i]<dis[m]))
m=i;
}
if(m==)
break;
vis[m]=;
ans+=dis[m];
for(int i=; i<=n; i++)
dis[i]=min(dis[i],mp[m][i]);
}
} int main()
{
int x,a,b;
while(scanf("%d",&n)){
if(n==)
break;
for(int i=; i<=n; i++){
for(int j=; j<=n; j++){
scanf("%d",&mp[i][j]);
}
}
scanf("%d",&x);
while(x--){
scanf("%d%d",&a,&b);
mp[a][b]=mp[b][a]=;
}
prim();
printf("%d\n",ans);
} }

AC代码:

 #include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 1000000 int n,ans;
int dis[],vis[],mp[][]; void prim()
{
memset(vis,,sizeof(vis));
memset(dis,INF,sizeof(dis));
dis[]=;
ans=;
dis[]=INF;
while(true){
int m=;
for(int i=; i<=n; i++){
if(!vis[i] && dis[i]<dis[m])
m=i;
}
if(m==)
break;
vis[m]=;
ans+=dis[m];
for(int i=; i<=n; i++)
dis[i]=min(dis[i],mp[m][i]);
}
} int main()
{
int x,a,b;
while(scanf("%d",&n)==){
for(int i=; i<=n; i++){
for(int j=; j<=n; j++){
scanf("%d",&mp[i][j]);
}
}
scanf("%d",&x);
while(x--){
scanf("%d%d",&a,&b);
mp[a][b]=mp[b][a]=;
}
prim();
printf("%d\n",ans);
} }

NSOJ Constructing Roads(图论)的更多相关文章

  1. Constructing Roads——F

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

  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. 【SICP读书笔记(三)】练习2.18 --- 表序列的reverse方法

    来自练习2.18 请定义出过程reverse,它以一个表为参数,返回的表中所包含的元素与参数表相同,但排列顺序与参数表相反: (reverse (list 1 4 9 16 25)) (25 16 9 ...

  2. 非正确使用浮点数据由项目产生BUG讨论的问题

    乘分配 当小学学会了乘法分配.详细乘法分配:并与多个两个数相乘的,他们能够把这个数字乘以,然后加入.由于一个恒定.乘法分配律也能够使用表达式的定义"(a+b)×c = a×c+b×c&quo ...

  3. JAVA学习JSTL与EL

    一.基础 1.EL(Expression Language):为了使jsp写起来更加简单,提供了在Jsp中简化表达式的方法 2.JSTL:(JSP Standard Tag Library)jstl标 ...

  4. 源代码版本控制工具TortoiseSVN,AnkhSVN最新版本下载地址

    TortoiseSVN http://tortoisesvn.net/downloads.html 页面下部有中文语言补丁 AnkhSVN https://ankhsvn.open.collab.ne ...

  5. 卓尼斯ZT-180点评

    卓尼斯ZT-180点评    ——我们出差,使用10”上网本发布,没有图片.并写冲忙.遗憾的不足之处. 一.购买 1.由于旅游.不想拿那台14"笔记本,台平板电脑.当时,选择的对象有维智A8 ...

  6. RedHat Linux乱码解决方案(转)

    RedHat Linux中出现中文乱码主要是由于没有安装中文字体,因此解决方案主要是安装中文字体,所以 第一步,挂载安装的光盘 在虚拟机的菜单栏里,选择:VM->Settings,点击Setti ...

  7. 旧Mj下拉刷新 An instance 0xca90200 of class UITableView was deallocated while key value observers were s

    An instance 0xca90200 of class UITableView was deallocated while key value observers were still regi ...

  8. JAVA中的super和this关键字的使用

    一 this关键字 this关键字可以出现在构造方法和实例方法中,不能出现在静态方法中,这是因为静态方法可以用类名来调用,这时可能还没有任何对象诞生. this主要有两种用法: 1 用在构造方法中,调 ...

  9. 从session实现机制分析模拟请求验证码的可行性(转)

    悲剧了,发现写完这篇blog没有配上这个格调超高的标题.   1.0问题背景 现在要实现一个带验证码网站的的自动登陆功能.验证码识别过程不再这篇文章的讨论之中.(之后有篇文章我会详细的总结验证码的识别 ...

  10. jsp、Servlet相关知识介绍(转)

    1.servlet生命周期 所谓生命周期,指的是servlet容器如何创建servlet实例.分配其资源.调用其方法.并销毁其实例的整个过程. 阶段一: 实例化(就是创建servlet对象,调用构造器 ...