[wikioi]最优布线问题
http://wikioi.com/problem/1231/
Kruskal+并查集。comp函数里面如果用const引用的话,可以减少copy。并查集find的时候是递归找父亲的根。无他。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <memory.h>
#define MAX(a, b) a>b?a:b
#define LEN 100005
using namespace std; struct Edge {
int a;
int b;
int val;
}; int n, m;
Edge edges[LEN];
int father[LEN]; int find(int x) {
if (father[x] != x)
father[x] = find(father[x]);
return father[x];
} void merge(int x, int y) {
father[find(x)] = find(y);
} void init() {
memset(father, 0, sizeof(father));
memset(edges, 0, sizeof(edges));
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &edges[i].a, &edges[i].b, &edges[i].val);
}
for (int i = 1; i <= n; i++) {
father[i] = i;
}
} bool comp(const Edge &a, const Edge &b) {
return a.val < b.val;
} int main()
{
init();
sort(edges, edges+m, comp);
long long sum = 0;
for (int i = 0; i < m; i++) {
int x = edges[i].a;
int y = edges[i].b;
if (find(x) != find(y)) {
sum += edges[i].val;
merge(x, y);
}
}
printf("%lld\n", sum);
return 0;
}
[wikioi]最优布线问题的更多相关文章
- 最优布线问题(wire.cpp)
最优布线问题(wire.cpp) [问题描述] 学校有n台计算机,为了方便数据传输,现要将它们用数据线连接起来.两台计算机被连接是指它们间有数据线连接.由于计算机所处的位置不同,因此不同的两台计算机的 ...
- 27.prim算法 最优布线问题(wire.cpp)
[例4-10].最优布线问题(wire.cpp) [问题描述] 学校有n台计算机,为了方便数据传输,现要将它们用数据线连接起来.两台计算机被连接是指它们间有数据线连接.由于计算机所处的位置不同,因此不 ...
- codevs1231 最优布线问题
1231 最优布线问题 题目描述 Description 学校需要将n台计算机连接起来,不同的2台计算机之间的连接费用可能是不同的.为了节省费用,我们考虑采用间接数据传输结束,就是一台计算机可以间接地 ...
- [图论]最优布线问题:kruskal
最优布线问题 目录 最优布线问题 Description Input Output Sample Input Sample Output Hint 解析 代码 Description 学校有n台计算机 ...
- [图论]最优布线问题:prim
最优布线问题 目录 最优布线问题 Description Input Output Sample Input Sample Output Hint 解析 代码 Description 学校有n台计算机 ...
- Codevs 1231 最优布线问题
题目描述 Description 学校需要将n台计算机连接起来,不同的2台计算机之间的连接费用可能是不同的.为了节省费用,我们考虑采用间接数据传输结束,就是一台计算机可以间接地通过其他计算机实现和另外 ...
- (最小生成树) codeVs 1231 最优布线问题
题目描述 Description 学校需要将n台计算机连接起来,不同的2台计算机之间的连接费用可能是不同的.为了节省费用,我们考虑采用间接数据传输结束,就是一台计算机可以间接地通过其他计算机实现和另外 ...
- 24.最优布线问题(kruskal算法)
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 学校需要将n台计算机连接起来,不同的2台计算机之间的连接费用 ...
- T1231 最优布线 codevs
http://codevs.cn/problem/1231/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 学校需要将n ...
随机推荐
- Ubuntu server搭建vsftpd小记
Ubuntu server中搭建vsftpd小记 <h1> 在Ubuntu server中安装vsftpd</h1> sudo apt-get install vsftpd & ...
- 【转】Android 应用测试总结
前提所有的功能分支已完成 启动:1. 启动入口:桌面正常启动,最近运行启动,所有程序列表中启动,锁屏快捷启动2. 其他入口:从其他程序开启应用,从外部以文件形式打开应用(如果有)3. 退回:从其他程序 ...
- Javascript,闭包
js变量的调用 一.全局变量函数调用 var n = 99; function f1() { alert(n); } f1();//99 在函数外部定义的变量,再函数中可以被调用,因为变量n,与f1在 ...
- java Spring 在WEB应用中的实例化
.前面讲解的都是通过直接读取配置文件,进行的实例化ApplicationContext AbstractApplicationContext app = new ClassPathXmlApplica ...
- MYSQL的全表扫描,主键索引(聚集索引、第一索引),非主键索引(非聚集索引、第二索引),覆盖索引四种不同查询的分析
文章出处:http://inter12.iteye.com/blog/1430144 MYSQL的全表扫描,主键索引(聚集索引.第一索引),非主键索引(非聚集索引.第二索引),覆盖索引四种不同查询的分 ...
- TMemIniFile 与TIniFile 区别
在uses 申明 Inifiles MyStream:TMemIniFile; MyStream:=TMemIniFile.Create('c:\proxy.ini'); memo1.Text:=My ...
- 什么是NSTimer
本文主要是介绍什么是NSTimer,具体使用请参考上一篇博客. 1.什么是NSTimer? NSTimer就是timer就是一个能在从现在开始的后面的某一个时刻或者周期性的执行我们指定的方法的对象. ...
- Swift 一些环境配置
#if DEBUG 使用 如下图配置即可使用
- 定位相关-CLLocationManager的使用。
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewCont ...
- O-C相关05:方法的封装.
前言:在 OC 中进行封装, 就是实现设置实例变量和获取实例变量数据的方法, 常常称为 setter 方法和 getter 方法. 或称为 get set 读写器. 1,setter 方法 sette ...