POJ 2420 A Star not a Tree? (计算几何-费马点)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3435 | Accepted: 1724 |
Description
problem in order to minimize the total cable length.
Unfortunately, Luke cannot use his existing cabling. The 100mbs system uses 100baseT (twisted pair) cables. Each 100baseT cable connects only two devices: either two network cards or a network card and a hub. (A hub is an electronic device that interconnects
several cables.) Luke has a choice: He can buy 2N-2 network cards and connect his N computers together by inserting one or more cards into each computer and connecting them all together. Or he can buy N network cards and a hub and connect each of his N computers
to the hub. The first approach would require that Luke configure his operating system to forward network traffic. However, with the installation of Winux 2007.2, Luke discovered that network forwarding no longer worked. He couldn't figure out how to re-enable
forwarding, and he had never heard of Prim or Kruskal, so he settled on the second approach: N network cards and a hub.
Luke lives in a loft and so is prepared to run the cables and place the hub anywhere. But he won't move his computers. He wants to minimize the total length of cable he must buy.
Input
Output
Sample Input
4
0 0
0 10000
10000 10000
10000 0
Sample Output
28284
Source
题目大意:
求n边形的费马点。即找到一个点使得这个点到n个点的距离之和最小。
解题思路:
三角形也有费马点。三角形费马点是这样定义的:寻找三角形内的一个点,使得三个顶点到该点的距离之和最小。
三角形费马点的做法是:
(1)若有一个角大于120度。那么这个角所在的点就是费马点。
(2)若不存在。那么对于三角形ABC,任取两条边(如果AB、AC),向外做等边三角形得到C' 和 A' 。那么AA' 和CC' 的交点就是费马点。
那么对于这题n多边形,我採取的策略全然不同,採用了模拟退火的做法。这样的做法相对照较简单,也能够用在求三角形费马点之中。
模拟退火算法就跟数值算法里面的自适应算法同样的道理
(1)定义好步长
(2)寻找一个起点,往8个方向的按这个步长搜索。
(3)假设找到比答案更小的点,那么以这个新的点为起点,反复(2)
(4)假设找不到比答案更小的点,那么步长减半,再尝试(2)
(5)直到步长小于要求的答案的精度就停止。
解题代码:
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std; const int offx[]={1,1,0,-1,-1,-1,0,1};
const int offy[]={0,1,1,1,0,-1,-1,-1};
const int maxn=110;
const double eps=1e-2; struct point{
double x,y;
point(double x0=0,double y0=0){x=x0;y=y0;}
double getdis(point p){
return sqrt( (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y) );
}
}p[maxn]; int n; double getsum(point p0){
double sum=0;
for(int i=0;i<n;i++) sum+=p0.getdis(p[i]);
return sum;
} void solve(){
for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
double ans=getsum(p[0]),step=100;
point s=p[0],d;
while(step>eps){
bool flag=false;
for(int i=0;i<8;i++){
d=point(s.x+step*offx[i],s.y+step*offy[i]);
double tmp=getsum(d);
if(tmp<ans){
s=d;
ans=tmp;
flag=true;
}
}
if(!flag) step/=2;
}
printf("%.0f\n",ans);
} int main(){
while(scanf("%d",&n)!=EOF){
solve();
}
return 0;
}
POJ 2420 A Star not a Tree? (计算几何-费马点)的更多相关文章
- 三分 POJ 2420 A Star not a Tree?
题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...
- [POJ 2420] A Star not a Tree?
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4058 Accepted: 200 ...
- POJ 2420 A Star not a Tree? 爬山算法
B - A Star not a Tree? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...
- poj 2420 A Star not a Tree? —— 模拟退火
题目:http://poj.org/problem?id=2420 给出 n 个点的坐标,求费马点: 上模拟退火. 代码如下: #include<iostream> #include< ...
- poj2420 A Star not a Tree? 找费马点 模拟退火
题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...
- poj 2420 A Star not a Tree?——模拟退火
题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include ...
- POJ 2420 A Star not a Tree?(模拟退火)
题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...
- POJ 2420 A Star not a Tree?【爬山法】
题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只 ...
- 【POJ】2420 A Star not a Tree?(模拟退火)
题目 传送门:QWQ 分析 军训完状态不好QwQ,做不动难题,于是就学了下模拟退火. 之前一直以为是个非常nb的东西,主要原因可能是差不多省选前我试着学一下但是根本看不懂? 骗分利器,但据说由于调参困 ...
随机推荐
- 剑指offer-面试题5.从尾到头打印链表
题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值. 刚看到这道题的小伙伴可能就会想,这还不简单,将链表反转输出. 但是这种情况破坏了链表的结构. 如果面试官要求不破坏链表结构呢,这时候我们 ...
- Poj 2187 Beauty Contest_旋转凸包卡壳
题意:给你n个坐标,求最远的两点距离 思路:用凸包算法求处,各个定点,再用旋转凸包卡壳 #include <iostream> #include <cstdio> #inclu ...
- Unity 鼠标点击左右移动,人物跟随旋转
上代码: using UnityEngine; using System.Collections; public class Test : MonoBehaviour { private Vector ...
- java学习之数据库
第一部分:DB2基础知识 一.复习 jre jdk jvm path classpath 二.数据库基础-DB2 1.访问172.22.602.24/ temp 123 2.安装检查 db2cmd d ...
- net 2.0使用ajax
asp.net ajax中用到了几个dll文件,这些可以从网上下载.http://ajax.asp.net站点下可以找到相关的下载.这其中包括:System.Web.Extensions.dll.Sy ...
- SQL Performance Improvement Techniques(转)
原文地址:http://www.codeproject.com/Tips/1023621/SQL-Performance-Improvement-Techniques This article pro ...
- data source 和initial catalog
initial catalog与database的区别是什么Initial Catalog: DataBase: 两者没有任何区别只是名称不一样,就好像是人类的真实姓名与曾用名一样..都可以叫你. * ...
- [原创]Windows下Google V8 javascript引擎编译
项目用到将v8嵌入到C++的情况,公司没时间研究,只有在家研究,编译过程一堆坑.记录一下. 网上百度的都是基于vs2010,或者早版本的v8编译,最新版本应该使用vs2013\vs2015.本文介绍的 ...
- Oracle表分区[转]
废话少说,直接讲分区语法. Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: create table gra ...
- [Non-original]OS X How do I unset an IP address set with ifconfig?
I recently used ifconfig en1 1.2.3.4 to set the IP address of a network interface (specifically, the ...