题目链接:

题目

Underground Cables

Time Limit: 3000MS

Memory Limit: Unknown

64bit IO Format: %lld & %llu

问题描述

A city wants to get rid of their unsightly power poles by moving their power cables underground. They have a list of points that all need to be connected, but they have some limitations. Their tunneling equipment can only move in straight lines between points. They only have room for one underground cable at any location except at the given points, so no two cables can cross.

Given a list of points, what is the least amount of cable necessary to make sure that every pair of points is connected, either directly, or indirectly through other points?

输入

There will be several test cases in the input. Each test case will begin with an integer N(2$ \le$N$ \le$1, 000), which is the number of points in the city. On each of the next N lines will be two integers, X and Y(- 1, 000$ \le$X, Y$ \le$1, 000), which are the (X, Y) locations of the N points. Within a test case, all points will be distinct. The input will end with a line with a single 0.

输出

For each test case, output a single real number, representing the least amount of cable the city will need to connect all of its points. Print this number with exactly two decimal places, rounded. Print each number on its own line with no spaces. Do not print any blank lines between answers.

样例

input

4

0 0

0 10

10 0

10 10

2

0 0

10 10

0

output

30.00

14.14

题意

给你n个点,求最少的线缆使得所有的点连在一起

题解

假设存在两根线交叉,那么明显存在一个不交叉的方案使这四个点连通,并且线缆总长度还要更小,所有我们构建完全图跑一遍最短生成树,是可以保证不会出现交叉边的。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std; const int maxn = 1010;
int n; struct Point {
int x, y;
}pt[maxn]; struct Edge {
int u, v;
double w;
Edge(int u, int v, double w) :u(u), v(v), w(w) {}
Edge() {}
bool operator < (const Edge& e) {
return w < e.w;
}
}egs[maxn*maxn]; double dis(const Point &p1, const Point &p2) {
return sqrt(1.0*(p1.x - p2.x)*(p1.x - p2.x) + 1.0*(p1.y - p2.y)*(p1.y - p2.y));
} int fa[maxn];
int find(int x) { return fa[x] = fa[x] == x ? x : find(fa[x]); } void init() {
for (int i = 0; i <= n; i++) fa[i] = i;
} int main() {
while (scanf("%d", &n) == 1 && n) {
init();
int tot = 0;
for (int i = 0; i < n; i++) {
scanf("%d%d", &pt[i].x, &pt[i].y);
for (int j = 0; j < i; j++) {
egs[tot++] = Edge(j, i, dis(pt[j], pt[i]));
}
}
sort(egs, egs + tot);
double ans = 0;
for (int i = 0; i < tot; i++) {
Edge& e = egs[i];
int pu = find(e.u);
int pv = find(e.v);
if (pu != pv) {
ans += e.w;
fa[pv] = pu;
}
}
printf("%.2lf\n", ans);
}
return 0;
}

UVALive 4872 Underground Cables 最小生成树的更多相关文章

  1. UvaLive 4872 Underground Cables (最小生成树)

    题意: 就是裸的最小生成树(MST), 完全图, 边长是实数. 分析: 算是复习一下MST把 方法一: prim 复杂度(n^2) #include <bits/stdc++.h> usi ...

  2. POJ 2075 Tangled in Cables 最小生成树

    简单的最小生成树,不过中间却弄了很久,究其原因,主要是第一次做生成树,很多细节不够熟练,find()函数的循环for判断条件是 pre[i]>=0,也就是遇到pre[i]==-1时停止,i就是并 ...

  3. 图论常用算法之一 POJ图论题集【转载】

    POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...

  4. poj2075

    Tangled in Cables Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6348   Accepted: 2505 ...

  5. UVALive - 2515 (最小生成树 kruskal)

    You are assigned to design network connections between certain points in a wide area. You are given ...

  6. 训练指南 UVALive - 5713(最小生成树 + 次小生成树)

    layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...

  7. ZOJ2326Tangled in Cables(最小生成树)

    Tangled in Cables Time Limit: 2 Seconds      Memory Limit: 65536 KB You are the owner of SmallCableC ...

  8. 最小生成树求最大比率 UVALive - 5713

    题目链接:https://vjudge.net/problem/UVALive-5713 题意:给出t组数据,每组数据第一行给出一个n,表示点的数量,接下来n行,每行有三个数字,分别是点的坐标x,y和 ...

  9. 最小生成树 prime算法 UVALive - 6437

    题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...

随机推荐

  1. jquery更改Reaper某一列的值

    一.实现效果:通过Jquery实现点击repeater中的按钮循环修改快递专线的线路状态 1.初始效果图 2.点击关闭专线按钮之后的效果图 二.MVC模式实现上述效果 SQLServerDAL层 #r ...

  2. PKIX: unable to find valid certification path to requested target

    // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = n ...

  3. oracle中编写java代码

    使用sql语句创建 create or replace and compile java source named test_java_source as package test_java_sour ...

  4. HTML+CSS学习笔记 (13) - CSS代码缩写,占用更少的带宽

    标签:HTML+CSS 盒模型代码简写 还记得在讲盒模型时外边距(margin).内边距(padding)和边框(border)设置上下左右四个方向的边距是按照顺时针方向设置的:上右下左.具体应用在m ...

  5. hdu 2057 A+B Again

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2057 题目分析:涉及到16进制内的加法,可以用%I64x直接来处理,要注意到16进制中负数是用补码来表 ...

  6. Unable to load DLL 'rasapi32.dll': 动态链接库(DLL)初始化例程失败。

    今天做项目传到服务器上碰到下面的问题 但是在本地VS中运行又没问题 后经上网搜索发现只要在web.config文件中加入如下代码即可解决 <system.net>     <defa ...

  7. AngularJS(13)-包含

    AngularJS 包含 使用 AngularJS, 你可以使用 ng-include 指令来包含 HTML 内容: 实例 <body> <div class="conta ...

  8. scp实现mac与linux服务器之间文件传输

    1.mac上传文件到linux服务器 scp 文件名 用户名@服务器ip:目标路径如:scp /Users/test/testFile test@xxx.xxx.xxx.xxx:/test/ 2.ma ...

  9. 浅析Mysql 数据回滚错误的解决方法

    介绍一下关于Mysql数据回滚错误的解决方法.需要的朋友可以过来参考下 MYSQL的事务处理主要有两种方法.1.用begin,rollback,commit来实现begin 开始一个事务rollbac ...

  10. URL锚点HTML定位技术机制

    一.锚点是什么 锚点就等同于火影中的“飞雷神之术”,我们先看百科中锚点的解释: 使用命名锚记可以在文档中设置标记,这些标记通常放在文档的特定主题处或顶部.然后可以创建到这些命名锚记的链接,这些链接可快 ...