hdu 1102(最小生成树)
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19579 Accepted Submission(s): 7474
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.
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.
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.
0 990 692
990 0 179
692 179 0
1
1 2
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; const int N = ;
const int INF = ;
int graph[N][N];
int low[N];
bool vis[N]; int prim(int n,int start){
memset(vis,false,sizeof(vis));
memset(low,false,sizeof(low));
int pos = start,cost=;
vis[pos]=true;
for(int i=;i<=n;i++){
low[i] = graph[pos][i];
}
for(int i=;i<n;i++){
int Min = INF;
for(int j=;j<=n;j++){
if(!vis[j]&&low[j]<Min){
pos = j,Min = low[j];
}
}
vis[pos]=true;
cost+=Min;
for(int j=;j<=n;j++){
if(!vis[j]&&low[j]>graph[pos][j]) low[j] = graph[pos][j];
}
}
return cost;
} int main(){
int n,m;
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&graph[i][j]);
}
}
scanf("%d",&m);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
graph[a][b]=graph[b][a] = ;
}
printf("%d\n",prim(n,));
}
}
hdu 1102(最小生成树)的更多相关文章
- HDU 1102 最小生成树裸题,kruskal,prim
1.HDU 1102 Constructing Roads 最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- 图论问题(2) : hdu 1102
题目转自hdu 1102,题目传送门 题目大意: 输入一个n*n的邻接矩阵,其中i行j列代表从i到j的路径的长度 然后又m条路已经帮你修好了,求最短要修多长的路才能使所有村庄连接 不难看出,这道题就是 ...
- HDU 1233(最小生成树)
HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- 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行中,会给出修好 ...
随机推荐
- poj3348 Cows 凸包 叉积求多边形面积
graham扫描法,参考yyb #include <algorithm> #include <iostream> #include <cstdio> #includ ...
- laravel5.5探究容器的秘密
目录 1. 定义一个契约(接口) 2. 一个实现这个接口的类 3. 创建服务提供者 4. 注册服务提供者 5. 创建facades 6. 再然后需要到配置文件config/app.php中注册门面类别 ...
- 【转】android makefile文件分析
Makefile的规则如下: target ... : prerequisites ... command ... ... target可以是一个目标文件,也可以是Object File(例如hell ...
- 实际遭遇GC回收造成的Web服务器CPU跑高
今天下午有段时间访问园子感觉不如以前那么快的流畅,上Web服务器一看,果然,负载均衡中的1台云服务器CPU跑高. 上图中红色曲线表示的是CPU占用率.正常情况下,CPU占用率一般在40%以下. 这台云 ...
- 新兵易学,老兵易用----C++(C++11的学习整理---如何减少代码量,加强代码的可读性)
1.auto类型推导 auto推导最大的优势就是在拥有初始化表达式的复杂类型变量声明时简化代码. auto第二个优势就是免去了程序员在一些类型声明时的麻烦,或者避免一些在类型声明时的错误. auto第 ...
- SpringMVC 集成 Velocity 模板引擎
本文通过 maven 项目中集成 1.引入 SpringMVC 与 Velocity 需要的依赖 <!-- SpringMVC --> <dependency> <gro ...
- 抓包工具 - Fiddler - (二)
<转载自 miantest> 在上一篇中介绍了Fiddler的基本使用方法.通过上一篇的操作我们可以直接抓取浏览器的数据包.但在APP测试中,我们需要抓取手机APP上的数据包,应该怎么操作 ...
- shell sort 排序大讨论
转自http://roclinux.cn 本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. === [正文开始]有时候学 ...
- [译]如何将docker日志重定向到单个文件里
原文来源: how-to-redirect-docker-logs-to-a-single-file 问题: 我想把某一个docker的log全部导出到一个文件里进行分析,我该怎么做? 其实不用那样操 ...
- docker 生成新的镜像
下载了ubuntu的初始化镜像,但是没有网络安装包,安装了字后,如果生成新的镜像 sudo docker commit -m "add ifconfig/ping package" ...