HDU 3644
模拟退火算法。。。。
这道题,呃。我怎么感觉他就是随机的。同一个代码,时而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的更多相关文章
- HDU - 3644:A Chocolate Manufacturer's Problem(模拟退火, 求多边形内最大圆半径)
pro:给定一个N边形,然后给半径为R的圆,问是否可以放进去. 问题转化为多边形的最大内接圆半径.(N<50): sol:乍一看,不就是二分+半平面交验证是否有核的板子题吗. 然而事情并没有那 ...
- 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856
并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- git的使用(转)
git 配置文件 git的配置文件位置针对所有用户:/etc/gitconfig针对当前用户: -/.gitconfig 查看配置的方法 git config --list 修改配置的方法 git c ...
- Django day14(二) Session
Session 1. Session干什么用的? 解决cookie不安全的问题, 2. 原理是: 1) 存在服务器上的键值对{'sdaf随机字符串':{name:prince,pwd:123}} 2 ...
- Django 安装步骤
Django的安装和简单使用 -安装: pip3 install django==1.11.9 pycharm 下安装,选择版本号, -使用: 命令创建项目:django-admin startpro ...
- HDU1043 Eight
题目: 简单介绍一下八数码问题: 在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8 在上图中,由于右下角位置是空的 ...
- ES6 Promise(2)
Promise的兴起,是因为异步方法调用中,往往会出现回调函数一环扣一环的情况.这种情况导致了回调金字塔的出现.不仅代码写起来费劲不美观,而且问题复杂的时候,阅读代码的人也难以理解. db.save( ...
- 移动端弹性滑动以及vue记录滑动位置
-webkit-overflow-scrolling介绍 -webkit-overflow-scrolling: auto | touch; auto: 普通滚动,当手指从触摸屏上移开,滚动立即停止 ...
- 自动换行 word-break:break-all和word-wrap:break-word
1.word-break:break-all;当内容(比如很长的一个单词)到每行的末端时,它会把单词截断显示一部分,下一行显示后一部分. 2.word-wrap:break-word;当内容(比如很长 ...
- matplotlib之pyplot 学习示例
现在通过numpy和matplotlib.pyplot 在Python上实现科学计算和绘图,而且和matlab极为相像(效率差点,关键是方便简单) 这里有大量plots代码例子. 1. 简单的绘图( ...
- VC常用代码之创建进程
作者:朱金灿 来源:http://blog.csdn.net/clever101 创建进程是编程开发的常用操作.Windows中的创建进程采用API函数CreateProcess实现.下面是一个使用例 ...
- 通过getSystemServices获取手机管理大全
getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象.以下介绍系统相应的服务. 传入 ...