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什么的. 代码 ...
随机推荐
- 涨知识---IV
1.如何减少换页错误? A.进程倾向于占用CPU. B.访问局部性(locality of reference)满足进程要求. C.进程倾向于占用I/O. D.使用基于最短剩余时间(shortest ...
- 去除IOS苹果手机自带按钮样式的问题~
input[type="button"], input[type="submit"], input[type="reset"] { -web ...
- Android sensor 系统框架 (一)
这几天深入学习了Android sensor框架,以此博客记录和分享分析过程,其中难免会有错误的地方,欢迎指出! 这里主要分析KERNEL->HAL->JNI这3层的流程.主要从以下几方面 ...
- Android基础TOP2:单机按钮改变字体颜色
---恢复内容开始--- Activity: <TextView android:id="@+id/t1" android:textSize="30dp" ...
- codeforces_724C_Ray Tracing
C. Ray Tracing time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- vue02 过滤器、计算和侦听属性、vue对象的生命周期、阻止事件冒泡和刷新页面
3. Vue对象提供的属性功能 3.1 过滤器 过滤器,就是vue允许开发者自定义的文本格式化函数,可以使用在两个地方:输出内容和操作数据中. 定义过滤器的方式有两种. 3.1.1 使用Vue.fil ...
- vue跨域问题解决(生产环境)
vue跨域问题解决(使用webpack打包的) 配置代理:(config下index.js文件) module.exports = { dev: { env: require('./dev.env') ...
- Xcode 插件因为UUID原因不能使用解决办法
Xcode 经常因为一些原因不能使用,需要重新在 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins目录下对每一个插件包下的p ...
- [JS]window.location获取url各项参数详解
window.location方法后还还可以带href,search等参数,下面我们来看看获取url各项参数的办法. URL即:统一资源定位符 (Uniform Resource Locator, U ...
- 从NLP任务中文本向量的降维问题,引出LSH(Locality Sensitive Hash 局部敏感哈希)算法及其思想的讨论
1. 引言 - 近似近邻搜索被提出所在的时代背景和挑战 0x1:从NN(Neighbor Search)说起 ANN的前身技术是NN(Neighbor Search),简单地说,最近邻检索就是根据数据 ...