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行中,会给出修好 ...
随机推荐
- webapi中常用attribute标签
HTTP Methods Instead of using the naming convention for HTTP methods, you can explicitly specify the ...
- Leetcode 074 Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- Vultr优惠码20美元享受20GB SSD和2T流量
美国vps主机商vultr最新优惠码:20FOR30,只限新注册用户,点击注册链接,进入后台Billing,找到Gift code,输入20FOR30,点击apply,就能免费获得20美元的账户余额. ...
- JS中offsetTop、clientTop、scrollTop、offsetTop各属性介绍(转载)
这里是JavaScript中制作滚动代码的常用属性 页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见 ...
- python paramiko模拟ssh登录,实现sftp上传或者下载文件
Python Paramiko模块的安装与使用详解 paramiko是短链接,不是持续链接,只能执行你设定的shell命令,可以加分号执行两次命令. http://www.111cn.net/phpe ...
- Hibernate查询、连接池、二级缓存
Hibernate第三天: 1. 对象状态 2. session缓存 3. lazy懒加载 4. 映射 一对一对映射 组件/继承映射 目标: 一.hibernate查询 二.hibernate对连接池 ...
- PHP通过url下载远程图片到本地
function GrabImage($url,$filename) { if($url==""):return false;endif; ob_start(); readfile ...
- Quartz任务调度器及与Spring的整合使用
参考 点击打开链接
- android代码格式化方法小结
转载:http://blog.csdn.net/androidzhaoxiaogang/article/details/7692526 Download the android-formatting. ...
- MFC创建动态链接库DLL并调用方法详解
实例一: 1.创建一个动态链接库工程,如login_dll. 2.在原工程头文件或者新建头文件如showdlg.h定义动态链接库的导出函数,代码如下: #include "stdafx.h& ...