Problem Description
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
 

 
Source
kicc
 

Recommend
Eddy

最小生成树的模板题目

下面的代码使用了Prim算法

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<iomanip>
#include<queue>
#define INF 0x7ffffff
#define MAXN 200
using namespace std;
const double eps=1e-;
const double PI=acos(-);
int G[MAXN][MAXN];
int vnew[MAXN];
int lowval[MAXN];
int sum;
int n,q;
void Prim(int start)
{
int j,mi;
for(int i=;i<=n;i++){
if(i!=start){
lowval[i]=G[start][i];
vnew[i]=;
}
}
vnew[start]=;
for(int i=;i<=n-;i++){
j=-;
mi=INF;
for(int i=;i<=n;i++){
if(vnew[i]==&&lowval[i]<mi){
j=i;
mi=lowval[i];
}
}
vnew[j]=;
sum+=lowval[j];
for(int i=;i<=n;i++){
if(vnew[i]==){
lowval[i]=min(lowval[i],G[j][i]);
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie();
//Prim算法
int a,b;
while(cin>>n){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
cin>>G[i][j];
}
}
cin>>q;
for(int i=;i<q;i++){
cin>>a>>b;
G[a][b]=G[b][a]=;
}
sum=;
Prim();
cout<<sum<<endl;
} }

HDU1102--Constructing Roads(最小生成树)的更多相关文章

  1. HDU1102 Constructing Roads —— 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...

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

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

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

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

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

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

  5. hdu1102 Constructing Roads (简单最小生成树Prim算法)

    Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...

  6. POJ2421 & HDU1102 Constructing Roads(最小生成树)

    嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...

  7. hdu1102 Constructing Roads 基础最小生成树

    //克鲁斯卡尔(最小生成树) #include<cstdio> #include<iostream> #include<algorithm> using names ...

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

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

  10. (step6.1.4)hdu 1102(Constructing Roads——最小生成树)

    题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...

随机推荐

  1. LeetCode OJ 75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  2. js 获取页面可视区域宽高

    获取浏览器窗口的可视区域高度和宽度,滚动条高度有需要的朋友可参考一下. 1.IE中,浏览器显示窗口大小只能以下获取: 代码如下复制代码 代码如下 document.body.offsetWidth d ...

  3. lnmp vps服务器删除mysql日志文件三种方法

    我在上一篇文章介绍了著名的LNMP主机一键安装工具,对比了军哥lnmp和AMH主机的差别,由于AMH拥有用户后台界面,易于新手操作,值得推荐. 但是,上周末我网站宕机,收到DNSPOD发来了宕机提醒, ...

  4. $each $position $sort $slice

    $push 向数组中添加元素 $each 循环数据(循环添加数据到数组) $sort 对数组进行排序(1:升序:-1:降序.) $slice 对整个collection表进行数据裁减,用的时候一定要当 ...

  5. python学习入门第一天总结

    虽然之前自己也看过许多关于python的视频,但一直没有动力与勇气,所以未能坚持且也没有学得这么深刻,这次希望通过python自动化培训,能够彻底改变自己,通过第一天的python学习,自己学到了许多 ...

  6. 【Machine Learning in Action --3】决策树ID3算法预测隐形眼睛类型

    本节讲解如何预测患者需要佩戴的隐形眼镜类型. 1.使用决策树预测隐形眼镜类型的一般流程 (1)收集数据:提供的文本文件(数据来源于UCI数据库) (2)准备数据:解析tab键分隔的数据行 (3)分析数 ...

  7. WPFbutton样式

    有四款button不同的风格 <Window x:Class="SjglzxRj.Window3" xmlns="http://schemas.microsoft. ...

  8. 【实验室笔记】C#上位机学习笔记

    用C#编写上位机,基本流程是[1]串口配置,[2]串口发送数据,[3]串口接收数据. [1]串口配置 串口的属性配置包括: No.1串口端口号 No.2串口波特率 No.3串口数据位 No.4串口停止 ...

  9. 对SNS网站现状和未来的一些想法——以我对人人网的体验为例

    现在对人人网越来越没有兴趣了,上面的照片.状态也越来越少了,反而是朋友圈里大家比较活跃. 我觉得在网上发内容的,至少是希望得到大家关注的,可是为什么人人越来越被大家嫌弃了呢? 人人上的消息越来越被淹没 ...

  10. Hive基础学习

    Hive 学习记录Hive介绍:Hive 是起源于Facebook,使得Hadoop进行SQL查询成为可能,进而使得非程序员也可以进进行对其使用:它是一种数据仓库工具,将结构化的数据文件 映射为一张数 ...