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行中,会给出修好 ...
随机推荐
- 关于mysql中数据类型
bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8 个字节. bigint ...
- jsonp原来是这么回事,豁然开朗
什么是JSONP 先说说JSONP是怎么产生的: 其实网上关于JSONP的讲解有很多,但却千篇一律,而且云里雾里,对于很多刚接触的人来讲理解起来有些困难,小可不才,试着用自己的方式来阐释一下这个问题, ...
- jQuery版本冲突解决办法
<!-- 引入1.6.4版的jq --> <script src="<a href="http://ajax.googleapis.com/ajax/lib ...
- XueTr 0.45 (手工杀毒辅助工具) 绿色版
软件名称: XueTr 0.45 (手工杀毒辅助工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 3.3MB图片预览: ...
- Python 学习笔记11
如何要飞得高,就该把天空忘掉.如果时时想着梦想,那就寸步难行.因为会产生很强的挫败感.倾空自己的杯子,把自己放空,才能放得进去东西. 这两天一直在鼓捣要用python写一个博客出来.先是下载了一个放到 ...
- table可更改th大小的jQuery插件
(function ($) { $.fn.resizetable = function () { var tableObj = $(this); var inResizeRange = false; ...
- Memcached启动脚本
ched: MemCached Daemon # # chkconfig: - # description: MemCached Daemon # # Source function library. ...
- Mongodb 抛出异常:dbexit: really exiting now
删除 数据库文件夹下,的 _tmp 和 mongodb.lock 文件 , 重启即可.我的数据文件在 /data/mongo/data/ 下
- HDU2202--最大三角形(凸包,枚举)
Problem Description 老师在计算几何这门课上给Eddy布置了一道题目,题目是这样的:给定二维的平面上n个不同的点,要求在这些点里寻找三个点,使他们构成的三角形拥有的面积最大.Eddy ...
- 用Replace Pioneer 提取正则内容
推荐用软件Replace Pioneer完成,支持正则表达式和文本替换,提取,很灵活容易. 以下举例说明怎样把<a href 和 </a>之间的内容提取出来,其他的全部删除. 1. ...