hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21947 Accepted Submission(s):
8448
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.
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.
the length of all the roads to be built such that all the villages are
connected, and this value is minimum.
| 2017-03-02 20:21:34 | Accepted | 15MS | 1456K | 878 B | G++ |
1 #include <stdio.h>
#include <string.h>
#define inf 0x3f3f3f3f int map[][],vis[],dis[];
int n; void prim()
{
int i,j,pos,min,sum = ;
for (i = ; i <= n; i ++)
{
vis[i] = ;
dis[i] = map[][i];
}
vis[] = ; dis[] = ;
for (i = ; i < n; i ++)
{
pos = -; min = inf;
for (j = ; j <= n; j ++)
{
if (!vis[j] && min > dis[j])
{
min = dis[j];
pos = j;
}
}
vis[pos] = ;
sum += min;
for (j = ; j <= n; j ++)
if (!vis[j] && dis[j] > map[pos][j])
dis[j] = map[pos][j];
}
printf("%d\n",sum);
}
int main ()
{
int i,j,m,a,b;
while (~scanf("%d",&n))
{
for (i = ; i <= n; i ++)
for (j = ; j <= n; j ++)
scanf("%d",&map[i][j]);
scanf("%d",&m);
for (i = ; i < m; i ++)
{
scanf("%d%d",&a,&b);
map[a][b] = map[b][a] = ;
}
prim();
}
return ;
}
hdu 1102 Constructing Roads (Prim算法)的更多相关文章
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- 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(kruskal || prim)
求最小生成树.有一点点的变化,就是有的边已经给出来了.所以,最小生成树里面必须有这些边,kruskal和prim算法都能够,prim更简单一些.有一点须要注意,用克鲁斯卡尔算法的时候须要将已经存在的边 ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1102 Constructing Roads(kruskal)
Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
随机推荐
- Pytorch学习笔记(二)——Tensor
一.对Tensor的操作 从接口的角度讲,对Tensor的操作可以分为两类: (1)torch.function (2)tensor.function 比如torch.sum(a, b)实际上和a.s ...
- Shiro源码解析-登录篇
本文以循序渐进的方式解析Shiro整个login过程的处理,读者可以边阅读本文边自己看代码来思考体会,如有问题,欢迎评论区探讨! 笔者shiro的demo源码路径:https://github.com ...
- Java第四次实训作业
1.编写“电费管理类”及其测试类. 第一步 编写“电费管理”类1)私有属性:上月电表读数.本月电表读数2)构造方法:无参.2个参数3)成员方法:getXXX()方法.setXXX()方法4)成员方法 ...
- vue-tree 组织架构图/树形图自动生成(含添加、删除、修改)
项目中用代码生成组织架构图 有新增,编辑,删除的功能 生成树形图的组件git-hub地址: https://github.com/tower1229/Vue-Tree-Char ...
- mysql安装启动 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
首次安装mysql 启动 mysql -uroot 以下错误: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using ...
- 理解restful 架构 && RESTful API设计指南
restful是前端和后端接口中都会使用的设计思想. 网站即软件,我们也常说的webapp,这种互联网软件采用的是“客户端/服务器”模式,建立在分布式体系上. 网站开发,也可以完全采用软件开发的模式, ...
- line-height详解
line-height详解 要说line-height就必须要知道这几个概念了: 顶线.中线.基线.底线. 这也就是在vertical-align中可能用到的top,middle,baseline和b ...
- Mongodb cassandra 和 Mysql对比
MongoDBDB.Cassandra和 Mysql对比 1.为什么是Nosql? 1.1 Nosql在大数据处理相对于关系型数据库具有优势 1.1.1 1. 低延迟 ...
- Netty SSL性能调优
TLS算法组合 在TLS中,5类算法组合在一起,称为一个CipherSuite: 认证算法 加密算法 消息认证码算法 简称MAC 密钥交换算法 密钥衍生算法 比较常见的算法组合是 TLS_ECDHE_ ...
- vue-cli中使用stylus
1.在package.json文件中写入依赖: "stylus-loader": "^2.5.0", "stylus": "0 ...