POJ 2420 A Star not a Tree? (模拟退火)
2015-09-15 13:49
465 查看
职务地址:POJ 2420
今天在比赛遇到了这题。。
于是现场学了一下模拟退火。。。。
这题是先初始化为一个点,然后不断趋近距离和最短的点。还是挺简单的。
。
代码例如以下:
今天在比赛遇到了这题。。
于是现场学了一下模拟退火。。。。
这题是先初始化为一个点,然后不断趋近距离和最短的点。还是挺简单的。
。
代码例如以下:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map> #include <set> #include <algorithm> using namespace std; #define LL __int64 const int INF=0x3f3f3f3f; int n; struct point { int x, y; }dian[200]; double dist(point a, point b) { return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y)); } double juhe(point x) { int i; double z=0; for(i=0;i<n;i++) { z+=dist(x,dian[i]); } return z; } int main() { int i, j, flag, step; point st, ed; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d%d",&dian[i].x,&dian[i].y); } step=100; st.x=0;st.y=0; double d=juhe(st), s; while(step>0.2) { flag=1; while(flag) { flag=0; point now; now.x=st.x+step; now.y=st.y; s=juhe(now); if(d>s) { flag=1; d=s; ed=now; } now.x=st.x-step; now.y=st.y; s=juhe(now); if(d>s) { flag=1; d=s; ed=now; } now.x=st.x; now.y=st.y+step; s=juhe(now); if(d>s) { flag=1; d=s; ed=now; } now.x=st.x; now.y=st.y-step; s=juhe(now); if(d>s) { flag=1; d=s; ed=now; } if(flag) st=ed; } step/=2.0; } printf("%.0f\n",d); return 0; }
相关文章推荐
- hibernate查询和jdbc查询
- Boss 怒改需求,产品经理该怎么办?
- 【转】 数学建模十大经典算法漫谈
- apk反编译
- 基于Jpcap的TCP/IP数据包分析(一)
- unix/linux共享内存应用与陷阱
- C#获得硬件信息(转载)
- 使用XCA(X Certificate and key management)可视化项目经理SSL 凭证(4)--凭借自身的凭证管理中心的定义(Certificate Authority)签名证书申请
- 文件上传之黑名单验证绕过
- Android之根据Uri获得图片或视频文件路径(解决4.4以上版本得不到路径的情况)
- OPEN(SAP) UI5 学习入门系列之三:MVC (下) - 视图与控制器
- C语言link过程详解(多文件编译过程)
- 让IE8浏览器支持Canvas
- Android之根据Uri获得图片或视频文件路径(解决4.4以上版本得不到路径的情况)
- 五个你可能闻所未闻的出色的 Ubuntu 替代发行版
- 揭秘12306技术改造(三):传统框架云化迁移到内存数据平台
- jquery常用案例
- zoj 3471 Most Powerful(状态压缩dp)
- YII的自定义路由规则类的使用(初级)
- 磁盘管理及文件系统管理