HDU1102--Constructing Roads(最小生成树)
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 |
|
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 |
|
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(最小生成树)的更多相关文章
- HDU1102 Constructing Roads —— 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- hdu1102 Constructing Roads (简单最小生成树Prim算法)
Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...
- POJ2421 & HDU1102 Constructing Roads(最小生成树)
嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree? orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...
- hdu1102 Constructing Roads 基础最小生成树
//克鲁斯卡尔(最小生成树) #include<cstdio> #include<iostream> #include<algorithm> using names ...
- POJ - 2421 Constructing Roads (最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
- (step6.1.4)hdu 1102(Constructing Roads——最小生成树)
题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...
随机推荐
- erlang lists模块函数使用大全
转自:http://qqdenghaigui.iteye.com/blog/1554541 一,带函数Pred1, all(Pred, List) -> boolean()如果List中的每个元 ...
- WinSnap屏幕截图 V4.5.6 官方最新版
软件名称: WinSnap屏幕截图软件语言: 多国语言授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 2.7MB图片预览: 软件简介:WinS ...
- 【开发笔记】Spring的配置文件
factory-method竟然写成了destroy-method,害得我运行报错,找原因找了几分钟. 原来我按alt+/来提示代码的时候,有许多可选项,可能一时眼花选错了
- Java多态(二)
public class ExtendsTest { public static void main(String[] args) { A a1 = new A(); A a2 = new B(); ...
- zabbix3.2在lamp环境安装
zabbix官网下载zabbix-3.2.1.tar.gz wget http://jaist.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20 ...
- 找回XP中的搜索助理
平台:GHOST版XP 问题:按下F3键后,打开的搜索界面没有搜索助理,比较难看.打开C:\WINDOWS\srchasst目录,发现该目录下空空如也,显然搜索助理文件已经被删除了. 解决: 1.准备 ...
- 用xftp传送避免乱码问题
用xftp传送文件时,需要输入ip地址,可连通的端口号,采用sftp协议 输入数据库传送,属性binary,二进制 上传文件,图片中文名称正常显示等,需要该属性支持UTF-8
- 阅读http://zh.lucida.me/有感
lucida大神本科毕业于大工,研究生毕业于北航,有这样的学历在社会上混就已经绰绰有余了,但是lucida神并不满足,刻苦努力,拼搏进取,最后进入google london工作,曾经一度在micros ...
- js循环POST提交添加辅助单位
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- @Value 注解获取properties值
转自:使用Spring 3的@value简化配置文件的读取 Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码. 1.在application ...