HRBEU/HEUOJ----1002 A Star not a Tree?
来源:互联网 发布:模拟人生4帅哥捏脸数据 编辑:程序博客网 时间:2024/06/05 02:08
给定n个点,求到这n个点距离和最小的点,输出最小距离。大体思路就是贪心什么的,选定一个起点(0,0)和一个初始步长,按着距离和小的方向走,走到不能走为止,然后减小步长到原来的1/10,重复上面操作,直到步长小于要求的精度返回,用递归写的。
#include <iostream>#include <cstdio>#include <cmath>#define N 105using namespace std;struct Point{double x,y;}p[N];int n;double dir[8][2]={1,0,0,1,-1,0,0,-1,1,1,1,-1,-1,1,-1,-1};inline double dis(Point p1,Point p2){return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}double get_sum(Point p0){double ans=0;for(int i=0;i<n;i++)ans+=dis(p0,p[i]);return ans;}double dfs(Point p0,double step){double sum=get_sum(p0),ts;if(step<0.1) return sum;Point tp;for(int i=0;i<8;i++){tp.x=p0.x+dir[i][0]*step;tp.y=p0.y+dir[i][1]*step;ts=get_sum(tp);if(ts<sum)return dfs(tp,step);}return dfs(p0,step/10.);}int main(){Point p0;while(~scanf("%d",&n)){for(int i=0;i<n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);p0.x=0,p0.y=0;printf("%d\n",int(dfs(p0,100)+0.5));}return 0;}
- HRBEU/HEUOJ----1002 A Star not a Tree?
- A Star not a Tree?
- poj_2420_A Star not a Tree?
- POJ2420 A Star not a Tree?
- POJ 2420 A Star not a Tree?
- A Star not a Tree? 模拟退火
- poj 2420 A Star not a Tree?
- poj 2420 A Star not a Tree?
- POJ 2420 A Star not a Tree?
- 【POJ 2420】A Star not a Tree?
- POJ 2420 A Star not a Tree?
- POJ 2420 A Star not a Tree?
- poj 2420 A Star not a Tree?
- poj 2420 A Star not a Tree?
- poj 2420 A Star not a Tree?
- poj 2420 A Star not a Tree?
- POJ 2420:A Star not a Tree?
- POJ 2420 A Star not a Tree?
- C语言中static的用法
- 6-5 长城维修测试
- 基于C#Winform的简易聊天程序客户端(3)
- boost::mutex vs boost::recursive_mutex
- SQL为字段添加一个唯一约束
- HRBEU/HEUOJ----1002 A Star not a Tree?
- VS2008 stack overflow
- 屏蔽siverlight的右键
- poj 1316 Self Numbers
- memcpy 和 memmove 的区别,memcpy 和 strcpy 的区别
- Excel导入数据库过程中遇到的问题
- 5_28_天天向上
- 5_29_天天向上
- 我的第一个板子--8个LED灯轮回显示