POJ 2421--Constructing Roads【水题 && 最小生成树 && kruskal】
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20889 | Accepted: 8817 |
Description
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.
Input
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.
Output
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std; int per[110];
int map[110][110];
int N, Q;
struct node{
int u, v, w;
};
node edge[20000]; int cmp(node a, node b){
return a.w < b.w;
} void init(){
for(int i = 1; i <= N; ++i)
per[i] = i;
} int find(int x){
if(x == per[x])
return x;
return per[x] = find(per[x]);
} bool join (int x, int y){
int fx = find(x);
int fy = find(y);
if(fx != fy){
per[fx] = fy;
return true;
}
return false;
} int main (){
while(scanf("%d", &N) != EOF){
int k = 0;
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j)
scanf("%d", &map[i][j]);
scanf("%d", &Q);
while(Q--){
int u, v;
scanf("%d%d", &u, &v);
map[u][v] = 0;
}
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j){
edge[k].u = i;
edge[k].v = j;
edge[k].w = map[i][j];
k++;
}
sort(edge, edge + k, cmp);
int sum = 0;
init();
for(int i = 0; i < k; ++i){
//printf("---%d %d %d\n", edge[i].u, edge[i].v, edge[i].w);
if(join(edge[i].u, edge[i].v))
sum += edge[i].w;
}
printf("%d\n", sum);
}
return 0;
}
POJ 2421--Constructing Roads【水题 && 最小生成树 && kruskal】的更多相关文章
- 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 ...
- poj 2421 Constructing Roads 解题报告
题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...
- POJ 2421 Constructing Roads(最小生成树)
Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...
- POJ - 2421 Constructing Roads (最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- POJ - 2421 Constructing Roads(最小生成树&并查集
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- POJ 2421 Constructing Roads
题意:要在n个城市之间建造公路,使城市之间能互相联通,告诉每个城市之间建公路的费用,和已经建好的公路,求最小费用. 解法:最小生成树.先把已经建好的边加进去再跑kruskal或者prim什么的. 代码 ...
随机推荐
- SublimeText学习(二)-基本操作
1.查看已安装的插件 看到已经安装的插件,看到了在上一篇中安装的Emmet 2.设置主题与字体 方法一: 方法二: 工具栏中 [Preference-浏览程序包]找到[Default文件夹]-用Sub ...
- DML和DQL语句
DML操作: a.插入单行数据: INSERT INTO 表名(列名,列名,...) VALUE(列值,列值,...); 表的字段名是可选的,如果省略,则一次插入所有字段 多个列表和多个值之 ...
- vue杂记
VUE杂记 声明式渲染 <div id="app"> {{ message }} </div> var app = new Vue({ el: '#app' ...
- Python爬取贴吧中的图片
#看到贴吧大佬在发图,准备盗一下 #只是爬取一个帖子中的图片 1.先新建一个scrapy项目 scrapy startproject TuBaEx 2.新建一个爬虫 scrapy genspider ...
- JS——鼠标跟随
注意事项: 1.pageX.pageY的兼容问题 2.使目标移动鼠标中间位置还必须减去盒子宽度的一半 <!DOCTYPE html> <html lang="en" ...
- 排序算法JavaScript版
冒泡排序 function bubbleSort(arr) { var len = arr.length; for (var i = 0; i < len - 1; i++) { for (va ...
- HDU_1158_Employment Planning_dp
Employment Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Please, commit your changes or stash them before you can merge
参照 : https://blog.csdn.net/iefreer/article/details/7679631 用git pull来更新代码的时候,遇到了下面的问题: error: Your l ...
- 一、Scrapy入门教程
本文转载自以下链接:https://scrapy-chs.readthedocs.io/zh_CN/latest/intro/tutorial.html 在本篇教程中,我们假定您已经安装好Scrapy ...
- python爬虫15 | 害羞,用多线程秒爬那些万恶的妹纸们,纸巾呢?
有时候 只是在人群中多看了一眼 就再也没办法忘掉那些容颜 小帅b在普通的一天 上着普通的网 不小心打开了一个不太普通的网站 https://www.mzitu.com/ 从此进入了不普通的一天 看着不 ...