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. 安卓反汇编工具arm-eabi-objdump

    安卓反汇编工具 在Arm平台系统自带的反编译工具在android/prebuild/linux-/toolchail/arm-abil-/bin目录下的arm_eabi-objdump进行反汇编 ar ...

  2. debain install scim

    1. su input root pwd 2. apt-cache search scim apt-get install scim apt-cache search scim-pinyin apt- ...

  3. Android游戏源代码合集(主要是AndEngine和Libgdx的)

    近期在网络上看到有网友抱怨Android游戏源代码找不到,所以小弟收集了一些AndEngine和Libgdx的游戏源代码,以Eclipseproject的形式配置好环境,再陆续发出(某引擎避嫌,不在此 ...

  4. Xamarin.Android 入门实例(4)之实现对 SQLLite 进行添加/修改/删除/查询操作

    1.Main.axml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...

  5. define a class for a linked list and write a method to delete the nth node.

    1.问题 define a class for a linked list and write a method to delete the nth node. 2.算法 template <t ...

  6. 《数字图像处理原理与实践(MATLAB文本)》书代码Part7

    这篇文章是<数字图像处理原理与实践(MATLAB文本)>一本书的代码系列Part7(由于调整先前宣布订单,请读者注意分页程序,而不仅仅是基于标题数的一系列文章),第一本书特色186经225 ...

  7. Android新建项目后src下没有自动生成文件

    最近开始学Android了,按照教材新建了一个项目,发现src下没有自动生成文件,怎么回事呢? 出现这种可能的原因很可能是ADT与SDK版本不同,造成不兼容. 在ADT(或者eclipse)中的hel ...

  8. 输出A打头的字符串

    题目描述 输出n个字符串,把其中以字母A打头的字符串输出. 输入 第一行 n 第二行到第n+1行,每行一个字符串 输出 A打头的字符串 样例输入 3 Ada Bob Alice 样例输出 Ada Al ...

  9. 【从翻译mos文章】不再用par file如果是,export or import 包含大写和小写表名称表

    不再用par file如果是,export or import 包含大写和小写表名称表 参考原始: How to Export or Import Case Sensitive Tables With ...

  10. CacheManager

    .Net缓存管理框架CacheManager Cache缓存在计算机领域是一个被普遍使用的概念.硬件中CPU有一级缓存,二级缓存, 浏览器中有缓存,软件开发中也有分布式缓存memcache, redi ...