poj2420A Star not a Tree?(模拟退火)

链接

求某一点到其它点距离和最小,求这个和,这个点 为费马点。

做法:模拟退火

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<stdlib.h>
 6 #include<vector>
 7 #include<cmath>
 8 #include<queue>
 9 #include<set>
10 using namespace std;
11 #define N 100000
12 #define LL long long
13 #define INF 0xfffffff
14 const double eps = 1e-8;
15 const double pi = acos(-1.0);
16 const double inf = ~0u>>2;
17 struct point
18 {
19     double x,y;
20     point(double x=0,double y =0):x(x),y(y){}
21 }p[N];
22 int n;
23 int dir[4][2] = {0,1,0,-1,1,0,-1,0};
24 typedef point pointt;
25 pointt operator -(point a,point b)
26 {
27     return point(a.x-b.x,a.y-b.y);
28 }
29 double dis(point a)
30 {
31     return sqrt(a.x*a.x+a.y*a.y);
32 }
33 double cal(point pp)
34 {
35     int i;
36     double s = 0;
37     for(i = 1; i <= n; i++)
38     s+=dis(pp-p[i]);
39     return s;
40 }
41 int main()
42 {
43     int i;
44     while(scanf("%d",&n)!=EOF)
45     {
46         double t = 10000;
47         point pp = point(0,0);
48         for(i =1 ; i <= n;i ++)
49         {
50             scanf("%lf%lf",&p[i].x,&p[i].y);
51             pp.x+=p[i].x;
52             pp.y+=p[i].y;
53         }
54         pp.x /=n;
55         pp.y /=n;
56         double sum = cal(pp),tmp;
57         point tp,ans = pp;
58         while(t>0.02)
59         {
60             int flag = 1;
61             while(flag)
62             {
63                 flag = 0;
64                 for(i =0 ; i< 4 ; i++)
65                 {
66                     tp = point(pp.x+dir[i][0]*t,pp.y+dir[i][1]*t);
67                     tmp = cal(tp);
68                     if(tmp<sum)
69                     {
70                         sum = tmp;
71                         flag = 1;
72                         ans = tp;
73                     }
74                 }
75                 pp = ans;
76             }
77             t/=2;
78         }
79         printf("%.0f\n",sum);
80     }
81     return 0;
82 }

poj2420A Star not a Tree?(模拟退火)

时间: 2024-08-02 17:33:11

poj2420A Star not a Tree?(模拟退火)的相关文章

UVA 10228 - Star not a Tree?(模拟退火)

UVA 10228 - Star not a Tree? 题目链接 题意:给定一些点,费马点(到这些点距离和最短),输出距离和 思路:模拟退火去搞,初始温度1W步,降温系数设为0.83,然后每次找周围4个方向,做10次保证答案准确 代码: #include <cstdio> #include <cstring> #include <cmath> #include <ctime> #include <cstdlib> #include <al

poj-2420 A Star not a Tree?(模拟退火算法)

题目链接: A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5219   Accepted: 2491 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allo

POJ 2420 A Star not a Tree? (模拟退火)

题目地址:POJ 2420 今天在比赛遇到了这题..于是现场学了一下模拟退火.... 这题是先初始化为一个点,然后不断趋近距离和最短的点.还是挺简单的.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h&g

poj 2420 A Star not a Tree?——模拟退火

题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<ctime> #define db double usin

POJ 2420 A Star not a Tree? (计算几何-费马点)

A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3435   Accepted: 1724 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you

[POJ 2420] A Star not a Tree?

A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4058   Accepted: 2005 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you

【POJ 2420】A Star not a Tree?

A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3929   Accepted: 1952 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you

A Star not a Tree?

题目链接 题意: 给n个点,求费马点到各点的距离和 分析: 费马点直接求即可,用模拟退火即可 这个方法其实就是默认费马点只有一个,那么就直接求局部最优解即可,不用再以一定概率去接受较差的状态 const double eps = 1e-5; const int MAXN = 110000; struct Point { double x, y; Point(double x=0, double y=0):x(x),y(y) { } }; int n; Point ipt[MAXN]; doubl

三分 POJ 2420 A Star not a Tree?

题目传送门 1 /* 2 题意:求费马点 3 三分:对x轴和y轴求极值,使到每个点的距离和最小 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3f3f; 12 double x[MAXN], y[MAXN]; 13 i