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. 插入排序java

    插入排序简述 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据.   第一个元素是有序队列,从第二个元素开始向有序队列中插入,插入完成后将第三个元素向 ...

  2. dm8148 jpeg编解码器测试

    测试过程: 1)于A8将jpeg传送到videoM3解码,然后,videoM3编码.在编译jpeg图像传输到A8,主要是测试jpeg编码的图像需要多少时间: 1000w像素:  编码时间:43ms. ...

  3. Linux Kernel(Android) 加密算法汇总(三)-应用程序调用内核加密算法接口

    于Linux Kernel(Android) 加密算法总结(cipher.compress.digest)文章中.介绍了怎样在内核中增加三种不同类型的内核加密算法, 并给出了在内核模块中怎样调用他们的 ...

  4. [LeetCode53]Maximum Subarray

    问题: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  5. 再谈Hibernate级联删除——JPA下的Hibernate实现一对多级联删除CascadeType.DELETE_ORPHAN

    声明: 1.本文系原创,非抄袭或转载过来的. 2.本文论点都亲手做过实验论证. 3.本文所讲的Hibernate配置都基于注解的方式,hbm语法未提供. 非常多人对持久层概念搞不清JPA.Hibern ...

  6. Linux鸟哥的私房菜(3)— 总体规划和磁盘分区 读书笔记

    1.每个硬件设备Linux中的文件名称 在Linux系统中.每一个设备都被当成一个文件来对待.而且差点儿全部的硬件设备文件都在/dev文件夹下 常见设备与其对于文件名称 2.磁盘连接的方式与设备文件名 ...

  7. js比量undefined种类

    js比量undefined种类 if (reValue== undefined) {     alert("undefined"); } 发现推断不出来.最后查了下资料要用type ...

  8. openSUSE 国内镜像摘要

    1. 北交(BJTU): http://mirror.bjtu.edu.cn/opensuse http://mirror.bjtu.edu.cn/packman/suse 2. 华中科大(HUST) ...

  9. Redis于windows在安装

    下载的windows版本号是redis-2.0.2,解压到D盘下: D:\redis-2.0.2 进到该文件夹下,有下列文件: redis-server.exe:服务程序 redis-check-du ...

  10. LINQ之路(3):LINQ扩展

    本篇文章将从三个方面来进行LINQ扩展的阐述:扩展查询操作符.自定义查询操作符和简单模拟LINQ to SQL. 1.扩展查询操作符 在实际的使用过程中,Enumerable或Queryable中的扩 ...