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. BAE3.0搭建wordpress注意

    仅仅是mark一个注意的点,数据库连接时,主机是: /** MySQL主机 */ define('DB_HOST', 'sqld.duapp.com:4050');

  2. Android Java汉字转拼音总结

    转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/23187701 开发过程中有时候会遇到使用拼音模糊搜索等功能(典型的就是Andro ...

  3. JTree demo

    JFrame居中方法一:   setLocationRelativeTo(null); 注意:必须在整个frame初始化完成后再加上此语句,否则将显示在屏幕右下角 方法二: private Dimen ...

  4. 【UVA272】TEX Quotes

    A - TEX Quotes Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu Submitcid=80 ...

  5. 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)

    原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...

  6. JFrame、JDialog close

    package common; import javax.swing.JFrame; import javax.swing.SwingUtilities; /*2015-5-26*/ public c ...

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

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

  8. OpenCV面、人眼检测

    /* 功能:实现对眼睛.脸部的跟踪. 版本号:1.0 时间:2014-4-27 */ #include <opencv2/objdetect/objdetect.hpp> #include ...

  9. linux 下一个 osw先从操作系统和标准脚本主动发起

    linux 下一个 osw与操作系统的引导和启动标准的脚本.osw它指的是--os watcher,这是一个显示器os这些指标shell脚本.osw监测数据一般使用oracle技能评估os资源的使用, ...

  10. nuget 命令详解

    包相关 Install-Package 安装包   -Version 4.3.1 参数指定版本Uninstall-Package 卸载包Update-Package 更新包Get-Package 默认 ...