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
|
Sample Output
|
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行中,会给出修好 ...
随机推荐
- Ajax.BeginForm 异步上传附件 替代方案
一:问题描述 含有文件信息表单内容,想通过异步上传到服务器,但是使用Ajax.BeginForm上传时,后台无法获取文件信息 二:解决方案 通过 $.ajaxFileUpload 可以实现文件及 ...
- 在64位Win7操作系统中安装Microsoft Access Engine的解决方案
在64位Win7操作系统中安装Microsoft Access Engine的解决方案 现在的Win7系统中安装的一般都是32位的Office,因为微软推荐使用32位的Office,兼容性更强,稳定性 ...
- asp 操作 json
<% Dim sc4Json Sub InitScriptControl Set sc4Json =Server.CreateObject("MSScriptControl.Scrip ...
- MediaWiki搭建教程
♦ MediaWiki是什么以及有什么作用,这里我就不再阐述了,网上可以查到很多.这里只是简单记录一下搭建wiki的基本过程,给一些热爱捯饬的小伙伴一些参考. ♦ 其实wiki的搭建本身很简单,最 ...
- MySql - JdbcType - Oracle类型映射
MySql - JdbcType - Oracle类型映射 MySQL数据类型 JDBC TYPE Oracle数据类型 BIGINT BIGINT NUMBER(20) TINYINT TINY ...
- 阅读http://zh.lucida.me/有感
lucida大神本科毕业于大工,研究生毕业于北航,有这样的学历在社会上混就已经绰绰有余了,但是lucida神并不满足,刻苦努力,拼搏进取,最后进入google london工作,曾经一度在micros ...
- ZOJ 1967 POJ 2570 Fiber Network
枚举起点和公司,每次用DFS跑一遍图,预处理出所有的答案.询问的时候很快就能得到答案. #include<cstdio> #include<cmath> #include< ...
- mysql 发生系统错误1067的解决方法
解决方法如下: 1.先删除mysql服务 控制面板->管理工具->服务,先停止mysql服务 开始->运行->输入cmd->sc delete mysql 服务删除 2. ...
- openwrt uci
UCI: Unified Configuration Interface 通用配置接口,主要用于集中控制openwrt的配置文件. 1.uci使用的配置文件一般放置在设备上的/etc/config目录 ...
- Linq第二讲
这一讲,来说说集合.因为linq主要用于对数据源进行查询,集合是最常见的数据源. 集合 形式: 数组,列表List<T> Arraylist等. 特点: 可通过索引或键访问.可进行fore ...