(最小生成树)Constructing Roads -- poj -- 2421
链接:
http://poj.org/problem?id=2421
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21130 | Accepted: 8924 |
Description
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
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.
Output
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0xfffffff; int n, J[N][N], dist[N], vis[N]; int Prim()
{
int i, j, ans=;
dist[]=;
memset(vis, , sizeof(vis));
vis[]=; for(i=; i<=n; i++)
dist[i]=J[][i]; for(i=; i<n; i++)
{
int index=, MIN=INF;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]<MIN)
{
index=j; MIN=dist[j];
}
}
vis[index]=;
ans += MIN;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]>J[index][j])
dist[j]=J[index][j];
}
}
return ans;
} int main ()
{
while(scanf("%d", &n)!=EOF)
{
int i, j; memset(J, , sizeof(J)); for(i=; i<=n; i++)
for(j=; j<=n; j++)
scanf("%d", &J[i][j]); int m, a, b;
scanf("%d", &m);
for(i=; i<=m; i++)
{
scanf("%d%d", &a, &b);
J[a][b]=J[b][a]=;
} int ans=Prim(); printf("%d\n", ans);
}
return ;
}
(最小生成树)Constructing Roads -- poj -- 2421的更多相关文章
- Constructing Roads POJ - 2421 (最小生成树)
思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将 所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...
- Constructing Roads POJ - 2421 最小生成树板子题
#include<iostream> #include<cstring> #include<algorithm> using namespace std; ; in ...
- Constructing Roads POJ - 2421
题目链接:https://vjudge.net/problem/POJ-2421 思路:一些村庄,建一些路,使得所有村庄能相连,而且使得所有路长度之和最短. 题目说了,有些村庄之间已经建了路,说明有些 ...
- (最小生成树) Jungle Roads -- POJ -- 1251
链接: http://poj.org/problem?id=1251 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2177 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19884 Accepted: 83 ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
随机推荐
- iKcamp新书上市《Koa与Node.js开发实战》
内容摘要 Node.js 10已经进入LTS时代!其应用场景已经从脚手架.辅助前端开发(如SSR.PWA等)扩展到API中间层.代理层及专业的后端开发.Node.js在企业Web开发领域也日渐成熟,无 ...
- 移去OleContainer的黑边框
//禁止双击打开word编辑 olecontainer1.AutoActivate := aaManual; //禁止右键菜单 olecontainer1.AutoVerbMenu := False; ...
- %s %r 区别 转
也可说是 str() 和 repr() 的区别 转自:http://blog.csdn.net/wusuopubupt/article/details/23678291 %r用rper()方法处理对象 ...
- MIUI添加内存调试工具:查看进程中的Bitmap信息
Android开发中的内存管理一直是令人头痛的事情.其中占用内存最大的一般是Bitmap. 在上周五发布的MIUI开发版中,我添加了查看内存里Bitmap信息的功能.大家开发app的时候可以使用这 ...
- Promise/Deferred
[fydisk] 1.$.get('/api').success(onSuccess).error(onError).comlete(onComplete); 2.对同一事件加入多个Handler. ...
- maven打包部署到私服
转载地址:http://blog.csdn.net/stormragewang/article/details/43407471 心得 apache的开源maven插件对我们使用maven进行打包,发 ...
- php 的 PHPExcel1.8.0 使用教程
PHPExcel是用来操作Office Excel文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言.可以使用它来读取.写入不同格式的电子表格. 一.下载PHPExcel http: ...
- 源码安装php时出现Sorry, I cannot run apxs. Possible reasons follow:
1.可能的原因是你没有安装perl > yum install perl > yum install httpd-devel 2.在你apache安装目录下的bin下找到apxs,并用vi ...
- 什么是maven,jar包的查找过程?
1.什么是maven? Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的项目管理工具软件. 2.什么是项目管理工具? 就是SVN.对于SVN的介绍见我的上一篇博客 ...
- css样式: 宽高按一定比例进行自适应
纯 CSS 实现高度与宽度成比例的效果 最近在做一个产品列表页面,布局如右图所示.页面中有若干个 item,其中每个 item 都向左浮动,并包含在自适应浏览器窗口宽度的父元素中. item 元素的 ...