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 ...
随机推荐
- Flex Alert的匿名回调函数如何得到正确的this
Flex中经常使用Alert来弹出提示或确认窗口,为了方便省事,会直接用匿名函数作为回调,但有时如果要调用外部的this,你会发现匿名函数中的this无法指向外部父类,可以使用e.target获取pa ...
- javaScript 封装
在基于web 的b/s 架构的项目中, 丰富的界面都离不开 javascript, javascript 在 html 中变得越来越强大,但是我们在写 javascript 的时候都比较随意,随着页面 ...
- No.002 Add Two Numbers
Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...
- com组件 智能指针崩溃问题崩溃问题
int main(){ CoInitialize(NULL); HRESULT hr; IWinHttpRequestPtr pHttpReq=NULL; pHttpReq.CreateInstanc ...
- WF4 常用类<第二篇>
一.WorkflowInvoker 常用方法如下: 方法 说明 BeginInvoke() 使用指定的 AsyncCallback 和用户提供的状态以异步方式调用工作流 EndInvoke() 返回使 ...
- Oracle笔记 目录索引
Oracle笔记 一.oracle的安装.sqlplus的使用 Oracle笔记 二.常用dba命令行 Oracle笔记 三.function .select Oracle笔记 四.增删改.事务 Or ...
- (笔记)angular 多选按钮
- JavaScript for...in 语句
JavaScript for...in 语句 for...in 语句用于遍历数组或者对象的属性(对数组或者对象的属性进行循环操作). for ... in 循环中的代码每执行一次,就会对数组的元素或者 ...
- java初始化深度剖析
当用一个类来创建对象的时候,JVM会帮我们执行下面的6个步骤. 1.在磁盘上找到对应内的class文件: 2.加载class文件到内存,初始化所有静态域(成员变量和方法): 3.如果使用new cla ...
- sqlite:多线程操作数据库“database is locked”解决方法(二)
上一篇博客<sqlite:多线程操作数据库“database is locked”解决方法>通过注册延时函数的方法来处理数据库被锁的问题.此方法固然能解决问题,但是在多个线程向数据库写入大 ...