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 ...
随机推荐
- poj3233Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23187 Accepted: ...
- 应用JavaScript搭建一个简易页面图片无缝滚动效果
页面图片无缝滚动JavaScript原理:移动的区块包含图片内容,区块相对父级元素进行定位脱离文档流.再令区块的left值每隔固定的时间进行等量减少(或增大)从而实现区块的匀速运动.由于每次间隔移动的 ...
- BZOJ 1877 拆点费用流
思路: 呃 水题不解释 行么,, //By SiriusRen #include <queue> #include <cstdio> #include <cstring ...
- usaco 过路费 Cow Toll Paths, 2009 Dec
Description 翰家有 N 片草地,编号为 1 到 N ,彼此之间由 M 条双向道路连接,第 i 条道路连接了 Ai 和Bi,两片草地之间可能有多条道路,但没有道路会连接同一片草地,现有的道路 ...
- Android Fragment与Activity交互的几种方式
这里我不再详细介绍那写比较常规的方式,例如静态变量,静态方法,持久化,application全局变量,收发广播等等. 首先我们来介绍使用Handler来实现Fragment与Activity 的交互. ...
- ListView使用、ListView优化和遇到的问题
1.先写遇到的问题: a.ListView只显示一个item. listview只显示一个item,并且做了listview的点击事件监听打印 Bean 对象的属性和哈希值,发现只有显示的那个 Bea ...
- APP开发中的弹窗体系,UI设计师不能忽视的地方
1. 弹窗的定义 弹窗分为模态弹窗和非模态弹窗两种. 弹窗分类 模态弹窗:很容易打断用户的操作行为,用户必须回应,否则不能进行其他操作. 非模态弹窗:不会影响用户的操作,用户可以不对其进行回应,非模态 ...
- C# 获取所有网卡信息
private void Form1_Load(object sender, EventArgs e) { //获取说有网卡信息 NetworkInterface[] nics = NetworkIn ...
- Python3编写自动签到服务程序
公司加班的餐补需要登录网站签到领取,有时候会忘记,于是自己用Python写了小程序来自动签到.刚开始只是做了自己用,直接写的黑框程序,后来给同事用,就打包成exe.再后来有人说要写成window服务会 ...
- dubbo之泛化实现
实现泛化调用 泛化接口调用方式主要用于客户端没有 API 接口及模型类元的情况,参数及返回值中的所有 POJO 均用 Map 表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过 Gene ...