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行中,会给出修好 ...
随机推荐
- 1-Linux运维人员要求
linux 运维: 1.linux基础操作命令2.linux基础服务搭建3.文本处理命令4.shell脚本编程 python perl php5.数据库 mysql oracle6.lvs集群 热备 ...
- 5 Post实现django表单
本节大纲 1.article-detail 评论页面的准备工作 (1)model层创建评论模型 class Comment(models.Model): """创建评论模 ...
- 2018"百度之星"程序设计大赛 - 资格赛 - 题集
1001 $ 1 \leq m \leq 10 $ 像是状压的复杂度. 于是我们(用二进制)枚举留下的问题集合 然后把这个集合和问卷们的答案集合 $ & $ 一下 就可以只留下被选中的问题的答 ...
- 关于 Google Chrome “Your connection is not private” 问题的处理
今天下午访问google网站的时候,突然不能访问了,提示“Your connection is not private”(你的连接不是私密连接):查看XX-NET的设置,显示“请检查浏览器代理设置”. ...
- 《数据结构》C++代码 栈与队列
线性表中,先进先出的叫队列,先进后出的叫栈.队列常用于BFS,而在函数递归层数过高时,需要手动实现递归过程,这时候便需要写一个“手动栈”. 有时候,我们会有大量数据频繁出入队列,但同时存在其内的元素却 ...
- Python全栈 MySQL 数据库(SQL命令大全、MySQL 、Python调用)
为了梦想与了信仰 开局一张图 主要三个方面: 1.Linux终端命令 2.MySQL语句 3.Python调用 先删库 再跑路..... ...
- 深入MySQL用户自定义变量:使用详解及其使用场景案例
一.前言 在前段工作中,曾几次收到超级话题积分漏记的用户反馈.通过源码的阅读分析后,发现问题出在高并发分布式场景下的计数器上.计数器的值会影响用户当前行为所获得积分的大小.比如,当用户在某超级话题下连 ...
- php数组循环的三种方式
PHP 的遍历数组的三种方式:for循环.foreach循环.while.list().each()组合循环 PHP当中数组分为:索引数组[转换成json是数组]和关联数组[转换成json是对象] f ...
- HDU 4455 Substrings ( DP好题 )
这个……真心看不出来是个DP,我在树状数组的康庄大道上欢快的奔跑了一下午……看了题解才发现错的有多离谱. 参考:http://www.cnblogs.com/kuangbin/archive/2012 ...
- atan与atan2的区别
相比较ATan,ATan2究竟有什么不同?本篇介绍一下ATan2的用法及使用条件. 对于tan(θ) = y / x: θ = ATan(y / x)求出的θ取值范围是[-PI/2, PI/2]. θ ...