Constructing Roads POJ - 2421
题目链接:https://vjudge.net/problem/POJ-2421
思路:一些村庄,建一些路,使得所有村庄能相连,而且使得所有路长度之和最短。
题目说了,有些村庄之间已经建了路,说明有些路我们不需要建,那么在预处理的时候
把那些已经建过边的两个村庄的距离赋值为0,那么在跑最小生成树板子的时候就完成了
一些路已经建立的情况。
#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std; const int inf = (int)1e9;
const int N = ;
int g[N][N];
int dis[N];
bool vis[N];
int n; struct node{
int loc;
int v; bool friend operator<(const node& a,const node& b){
return a.v > b.v;
}
}; priority_queue<node > que; int prime(){ que.push(node{,});
dis[] = ; while(!que.empty()){
int u = que.top().loc;
que.pop(); vis[u] = true; for(int v = ; v <= n; v++){
if(!vis[v] && dis[v] > g[u][v]){
dis[v] = g[u][v];
que.push(node{v,dis[v]});
}
}
} int ans = ;
for(int i = ; i <= n; i++){ // printf("%d ",dis[i]);
ans += dis[i];
} // printf("\n"); return ans;
} int main(){ scanf("%d",&n); for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
scanf("%d",&g[i][j]); for(int i = ; i <= n; i++)
dis[i] = inf; int m;
scanf("%d",&m); int u,v;
for(int i = ; i <= m; i++){
scanf("%d%d",&u,&v);
g[u][v] = g[v][u] = ;//已经有路的村庄
} printf("%d\n",prime()); return ;
}
Constructing Roads POJ - 2421的更多相关文章
- (最小生成树)Constructing Roads -- poj -- 2421
链接: http://poj.org/problem?id=2421 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2113 ...
- Constructing Roads POJ - 2421 (最小生成树)
思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将 所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...
- Constructing Roads POJ - 2421 最小生成树板子题
#include<iostream> #include<cstring> #include<algorithm> using namespace std; ; in ...
- 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 ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
随机推荐
- Java面试中遇到的坑【篇二面试干货】
俗话说早起的鸟儿有虫吃,现在临年关越来越近,有跳槽的想法的同事也都打算年前做好功课年后入职,所谓年终奖拿了,工作换的也是水到渠成. 说到这里想必有同学要说了,年底了放着年终奖不拿为何要跳槽呢?这个就要 ...
- zz《百度地图商业选址》
作者 | 阚长城 编辑 | 张慧芳 题图 | 站酷海阔 人类几千年的文明催生了城市的发展,计算机与复杂科学带给我们新的资源——大数据.罗马非一日建成,人力和时间成本极大,但试想一下,如果有了大数据,罗 ...
- PointNet++作者的视频讲解文字版
转载请注明本文链接: https://www.cnblogs.com/Libo-Master/p/9759130.html PointNet: Deep Learning on Point Sets ...
- Ultimate Chicken Horse GameProject第三次迭代成果文档
经过三次迭代我们实现了游戏的基本功能 项目文档的github链接:https://github.com/k6tok12355/Ultimate-Chicken-Horse 下面是我们在第一次迭代中设定 ...
- 【day09】PHP
一.函数 1. 作用域(Scope) (1)局部变量:变量在声明的代码段中有效 a.动态变量 b.静态变量:static ,用在函数中,当调用函数后内存不释放,能存储变量的最后的值. (2)全局变量: ...
- 金山云笔试题:AKM函数
1. 题目描述 /** 阿克曼(Ackmann)函数 [题目描述] 阿克曼(Ackmann)函数A(m,n)中,m,n定义域是非负整数(m<=3,n<=10),函数值定义为: akm(m, ...
- PHP自动加载-spl_autoload_register
spl_autoload_register 自动加载spl : Standard PHP library (标准PHP库) 首先来了解 __autoload print.class.php <? ...
- Mysql权限整理及授权命令
1.创建用户sql> use mysql;sql> create user 'Ruthless'@'%' identified by '123456';注意:Ruthless -> ...
- Java生鲜电商平台-源码地址公布与思考和建议
Java生鲜电商平台-源码地址公布与思考和建议 说明:今天是承诺给大家的最后一天,我公布了github地址(QQ群里面有).诚然这个是我的计划中的事情,但是有以下几点思考请大家共勉: 1. 你下了那么 ...
- 超时空英雄传说2复仇魔神完全攻略&秘技
╓─╥───────────────────────────────────────────────────╥─╖ ║ ║ 超 時 空 英 雄 傳 說 2 ║ ║ ║ ║ --復 仇 魔 神-- ║ ...