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 ...
随机推荐
- BT网站-IBMID.COM
最近把網站改版了,主要是更改了搜索引擎. 大家可以訪問 什么是磁力链接(IBMID.COM)(Magnet URI)? 简单的说:类似下面这样以“magnet:?”开头的字符串,就是一条“磁力链接” ...
- 食品生鲜调料代理分销拼团商城微信小程序
食品生鲜调料代理分销拼团商城微信小程序 现在小程序越来越火爆了,一种新的分销拼团模式出现了.一起来分享一下吧 调料商城是一家是专业从事各种调料生产和网上调料商品销售平台,是藤椒油.花椒油.香油.火锅油 ...
- vue-router vuex 用户信息管理
实现原理: 每次进行路由跳转检测全局下用户信息状态是否存在 新建store.js文件 import Vue from 'vue': import Vuex from 'vuex': Vue.use(V ...
- Robot Framework的安装、更新与卸载
Robot Framework的安装.更新与卸载 一,安装RF前的准备 一般就三种执行环境 Python, Jython (JVM) 和 IronPython (.NET): 安装python: #T ...
- Ajax(Asychronous JavaScript and XML)笔记
1 Ajax简介 1 ajax概念 2 什么是同步?什么是异步? 3 ajax原理 2 JavaScript原生的ajax 1 ajax.html代码 <!DOCTYPE html> &l ...
- 【linux】在ubuntu中使用apt-get安装oracle jdk6
在Ubuntu 12.04 LTS上安装JDK6本身并不复杂,只是目前较新版本的Ubuntu已经不支持直接通过apt-get安装了.因此,需要从Oracle官方网站下载安装包进行安装. 从Oracle ...
- spring异常被吞的一种情形
你是否遇到过下面的情况,控制台无限的输出下面的日志: Logging initialized using ‘class org.apache.ibatis.logging.log4j.Log4jImp ...
- 1.6 js基础
必会示例: i的问题 qq头像完整版 this的错误用法 按住鼠标连续加减 封闭空间 甲乙的问题 京东轮播图 苏宁延迟选项卡 无限下拉菜单 淘宝短发送倒计时 1.必须会的 选项卡.按钮 ...
- 修改Windows远程桌面端口
要修改注册表中的两处 PortNumber 1. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdp ...
- 记一次数据、逻辑、视图分离的原生JS项目实践
一切的开始源于这篇文章:一句话理解Vue核心内容. 在文章中,作者给出了这样一个思考: 假设现在有一个这样的需求,有一张图片,在被点击时,可以记录下被点击的次数. 这看起来很简单吧, 按照上面提到到开 ...