POJ 2420 A Star not a Tree?(模拟退火)
2013-10-21 10:26
447 查看
题目链接
居然1Y了,以前写的模拟退火很靠谱啊。
居然1Y了,以前写的模拟退火很靠谱啊。
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <ctime> #include <cstdlib> #include <iostream> using namespace std; struct point { double x,y; }p[201]; int n; int a[4] = {0,0,-1,1}; int b[4] = {1,-1,0,0}; double dis(double x1,double y1,double x2,double y2) { return sqrt((x1-x2)*(x1-x2)+(y2-y1)*(y2-y1)); } double fun(double x,double y) { double ans = 0; int i; for(i = 0;i < n;i ++) { ans += dis(p[i].x,p[i].y,x,y); } return ans; } int main() { int T,i,key,num,j,k; double tx,ty,ans,d,x,y; srand(time(NULL)); scanf("%d",&n); for(i = 0;i < n;i ++) { scanf("%lf%lf",&p[i].x,&p[i].y); } key = 5; ans = 100000000; num = 200; x = p[0].x; y = p[0].y; T = 40; while(T--) { for(i = 1;i <= num;i ++) { for(j = 0;j < 4;j ++) { k = rand()%key; tx = x + k*a[j]*T; ty = y + k*b[j]*T; if(tx >= 0&&tx <= 10000&&ty >= 0&&ty <= 10000) { d = fun(tx,ty); if(ans > d) { ans = d; x = tx; y = ty; } } } } } printf("%.f\n",ans); return 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?
- POJ2420 A Star not a Tree?
- POJ2420——三分——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?