(最小生成树)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 ...
随机推荐
- Jquery和Ajax
jQuery 是一个 JavaScript 函数库.JavaScript 是 HTML5 以及所有现代浏览器中的默认脚本语言! jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 ...
- Ansiable Manage MySQL global variables
mysql_variables - Manage MySQL global variables New in version 1.3. Synopsis Requirements (on host t ...
- Linux下php5.3.3安装mcrypt扩展
具体操作: 一.下载软件包 1.下载php(版本要与系统安装的一致) http://pan.baidu.com/s/1mifTbfE 2.下载libmcrypt(安装mcrypt需要此软件包) htt ...
- cmd 获取 拖拽文件名
1. @echo off & setlocal enableDelayedExpansion set a= set /p a=Please drag your txt file for spl ...
- 第七章 二叉搜索树(d4)AVL树:(3+4)-重构
- git pull和git fetch命令
git pull和git fetch命令 git pull git pull命令的作用是取回远程主机某个分支的更新,在与本地指定分支合并,格式如下: $ git pull <远程主机名>& ...
- Halcon的C#二次开发及经验分享
本文涉及面较广,因此很难在所有方面都讲解得很详细,故适合具有一定Halcon开发经验的人阅读. 1.Halcon二次开发的两种方式 ① 使用C#的语法方式逐句改写Halcon代码 优点:各种变量的类型 ...
- Python3编程技巧
高效处理数据类型方法: In []: from random import randint In []: data=[randint(-,) )] In []: data Out[]: [-, -, ...
- linux信号处理总结
本文主要讲解常见信号的处理方式. Sighup:终端关闭时,发送给此会话的所有进程组.Setsid成功后不再属于该会话,收不到该消息. Sigterm: kill process_id时产生. Si ...
- laravel中的old()函数
1.控制器 2.模板