poj 2420 A Star not a Tree?
来源:互联网 发布:java 切面 编辑:程序博客网 时间:2024/06/06 00:32
题意:给出平面上N(<=100)个点,你需要找到一个这样的点,使得这个点到N个点的距离之和尽可能小。输出这个最小的距离和(四舍五入到最近的整数)。
分析:用模拟退火
注意:在poj上提交,g++,double printf("%f")而不要printf("%lf)
#include <iostream>#include <cstdio>#include <vector>#include <string>#include <algorithm>#include <ctime>#include <cmath>#define INF 0xffffff#define N 10#define M 20#define PI acos(-1.0)using namespace std;const int RD = 1000;struct Point{ double x; double y; Point() { } Point(double xx,double yy) { x = xx; y = yy; }};vector<Point> point;vector<double> mini_sum;vector<Point> Random;vector<Point>::iterator it;double getRandom(){ return (rand()%RD + 1) * 1.0 /RD;}Point getPoint(Point a,Point b){ double x = a.x + (- a.x + b.x)*getRandom(); double y = a.y + (- a.y + b.y) *getRandom() ; Point pp = Point(x,y); return pp;}double diss(Point p1,Point p2){ return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y )*(p1.y - p2.y));}double getDouble(Point& p){ double mini = 0; for(it = point.begin();it!=point.end();it++) { mini += diss(p,*it); } return mini;}int main(){ int n,i,j; double a,b,mini; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%lf %lf",&a,&b); point.push_back(Point(a,b)); } Point p1 = Point(-100000.0,-100000.0); Point p2 = Point(100000.0,100000.0); Point p0; for(i = 0; i < N; i++) { p0 = getPoint(p1,p2); Random.push_back(p0); mini = getDouble(p0); mini_sum.push_back(mini); // printf("mini[%d] = %.1lf\n",i,mini); } double delta = max(p2.y-p1.y,p2.x-p1.x)/n; while(delta > 1e-3) { for(i=0;i<N;i++) { for(j=0;j<M;j++) { double angle = getRandom() * 2 * PI; Point cur; cur.x = Random[i].x + delta * cos(angle); cur.y = Random[i].y + delta * sin(angle); double sum = getDouble(cur); if(sum < mini_sum[i]) { mini_sum[i] = sum; Random[i] = cur; } } } delta*=0.85; } double ans = INF; for(i=0;i<N;i++) { if(mini_sum[i] < ans) ans = mini_sum[i]; } printf("%.0f\n",ans); return 0;}
1、输出double类型时,如果采用G++提交,scanf采用%lf,prinf采用%f,否则会报错
2、使用GCC/G++的提醒:
对于64位整数, long long int 和 __int64 都是支持并且等价的.但是在读和写的时候只支持scanf("%
I64d", ...)和printf("%I64d", ...).
不支持"%lld"是因为MinGW下的GCC和G++使用的msvcrt.dll动态链接库并不支持C99标准.
根据ISO C++标准,在G++下,main函数的返回值必须是int,否则将会导致Compile Error(编译错误)的判答
3、G++/GCC使用scanf、printf时注意引用<stdio.h>,只引用<iostream>不识别
4、srand(time(NULL)),g++也会报错 0 0
- 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?
- 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?(模拟退火求费马点)
- 在Eclipse中在线安装插件SVN和Android studio中配置SVN的一些坑
- 欢迎使用CSDN-markdown编辑器
- android 自定义view
- 解密SparkStreaming运行机制和架构
- JQuery方法查询
- poj 2420 A Star not a Tree?
- 在编辑框(CEdit)控件上追加内容
- css文件内容格式化
- c/c++ 复习随记(4)----fwqlzz love is for ever
- BZOJ 4522: [Cqoi2016]密钥破解
- inline-block和float应区别对待
- 栈与队列
- 诗歌五
- C# 模拟 鼠标 键盘操作