模拟退火算法。。。。

这道题,呃。我怎么感觉他就是随机的。同一个代码,时而AC,时而WA。其实还真的是随机的。呵呵呵呵呵。。。因为下降火太快了,没办法,而降得慢又会。。。TLE,虽然精度提高了。

敢问,还有什么好的方法?我是在做退火算法时遇到这个练手的。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <time.h> using namespace std;
const int MAXN=55;
const double PI=3.141592653;
const double eps=1e-5; #define zero(a) fabs(a)<eps struct point {
double x,y;
};
struct Segment{
point a,b;
}; point p[MAXN]; int n; double ans; int cot;
point tar[MAXN]; double best[MAXN]; double R; point operator -(point &u,point &v){
point re;
re.x=u.x-v.x; re.y=u.y-v.y;
return re;
} double dot(point &u,point &v){
return u.x*v.x+u.y*v.y;
} double dist(point tt){
return sqrt(tt.x*tt.x+tt.y*tt.y);
}
double multi(point &u,point &v){
return u.x*v.y-u.y*v.x;
}
/*
bool whether_in(point &t){
double angle=0;
for(int i=0;i<n;i++){
point A=p[i]-t;
point B=p[i+1]-t;
angle+=acos(dot(A,B)/dist(A)/dist(B));
}
if(fabs(angle-PI*2)<1e-5)
return true;
return false;
} */ double xmul(point p0,point p1,point p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
} bool online(point p1,point p2,point p){
if(zero(xmul(p1,p2,p))&&((p.x-p1.x)*(p.x-p2.x)<eps&&(p.y-p1.y)*(p.y-p2.y)<eps))
return true;
return false;
} inline bool across(Segment s1,Segment s2){
if(xmul(s1.a,s1.b,s2.a)*xmul(s1.a,s1.b,s2.b)<eps)
if(xmul(s2.a,s2.b,s1.a)*xmul(s2.a,s2.b,s1.b)<eps)
return true;
return false;
} bool whether_in(point cen){
int cnt=0;
Segment s,e;
s.a=cen;s.b.y=cen.y;s.b.x=20000.0;
for(int i=0;i<n;i++){
e.a=p[i];e.b=p[i+1];
if(online(p[i],p[i+1],cen)) return false;
if(zero(p[i].y-p[i+1].y)) continue;
if(online(s.a,s.b,p[i])){
if(p[i].y>p[i+1].y) cnt++;
}
else if(online(s.a,s.b,p[i+1])){
if(p[i+1].y>p[i].y) cnt++;
}
else if(across(s,e))
cnt++;
}
return cnt&1;
} double count_d(point &tt){
double mm=1e10; double tp;
for(int i=0;i<n;i++){
point A=tt-p[i];
point B=p[i+1]-p[i];
if(dot(A,B)<=0){
tp=dist(A);
// cout<<"1"<<' '<<tp<<endl;
mm=min(mm,tp);
continue;
}
A=tt-p[i+1];
B=p[i]-p[i+1];
if(dot(A,B)<=0){
tp=dist(A);
mm=min(mm,tp);
// cout<<"2"<<' '<<tp<<endl;
continue;
}
double area=multi(A,B);
tp=fabs(area)/dist(p[i]-p[i+1]);
// cout<<"3"<<' '<<tp<<endl;
mm=min(mm,tp);
}
return mm;
} double getdouble(){
double re=((rand()*rand())%1000000)*1.0/1e6;
return re;
} int main(){
srand(time(0));
while(scanf("%d",&n),n){
cot=0;
for(int i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
scanf("%lf",&R);
p[n]=p[0];
point tmp;
for(int i=0;i<n;i++){
tmp.x=(p[i].x+p[i+1].x)/2;
tmp.y=(p[i].y+p[i+1].y)/2;
tar[cot++]=tmp;
}
// cout<<cot<<endl;
bool flag=false;
for(int i=0;i<cot;i++){
best[i]=0;
}
// cout<<"YES"<<endl;
double T=50;
for(double t=T;t>1e-4;t*=0.55){
for(int i=0;i<cot;i++){
for(int j=0;j<10;j++){
double td=getdouble();
if(rand()&1) td*=-1;
tmp.x=tar[i].x+td*t;
td=getdouble();
if(rand()&1) td*=-1;
tmp.y=tar[i].y+td*t;
if(whether_in(tmp)){
td=count_d(tmp);
if(td>best[i]){
best[i]=td;
tar[i]=tmp;
}
if(td>=R||fabs(td-R)<1e-4){
// cout<<tmp.x<<' '<<tmp.y<<endl;
// cout<<td<<endl;
flag=true;
break;
}
}
}
if(flag) break;
}
if(flag) break;
}
if(flag) printf("Yes\n");
else printf("No\n");
}
return 0;
}

  

HDU 3644的更多相关文章

  1. HDU - 3644:A Chocolate Manufacturer's Problem(模拟退火, 求多边形内最大圆半径)

    pro:给定一个N边形,然后给半径为R的圆,问是否可以放进去.  问题转化为多边形的最大内接圆半径.(N<50): sol:乍一看,不就是二分+半平面交验证是否有核的板子题吗. 然而事情并没有那 ...

  2. 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856

    并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. windows php文件下载地址

    http://windows.php.net/downloads/releases/archives/

  2. Python 37 进程池与线程池 、 协程

    一:进程池与线程池 提交任务的两种方式: 1.同步调用:提交完一个任务之后,就在原地等待,等任务完完整整地运行完毕拿到结果后,再执行下一行代码,会导致任务是串行执行 2.异步调用:提交完一个任务之后, ...

  3. TCP/IP详解(二)

    首先,不得不吐槽一下中文版的翻译,把英文版的很多部分的删除了.中文版的pdf只有400多页,英文版有1000多页.迫于时间,只有先将就着看中文版,但是遇到不懂的地方,一定要对照英文版来看. 滑动窗口协 ...

  4. JavaScript 进阶 常用内置对象

    一.常用内置对象 所谓内置对象就是ECMAscript提供出来的一些对象,我们知道对象都是有相应的属性和方法 数组Arry 1.数组的创建方式 字面量方式创建(推荐使用,简单粗暴) var color ...

  5. .net core2.0 自定义中间件

    一.中间件(Middleware) 中间件是被组装成一个应用程序管道来处理请求和响应的软件组件. 二.编写SimpleMiddleware using Microsoft.AspNetCore.Htt ...

  6. python3爬取豆瓣排名前250电影信息

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @File : doubanmovie.py # @Author: Anthony.waa # @Dat ...

  7. 06-联系人管理(xib应用)

    ViewController.h文件中: @interface ViewController : UIViewController - (IBAction)add:(UIBarButtonItem * ...

  8. bootstrap模态框和select2合用时input无法获取焦点(转)

    在bootstrap的模态框里使用select2插件,会导致select2里的input输入框没有办法获得焦点,没有办法输入. 解决方法: 1. 把页面中的  tabindex="-1&qu ...

  9. Webpack 打包学习

    前段时间项目主管让测试组长研究webpack打包方式,闲暇时自己想学习一下,留着备用,本周日学习一下. https://www.jianshu.com/p/42e11515c10f

  10. AI:忧郁的机器人

    1.塔奇克马 塔奇克马研究起来哲学,被缴械....... 2.机器人瓦力 孤独等待EVA的瓦力 3.马文 http://www.guokr.com/post/683881/