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<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iomanip>
using namespace std;
#define MAXN 104
#define INF 0x3f3f3f3f
bool been[MAXN];
int n,q,g[MAXN][MAXN],lowcost[MAXN];
/*
最小生成树,如果D(a,b)<=ra+rb,那么g[a][b]=0
否则D(a,b)>ra+rb,g[a][b] = ra+rb-D(a,b)
*/
int Prim(int beg)
{
int ans = ;
memset(been,false,sizeof(been));
for(int i=;i<=n;i++)
{
if(i==beg) lowcost[i] = ;
else lowcost[i] = g[beg][i];
}
been[beg] = true;
for(int j=;j<n;j++)
{
int Minc = INF,k=-;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]<Minc)
{
Minc = lowcost[i];
k = i;
}
}
if(k==-) return -;
been[k] = true;
ans+=Minc;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]>g[k][i])
lowcost[i] = g[k][i];
}
}
return ans;
}
int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>g[i][j];
}
}
cin>>q;
int tx,ty;
for(int i=;i<=q;i++)
{
cin>>tx>>ty;
g[tx][ty] = g[ty][tx] = ;
}
int ans = Prim();
cout<<ans<<endl;
return ;
}

最小生成树 D - Constructing Roads的更多相关文章

  1. (最小生成树)Constructing Roads -- poj -- 2421

    链接: http://poj.org/problem?id=2421 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2113 ...

  2. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

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

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

  4. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

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

  5. hdu Constructing Roads (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...

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

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

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

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

  8. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

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

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

随机推荐

  1. [IOI1998]Polygon

    很早就看到这题了...但因为有个IOI标志,拖到现在才做 由于是以前在书上看到的,就没有想过其他算法,直接区间DP了... 方程式也挺好想的 跟我们平时做数学题求几个数乘积最大差不多 最大的*最大的 ...

  2. python自动化测试学习笔记-6excel操作xlwt、xlrd、xlutils模块

    python中通过xlwt.xlrd和xlutils操作xls xlwt模块用于在内存中生成一个xls/xlsx对象,增加表格数据,并把内存中的xls对象保存为本地磁盘xls文件; xlrd模块用于把 ...

  3. Android内存管理(14)*使用开源库LeakCanary检查内存泄漏

    1.简介 它是一个非常简单好用的内存泄漏检测工具库.可以轻松检测Activity,Fragment的内存泄漏.如果有内存泄漏,它会产生一个通知. 2.资料 官网: https://github.com ...

  4. Python(1)-第一天

    PTVS下载地址:https://pytools.codeplex.com/releases/view/109707 Python下载地址:https://www.python.org/downloa ...

  5. leetcode264 Ugly Number II

    思路: 新生成的数字一定是原来的某个数字乘以2.3或5,为了得到最小的一个,需要用三个指针记录原数字的位置以供比较.为了避免重复,生成新数字以后,原数字对应的指针需要后移一下. 实现: class S ...

  6. vux+vuex+vue+Es6开发微信公众号的坑

    初次开发微信公众号遇到很多问题,可能是基础不怎么牢靠,最近几天一直在看vue的东西,现在就来慢慢介绍vux和vue这个骚东西的用法: 细看文档一步步来, npm install vux --save ...

  7. 02网页<body></body>常用标记及属性

    网页<body></body>常用标记及属性 <body></body>标记表示的是在整个浏览器内容框架中显示的部分. text属性用于控制HTML文档 ...

  8. jquery 实现点评标签 类似淘宝大众点评的 快速准时 货品完好等

    111 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...

  9. Python学习-变量

    什么是变量? 概念:变量就是会变化的量,主要是“变”与“量”二字.变即是“变化”. 特点:与其他编程语言相同,变量是最基本的存储单位,是用来存放数据的容器.可以引用一个具体的数值,进而直接去改变这个引 ...

  10. TestNG套件测试(二)

    在xml中指定要运行的整个包来执行套件测试 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...