[hdu3644 A Chocolate Manufacturer's Problem]模拟退火,简单多边形内最大圆
题意:判断简单多边形内是否可以放一个半径为R的圆
思路:如果这个多边形是正多边形,令r(x,y)为圆心在(x,y)处多边形内最大圆的半径,不难发现,f(x,y)越靠近正多边形的中心,r越大,所以可以利用模拟退火法来逼近最优点。对于一般的多边形,由于可能存在多个这样的"局部最优点",所以可以选不同的点作为起点进行多若干次模拟退火即可。
模拟退火的过程:每次由原状态S生成一个新状态T,如果T比S优,那么接受这一次转移,否则以一定概率P接受这次转移,因为这样可能会跳过局部最优解而得到全局最优解。
PS:步长每次改变的系数一般设为0.8~0.9,eps不能设太高。
#pragma comment(linker, "/STACK:10240000")
#include <bits/stdc++.h>
using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii; namespace Debug {
void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<" ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
}
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
/* -------------------------------------------------------------------------------- */ const double eps = 1e-4;/** 设置比较精度 **/
struct Real {
double x;
double get() { return x; }
int read() { return scanf("%lf", &x); }
Real(const double &x) { this->x = x; }
Real() {}
Real abs() { return x > ? x : -x; } Real operator + (const Real &that) const { return Real(x + that.x);}
Real operator - (const Real &that) const { return Real(x - that.x);}
Real operator * (const Real &that) const { return Real(x * that.x);}
Real operator / (const Real &that) const { return Real(x / that.x);}
Real operator - () const { return Real(-x); } Real operator += (const Real &that) { return Real(x += that.x); }
Real operator -= (const Real &that) { return Real(x -= that.x); }
Real operator *= (const Real &that) { return Real(x *= that.x); }
Real operator /= (const Real &that) { return Real(x /= that.x); } bool operator < (const Real &that) const { return x - that.x <= -eps; }
bool operator > (const Real &that) const { return x - that.x >= eps; }
bool operator == (const Real &that) const { return x - that.x > -eps && x - that.x < eps; }
bool operator <= (const Real &that) const { return x - that.x < eps; }
bool operator >= (const Real &that) const { return x - that.x > -eps; } friend ostream& operator << (ostream &out, const Real &val) {
out << val.x;
return out;
}
friend istream& operator >> (istream &in, Real &val) {
in >> val.x;
return in;
}
}; struct Point {
Real x, y;
int read() { return scanf("%lf%lf", &x.x, &y.x); }
Point(const Real &x, const Real &y) { this->x = x; this->y = y; }
Point() {}
Point operator + (const Point &that) const { return Point(this->x + that.x, this->y + that.y); }
Point operator - (const Point &that) const { return Point(this->x - that.x, this->y - that.y); }
Real operator * (const Point &that) const { return x * that.x + y * that.y; }
Point operator * (const Real &that) const { return Point(x * that, y * that); }
Point operator += (const Point &that) { return Point(this->x += that.x, this->y += that.y); }
Point operator -= (const Point &that) { return Point(this->x -= that.x, this->y -= that.y); }
Point operator *= (const Real &that) { return Point(x *= that, y *= that); } bool operator == (const Point &that) const { return x == that.x && y == that.y; } Real cross(const Point &that) const { return x * that.y - y * that.x; }
Real dist() { return sqrt((x * x + y * y).get()); }
};
typedef Point Vector; struct Segment {
Point a, b;
Segment(const Point &a, const Point &b) { this->a = a; this->b = b; }
Segment() {}
bool intersect(const Segment &that) const {
Point c = that.a, d = that.b;
Vector ab = b - a, cd = d - c, ac = c - a, ad = d - a, ca = a - c, cb = b - c;
return ab.cross(ac) * ab.cross(ad) < && cd.cross(ca) * cd.cross(cb) < ;
}
Point getLineIntersection(const Segment &that) const {
Vector u = a - that.a, v = b - a, w = that.b - that.a;
Real t = w.cross(u) / v.cross(w);
return a + v * t;
}
Real Distance(Point P) {
Point A = a, B = b;
if (A == B) return (P - A).dist();
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if (v1 * v2 < ) return v2.dist();
if (v1 * v3 > ) return v3.dist();
return v1.cross(v2).abs() / v1.dist();
}
}; const int maxn = ;
double PI = acos(-1.0); Point p[maxn];
int n; Real getAngel(Point o, Point a, Point b) {
a -= o;
b -= o;
Real ans = acos((a * b / a.dist() / b.dist()).get());
return a.cross(b) <= ? ans : -ans;
} bool inPolygon(Point o) {
Real total = ;
for (int i = ; i < n; i ++) {
total += getAngel(o, p[i], p[(i + ) % n]);
}
return total.abs() > PI;
} Real getR(Point o) {
Real ans = 1e9;
for (int i = ; i < n; i ++) {
Segment seg(p[i], p[(i + ) % n]);
umin(ans, seg.Distance(o));
}
return ans;
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
while (cin >> n, n) {
p[].read();
Real maxx = p[].x, minx = p[].x, maxy = p[].y, miny = p[].y;
for (int i = ; i < n; i ++) {
p[i].read();
umax(maxx, p[i].x);
umin(minx, p[i].x);
umax(maxy, p[i].y);
umin(miny, p[i].y);
}
Real R;
R.read();
Point a(minx, miny), b(maxx, maxy);
bool ok = false;
for (int i = ; !ok && i < n; i ++) {
Real deta = (b - a).dist() / ;
Point O = (p[i] + p[(i + ) % n]) * 0.5;
int cnt = ;
while (!ok && deta > && cnt < ) {
for (int j = ; ; j ++) {
double randnum = rand();
Point newp(O.x + deta * sin(randnum), O.y + deta * cos(randnum));
if (!inPolygon(newp)) continue;
Real buf = getR(newp);
if (buf > getR(O) || j > ) { /** 这里考虑了概率因素 **/
if (buf >= R) ok = true;
O = newp;
break;
}
}
deta *= 0.8;
cnt ++;
}
}
puts(ok? "Yes" : "No");
} }
[hdu3644 A Chocolate Manufacturer's Problem]模拟退火,简单多边形内最大圆的更多相关文章
- HDU - 3644:A Chocolate Manufacturer's Problem(模拟退火, 求多边形内最大圆半径)
pro:给定一个N边形,然后给半径为R的圆,问是否可以放进去. 问题转化为多边形的最大内接圆半径.(N<50): sol:乍一看,不就是二分+半平面交验证是否有核的板子题吗. 然而事情并没有那 ...
- Codeforces Beta Round #2 C. Commentator problem 模拟退火
C. Commentator problem 题目连接: http://www.codeforces.com/contest/2/problem/C Description The Olympic G ...
- How Cocoa Beans Grow And Are Harvested Into Chocolate
What is Cocoa Beans Do you like chocolate? Most people do. The smooth, brown candy is deliciously sw ...
- bzoj2965
http://www.lydsy.com/JudgeOnline/problem.php?id=2965 http://www.tsinsen.com/A1385 平面图网络流. 首先我们要将平面图转 ...
- bzoj4948: World Final2017 A
求简单多边形内的最长线段长度 显然存在一组最优解,使其所在直线经过多边形的两个端点,枚举这两个端点,求出直线和多边形的有效交点,从而得出直线有哪些部分在多边形内(含边界). 由于多边形的一些边可能与直 ...
- 21天学习caffe(一)
ubuntu环境安装caffe1 安装依赖 apt-get install libatlas-base-dev apt-get install python-dev apt-get install l ...
- 【智能算法】用模拟退火(SA, Simulated Annealing)算法解决旅行商问题 (TSP, Traveling Salesman Problem)
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 文章声明 此文章部分资料和代码整合自网上,来源太多已经无法查明出处,如侵犯您的权利,请联系我删除. 01 什么是旅行商问题(TS ...
- Codeforces Problem 598E - Chocolate Bar
Chocolate Bar 题意: 有一个n*m(1<= n,m<=30)的矩形巧克力,每次能横向或者是纵向切,且每次切的花费为所切边长的平方,问你最后得到k个单位巧克力( k <= ...
- 【模拟退火】Petrozavodsk Winter Training Camp 2017 Day 1: Jagiellonian U Contest, Monday, January 30, 2017 Problem F. Factory
让你在平面上取一个点,使得其到给定的所有点的距离和最小. 就是“费马点”. 模拟退火……日后学习一下,这是从网上扒的,先存下. #include<iostream> #include< ...
随机推荐
- scheduler_default_filters 详解
Filter scheduler 是 nova-scheduler 默认的调度器,调度过程分为两步: 通过过滤器(filter)选择满足条件的计算节点(运行 nova-compute) 通过权 ...
- Python软件定时器APScheduler使用【软件定时器,非操作系统定时器,软件可控的定时器】【用途:定时同步数据库和缓存等】【刘新宇】
APScheduler使用 APScheduler (advanceded python scheduler)是一款Python开发的定时任务工具. 文档地址 https://apscheduler. ...
- 基于layui,Jquery 表格动态编辑 设置 编辑值为 int 或者 double 类型及默认值
首先先推荐大家在看这篇笔记时,阅读过我写的这篇 Layui表格编辑[不依赖Layui的动态table加载] 阅读过上面那篇笔记之后呢,才能更好的理解我现在所要说的这个东西 接下来废话不多说,上代码. ...
- 23-Java-Spring框架(一)
一.Spring框架了解 Spring框架是一个开源的框架,为JavaEE应用提供多方面的解决方案,用于简化企业级应用的开发,相当于是一种容器,可以集成其他框架(结构图如下). 上图反映了框架引包的依 ...
- 二叉树中两节点的最近公共父节点(360的c++一面问题)
面试官的问题:写一个函数 TreeNode* Find(TreeNode* root, TreeNode* p, TreeNode* q) ,返回二叉树中p和q的最近公共父节点. 本人反应:当时有点 ...
- WFS: postgresql(postgis)和shp文件查询效率对比
对GeoServer上的WFS的各种数据源查询效率感兴趣,做个测试.本次测试了Postgresql.geopackage.shp文件三种数据源的查询效率,无论是本机还是服务器环境,pg存储查询效率都比 ...
- 2019-2020-1 20199310《Linux内核原理与分析》第二周作业
1.问题描述 众所周知,计算机是20世纪最伟大的发明之一,计算机是如何工作的呢?本文主要通过计算机的组成结构和工作原理,以及汇编代码工作过程来进行详细叙述. 2.解决过程 2.1 冯·诺依曼体系结构 ...
- 2019-2020-1 20199326《Linux内核原理与分析》第七周作业
实验内容:分析Linux内核创建一个新进程的过程 初始化Menu Os,输入fork可以看到menuos触发了一个fork系统调用 再开一个shell,进入调试模式,设置几个断点sys_clone,d ...
- python25之进制转换
一.进制转换函数 bin():将十进制转换为二进制 oct():将十进制转换为八进制 hex():将十进制转换为十六进制 >>> x=1234>>> bin(x)' ...
- java中Future的使用
文章目录 创建Future 从Future获取结果 取消Future 多线程环境中运行 java中Future的使用 Future是java 1.5引入的一个interface,可以方便的用于异步结果 ...