题目连接

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 (XiYi) on the plane (0 ≤ X≤ 1,000,000; 0 ≤ Y≤ 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: Xand Y
* 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的更多相关文章

  1. poj 3625 Building Roads(最小生成树,二维坐标,基础)

    题目 //最小生成树,只是变成二维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> ...

  2. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  3. poj 2749 Building roads (二分+拆点+2-sat)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 De ...

  4. [poj] 2749 building roads

    原题 2-SAT+二分答案! 最小的最大值,这肯定是二分答案.而我们要2-SATcheck是否在该情况下有可行解. 对于目前的答案limit,首先把爱和恨连边,然后我们n^2枚举每两个点通过判断距离来 ...

  5. POJ 2749 Building roads 2-sat+二分答案

    把爱恨和最大距离视为限制条件,可以知道,最大距离和限制条件多少具有单调性 所以可以二分最大距离,加边+check #include<cstdio> #include<algorith ...

  6. Building roads

    Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  7. [POJ2749]Building roads(2-SAT)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8153   Accepted: 2772 De ...

  8. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  9. 多次访问节点的DFS POJ 3411 Paid Roads

    POJ 3411 Paid Roads Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 24 ...

随机推荐

  1. gis 导出 dwg,shp

    当我们在webgis 想要把某个地块或者多个地块导出dwg或者shp文件的时候怎么办?这个时候最好就是用后台的方式.首先把web gis上的graphic 的polygon提取为坐标的形式(类似于x, ...

  2. 【PL/SQL练习】命名块: 存储过程、函数、触发器、包

    创建时定义名称 2.可以被Oracle server 保存 3.可以被任何程序调用 4.可以被共享 存储过程: 1.不带参数的存储过程: SQL> create or replace proce ...

  3. ios assetlibrary

    公司做个app项目,用phonegap做,好调页面,哎,就是骗那些土大款客户,觉得phonegap性能一般吧,不过html5的确好强大,页面设计好了看起来也好看.原生的用的不多,比如什么二维码扫描啊, ...

  4. procfs

    https://www.kernel.org/doc/Documentation/filesystems/proc.txt /proc/stat cpu 493610 1050 955506 6140 ...

  5. Struts2使用ModelDriven后JSON数据返回不正确

    在struts.xml中加入<param name="root">action</param> <result name="exist&qu ...

  6. asp.net 在线人数

    很网站都有在线人数,这一功能无处不在.现在,我们就介绍在.NET中一个简单明了的方法来统计在线用户的多少,该方法的特点就是充分的利用了ASP.NET的特点,结合global.asax文件,用Appli ...

  7. MS SqlSever一千万条以上记录分页数据库优化经验总结【索引优化 + 代码优化】[转]

    对普通开发人员来说经常能接触到上千万条数据优化的机会也不是很多,这里还是要感谢公司提供了这样的一个环境,而且公司让我来做优化工作.当数据库中的记录不超过10万条时,很难分辨出开发人员的水平有多高,当数 ...

  8. Visual studio 2013 Team Foundation Server TFS2013 设置签出独占锁

    摘自: http://www.cnblogs.com/52XF/p/4239056.html 以备自查 如侵权,请告知

  9. Android的Intent作用

    Android应用程序中有三个核心组件Activity.Services.Broadcast Receiver. Intent 提供应用程序之间的的交互机制,负责对一次操作的动作,动作涉及的数据,附加 ...

  10. 转:OpenCms 9.0.1汉化

    LHD私人汉化. 1.完成安装OpenCms 2.如果正确安装,在浏览器输入以下地址即可打开登录页面(默认账号/密码:Admin/admin) http://localhost:8080/opencm ...