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<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 105
int mapn[maxn][maxn],vis[maxn],low[maxn],n;
void prim()
{
int sum = ;
memset(vis,,sizeof(vis));
int pos = ;
for(int i=;i<=n;i++)
low[i] = mapn[][i];
vis[] = ;
for(int i=;i<n;i++)
{
int minn = 1e9;
for(int j=;j<=n;j++)
{
if(!vis[j]&&minn>low[j])//找下一个点到这个集合的最小值
{
minn=low[j];//记下这个最小值
pos=j;//记下这个点
}
}
if(minn == 1e9)
return;
sum += minn;
vis[pos] = ;
for(int j=;j<=n;j++)
{
if(!vis[j]&&low[j]>mapn[pos][j])
low[j] = mapn[pos][j];
}
}
cout << sum << endl;
}
int main()
{
while(cin >> n)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cin >> mapn[i][j];
int m;
cin >> m;
while(m--)
{
int a,b;
cin >> a >> b;
mapn[b][a] = ;
mapn[a][b] = ;
}
prim();
}
return ;
}

Constructing Roads HDU 1102的更多相关文章

  1. Constructing Roads(1102 最小生成树 prim)

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

  3. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  4. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  5. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  6. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  7. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

  8. HDU 1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. HDU 1102 Constructing Roads(kruskal)

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

随机推荐

  1. 如何让传统ASP.NET网站在Docker中运行

    本文主要描述如何让传统ASP.NET网站在Docker中运行,侧重Docker image 搭建. 使用条件: Docker for windows 用户切换到Windows 容器模式 Windows ...

  2. pytest

    pytest可以生成多种样式的结果:1.生成JunitXML格式测试报告:命令: --junitxml=path(相对路径)2.生成result log 格式的测试报告: 命令:--resultlog ...

  3. 页面元素定位-CSS元素基本定位

    基本定位 """属性定位 一 """ # #通过id # driver.find_element_by_css_selector(" ...

  4. SBT安装及命令行打包spark程序

    1.从https://www.scala-sbt.org/download.html官网上寻找所需要的安装包 可以直接本地下载完扔进去也可以wget路径,在这里我用的是sbt1.2.8版本的,下载到/ ...

  5. Winform 自定义文本框

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. Windows 10“数字权利激活”永久性激活!!!

    直接运行软件即可自动激活,等出现"激活成功"即可关闭软件. 注意事项: 激活软件不会帮你打开Windows update服务,如关闭系统自动更细服务的需要先启动服务. 可以在小娜搜 ...

  7. Nacos(五):多环境下如何“读取”Nacos中相应的配置

    前言 前景回顾: Nacos(四):SpringCloud项目中接入Nacos作为配置中心 Nacos(三):Nacos与OpenFeign的对接使用 Nacos(二):SpringCloud项目中接 ...

  8. springmvc集成swaggerui

    这里先写下需要的pom.xml配置(我引用的2.4.0,相对稳定) 在集成springfox-swagger2之前,我也尝试着集成了swagger-springmvc,方式差不多,但是swagger- ...

  9. c#滑窗缓存

    前言 在大数据时代,软件系统需要具备处理海量数据的能力,同时也更加依赖于系统强大的存储能力与数据响应能力.各种大数据的工具如雨后春笋般孕育而生,这对于系统来说是极大的利好.但在后端采用分布式.云存储和 ...

  10. 支持向量机 (一): 线性可分类 svm

    支持向量机(support vector machine, 以下简称 svm)是机器学习里的重要方法,特别适用于中小型样本.非线性.高维的分类和回归问题.本系列力图展现 svm 的核心思想和完整推导过 ...