poj 3625 Building Roads
题目连接
http://poj.org/problem?id=3625
Building Roads
Description
Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms.
Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (Xi, Yi) on the plane (0 ≤ Xi ≤ 1,000,000; 0 ≤ Yi ≤ 1,000,000). Given the preexisting M roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Two space-separated integers: Xi and Yi
* Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farm i and farm j.
Output
* Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers.
Sample Input
4 1
1 1
3 1
2 3
4 3
1 4
Sample Output
4.00
最小生成树。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<set>
using std::set;
using std::sort;
using std::pair;
using std::swap;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1010;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
struct Node {
int x, y;
double w;
Node() {}
Node(int i, int j, double k) :x(i), y(j), w(k) {}
inline bool operator<(const Node &t) const {
return w < t.w;
}
}G[(N * N) << 1];
struct P {
double x, y;
inline double calc(const P &t) const {
return sqrt((x - t.x) * (x - t.x) + (y - t.y) * (y - t.y));
}
}A[N];
struct Kruskal {
int E, par[N], rank[N];
inline void init() {
E = 0;
rep(i, N) {
par[i] = i;
rank[i] = 0;
}
}
inline int find(int x) {
while (x != par[x]) {
x = par[x] = par[par[x]];
}
return x;
}
inline bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
if (rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
rank[x] += rank[x] == rank[y];
}
return true;
}
inline void built(int n, int m) {
int u, v;
for(int i = 1; i<= n; i++) scanf("%lf %lf", &A[i].x, &A[i].y);
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
G[E++] = Node(i, j, A[i].calc(A[j]));
}
}
while (m--) {
scanf("%d %d", &u, &v);
G[E++] = Node(u, v, 0.0);
}
}
inline double kruskal(int n) {
int tot = 0;
double ans = 0.0;
sort(G, G + E);
rep(i, E) {
Node &e = G[i];
if (unite(e.x, e.y)) {
ans += e.w;
if (++tot >= n - 1) return ans;
}
}
return -1.0;
}
inline void solve(int n, int m) {
init(), built(n, m);
printf("%.2lf\n", kruskal(n));
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m;
while (~scanf("%d %d", &n, &m)) {
go.solve(n, m);
}
return 0;
}
poj 3625 Building Roads的更多相关文章
- poj 3625 Building Roads(最小生成树,二维坐标,基础)
题目 //最小生成树,只是变成二维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- poj 2749 Building roads (二分+拆点+2-sat)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6229 Accepted: 2093 De ...
- [poj] 2749 building roads
原题 2-SAT+二分答案! 最小的最大值,这肯定是二分答案.而我们要2-SATcheck是否在该情况下有可行解. 对于目前的答案limit,首先把爱和恨连边,然后我们n^2枚举每两个点通过判断距离来 ...
- POJ 2749 Building roads 2-sat+二分答案
把爱恨和最大距离视为限制条件,可以知道,最大距离和限制条件多少具有单调性 所以可以二分最大距离,加边+check #include<cstdio> #include<algorith ...
- Building roads
Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- [POJ2749]Building roads(2-SAT)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8153 Accepted: 2772 De ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- 多次访问节点的DFS POJ 3411 Paid Roads
POJ 3411 Paid Roads Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 24 ...
随机推荐
- Entity FrameWork 与 NHibernate
1 Nhibernate 展示了NHibernate在数据库和用程序之间提供了一个持久层. 应用程序自己提供ADO.NET连接,并且自行管理事务.NHibernate体系结构如图1-51所示.它体 ...
- 洛谷P1530 分数化小数 Fractions to Decimals
P1530 分数化小数 Fractions to Decimals 103通过 348提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目 ...
- 使用Cookie保存商品浏览记录
数据流程:页面上是商品列表,点击<a href="productServlet">商品名</a> ==>跳转到自定义的servlet中进行处理,先得到 ...
- oracle学习系列之四 (视图)
视图视图是数据库中特有的对象.视图用于存储查询,但不会存储数据(物化视图除外).这是视图和数据表的重要区别.可以利用视图进行查询,插入,更新和删除数据.Oracle有如下四种视图(关系视图,内嵌视图, ...
- WebApi简单使用
一.建立一个WebApi项目 WebApi项目的文件和MVC的基本项目内容差不多,都有Models View Controller等,区别在于WebApi的控制器继承的不是Controller类,而是 ...
- js设计模式-建造者模式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- angular.foreach 循环方法使用指南
angular有自己的生命周期.循环给一个 angular监听的变量复值时.最好还是用angular自带的循环方法.“angular.foreach” },{a:}]; angular.forEach ...
- 解决win 7&win xp等系统无法正常用U盘安装或启动
目前,制作启动U盘通常是用ultraiso,但由于各种硬件设备与系统的更新,导致现在装系统会出现各种错误. 在用ultraiso制作的启动U盘,装XP时,可能找不到引导项:装win7时,可能提示”wi ...
- Android IOS WebRTC 音视频开发总结(四一)-- QQ和webrtc打洞能力pk
很多人知道webrtc打洞能力很强,到底有多强但是不知道,比较好的方法就是跟QQ对比,但大多数公司很难模拟各种网络环境进行测试,比如联通,铁通,电信,移动,所以这次请小师妹在实验室下进行了一个比较全面 ...
- 新版本的pdo会有这个问题
新版本的pdo会有这个问题: General error: 2014 Cannot execute queries while other unbuffered queries are active. ...