UVALive 4872 Underground Cables 最小生成树
题目链接:
题目
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 最小生成树的更多相关文章
- UvaLive 4872 Underground Cables (最小生成树)
题意: 就是裸的最小生成树(MST), 完全图, 边长是实数. 分析: 算是复习一下MST把 方法一: prim 复杂度(n^2) #include <bits/stdc++.h> usi ...
- POJ 2075 Tangled in Cables 最小生成树
简单的最小生成树,不过中间却弄了很久,究其原因,主要是第一次做生成树,很多细节不够熟练,find()函数的循环for判断条件是 pre[i]>=0,也就是遇到pre[i]==-1时停止,i就是并 ...
- 图论常用算法之一 POJ图论题集【转载】
POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...
- poj2075
Tangled in Cables Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6348 Accepted: 2505 ...
- UVALive - 2515 (最小生成树 kruskal)
You are assigned to design network connections between certain points in a wide area. You are given ...
- 训练指南 UVALive - 5713(最小生成树 + 次小生成树)
layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...
- ZOJ2326Tangled in Cables(最小生成树)
Tangled in Cables Time Limit: 2 Seconds Memory Limit: 65536 KB You are the owner of SmallCableC ...
- 最小生成树求最大比率 UVALive - 5713
题目链接:https://vjudge.net/problem/UVALive-5713 题意:给出t组数据,每组数据第一行给出一个n,表示点的数量,接下来n行,每行有三个数字,分别是点的坐标x,y和 ...
- 最小生成树 prime算法 UVALive - 6437
题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...
随机推荐
- Python Tool Visual Studio简单使用
由于一直在做.NET的开发,一直用的IDE是VS系列的,所以想用VS也能开发Python,刚好微软提供一个插件PTVS(Python Tool Visual Studio)专门应用于Python开发的 ...
- UILabel自适应高、宽
根据Label和字体大小自适应高度 - (CGFloat)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size { label.n ...
- VxWorks 6.9 内核编程指导之读书笔记 -- VxWorks Kernel application (二)
#1 内核对象的静态实例化 内核对象的静态实例化 任务的静态实例化 VX_TASK宏用来在编译时声明一个任务对象.该宏带有2个参数:任务名和栈大小.不像taskSpawn函数,任务名称可以是NULL. ...
- javascript代码复用--继承
由于javascript没有类的概念,因此无法通过接口继承,只能通过实现继承.实现继承是继承实际的方法,javascript中主要是依靠原型链要实现. 原型链继承 原型链继承是基本的继承模式,其本质是 ...
- 怎样按字母顺序(ABCDEF)动态添加控件
考试系统中题库设计时,我想动态添加选项,顺序按ABCDEF这样,点击一下按钮添加A(radiobutton),再点击添加B,如此依次添加.本人比较菜,求达人写一个方法. private void bu ...
- 支持IE6的树形节结构TreeTable
关于TreeTable实际应用的案例:http://www.cnblogs.com/qigege/p/5213689.html treeTable是跨浏览器.性能很高的jquery的树表组件,它使用非 ...
- [大牛翻译系列]Hadoop(2)MapReduce 连接:复制连接(Replication join)
4.1.2 复制连接(Replication join) 复制连接是map端的连接.复制连接得名于它的具体实现:连接中最小的数据集将会被复制到所有的map主机节点.复制连接有一个假设前提:在被连接的数 ...
- Zencart 国家排序及中文名称的扩展
最终实现效果如上 具体步骤: 1. 手动或SQL修改数据表,增加2个字段 ) ) '; 2. 修改admin/countries.php文件,增加表单插入编辑功能, 共计7处,此处忽略具体代码. 3. ...
- 【Delphi】圆角窗体
procedure TForm1.FormCreate(Sender: TObject); var hr :thandle; begin hr:=createroundrectrgn(1,1,widt ...
- smarty安装及例子
环境: smarty3.1.16 1.在http://www.smarty.net/download下载最新smarty包,window选择zips,linux下选择tar.gz.以windows为例 ...