POJ2560 Freckles
| Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.
Input
Output
Sample Input
3
1.0 1.0
2.0 2.0
2.0 4.0
Sample Output
3.41
Source
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int mxn=;
int n;
struct point{
double x;double y;
}p[mxn];
double dist(point a,point b){
return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
}
struct edge{
int x,y;
double dis;
}e[mxn*mxn];int cnt=;
int cmp(edge a,edge b){
return a.dis<b.dis;
}
int fa[mxn];
void init(){
for(int i=;i<=n;i++)fa[i]=i;
}
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
double ans=;
void Kruskal(){
int i,j;
int ti=;
ans=;
for(i=;i<=cnt;i++){
int u=find(e[i].x),v=find(e[i].y);
if(u==v)continue;
ti++;
ans+=e[i].dis;
fa[u]=v;
if(ti==n-)break;
}
return;
}
int main(){
int i,j;
while(scanf("%d",&n)!=EOF){
init();
cnt=;
int u,v,dis;int m;
int i,j;
for(i=;i<=n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
for(i=;i<n;i++)
for(j=i+;j<=n;j++){
e[++cnt]=(edge){i,j,dist(p[i],p[j])};
}
sort(e+,e+cnt+,cmp);
Kruskal();
printf("%.2f\n",ans);
}
return ;
}
POJ2560 Freckles的更多相关文章
- 10034 - Freckles 克鲁斯克尔最小生成树!~
/* 10034 - Freckles 克鲁斯克尔最小生成树!- */ #include<iostream> #include<cstdio> #include<cmat ...
- poj 2560 Freckles
题目连接 http://poj.org/problem?id=2560 Freckles Description In an episode of the Dick Van Dyke show, li ...
- freckles
题目描述: In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back ...
- 题目1144:Freckles(最小生成树进阶)
题目链接:http://ac.jobdu.com/problem.php?pid=1144 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- 九度OJ 1144:Freckles(斑点) (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1538 解决:760 题目描述: In an episode of the Dick Van Dyke show, little Richi ...
- 【九度OJ】题目1144:Freckles 解题报告
[九度OJ]题目1144:Freckles 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1144 题目描述: In an ...
- UVA 10034 Freckles 最小生成树
虽然是道普通的最小生成树题目,可还是中间出了不少问题,暴露的一个问题是不够细心,不够熟练.所以这篇博客就当记录一下bug吧. 代码一:kruskal #include<stdio.h> # ...
- uva 10034 Problem A: Freckles
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- POJ 2560 Freckles Prime问题解决算法
这个问题正在寻求最小生成树. 给定节点的坐标,那么我们需要根据各个点之间的这些坐标来计算距离. 除了这是标准的Prime算法的,能源利用Prime基本上,你可以使用Kruskal. 经典的算法必须填写 ...
随机推荐
- GNU make(2)
GNU make(2) 参考: GNU Make学习总结(二) 变量 变量由一个前导符号$加上字符或者是括号字符组成, 名称区分大小写. 命名: 习惯上用全部大写字符表示常量, 小写字符表示变量, 单 ...
- 与调试器共舞 - LLDB 的华尔兹
你是否曾经苦恼于理解你的代码,而去尝试打印一个变量的值? 1 NSLog(@"%@", whatIsInsideThisThing); 或者跳过一个函数调用来简化程序的行为? 1 ...
- IOS修改系统音量
#import <IOKit/IOKitLib.h> #import <IOKit/hidsystem/IOHIDLib.h> #import <IOKit/hidsys ...
- Luogu P2397 yyy loves Maths VI (mode)
题目传送门 虽然只是一道黄题,但还是学到了一点新知识-- 摩尔投票法 用\(O(1)\)的内存,\(O(n)\)的时间来找出一串长度为n的数中的众数,前提是众数出现的次数要大于\(n/2\) 方法很简 ...
- 详解Mac睡眠模式设置
详解Mac睡眠模式设置 原文链接:http://www.insanelymac.com/forum/index.php?showtopic=281945 需要说明的是,首先这篇文章是针对已经能够成功睡 ...
- 响应式web设计视图工具及插件总结----20150113
响应式web设计可以说火不火是迟早的,下面就对于最开始的视口调试的方法汇总,希望有好的方法大家一起交流. 1.火狐:从Firefox升级到29.0之后就不直接支持Firesizer了. 先安装Add- ...
- 【Java_多线程并发编程】JUC原子类——AtomicLong原子类
1. AtomicLong是基本原子类中的一种 AtomicLong是对长整形进行原子操作. 1.1 AtomicLong类的函数列表 // 构造函数 AtomicLong() // 创建值为init ...
- mysql 导入数据库
1:创建数据库 dos 进入 xxx\MySQL5.7\bin 目录(很多人喜欢把这个路径配置在环境变量path中,这样在dos敲命令时,就直接是mysql......) mysql -uroot - ...
- 1 producer — n consumers 模型 实现
#include<stdio.h> #include<string.h> #include<pthread.h> #include<stdlib.h> ...
- Oracle排错总结
一.Oracle常规恢复之不安全恢复 http://www.cnblogs.com/jyzhao/p/4723994.html#2.11