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进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
随机推荐
- 201871010111-刘佳华《面向对象程序设计(java)》第6-7周学习总结
201871010111-刘佳华<面向对象程序设计(java)>第6-7周学习总结 实验六 继承定义与使用 实验时间 2019-9-29 第一部分:理论部分. 1.继承:已有类来构建新类的 ...
- HTML与CSS学习笔记(7)
1.响应式布局 利用媒体查询,即media queries,可以针对不同的媒体类型定义不同的样式,从而实现响应式布局. 常见选项: 媒体类型 and.not min-width.max-width: ...
- SpringBoot application.properties配置参数详情
multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...
- C++ class 内的 [] 重载示例。
#include <iostream> // overloading "operator [] " inside class ///////////////////// ...
- CF1225B2 TV Subscriptions (Hard Version)
CF1225B2 TV Subscriptions (Hard Version) 洛谷评测传送门 题目描述 The only difference between easy and hard vers ...
- CF1005F Berland and the Shortest Paths 最短路树计数
问题描述 LG-CF1005F 题解 由题面显然可得,所求即最短路树. 所以跑出最短路树,计数,输出方案即可. \(\mathrm{Code}\) #include<bits/stdc++.h& ...
- 第十 构建Web内容的技术
第十章 构建Web内容的技术 一.HTML HTML(HyperText Markup Language,超文本标记语言)是为了发送Web 上的超文本(Hypertext)而开发的标记语言.超文本是一 ...
- 下载代码的时候 SSH与http的区别
SSH: git@gitlab.alibaba-inc.com:damai-mz/mz-market.git HTTP:http://gitlab.alibaba-inc.com/damai-mz/m ...
- 记录战斗记录你,详解妖尾战斗录像系统[Unity]
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- Start LaTex
目录 Size Color Shape Common Function Type Fill Label Beamer Example Size You can use: ultra thin , ve ...