模拟退火算法。。。。

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

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

  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <time.h>
  7.  
  8. using namespace std;
  9. const int MAXN=55;
  10. const double PI=3.141592653;
  11. const double eps=1e-5;
  12.  
  13. #define zero(a) fabs(a)<eps
  14.  
  15. struct point {
  16. double x,y;
  17. };
  18. struct Segment{
  19. point a,b;
  20. };
  21.  
  22. point p[MAXN]; int n; double ans; int cot;
  23. point tar[MAXN]; double best[MAXN]; double R;
  24.  
  25. point operator -(point &u,point &v){
  26. point re;
  27. re.x=u.x-v.x; re.y=u.y-v.y;
  28. return re;
  29. }
  30.  
  31. double dot(point &u,point &v){
  32. return u.x*v.x+u.y*v.y;
  33. }
  34.  
  35. double dist(point tt){
  36. return sqrt(tt.x*tt.x+tt.y*tt.y);
  37. }
  38. double multi(point &u,point &v){
  39. return u.x*v.y-u.y*v.x;
  40. }
  41. /*
  42. bool whether_in(point &t){
  43. double angle=0;
  44. for(int i=0;i<n;i++){
  45. point A=p[i]-t;
  46. point B=p[i+1]-t;
  47. angle+=acos(dot(A,B)/dist(A)/dist(B));
  48. }
  49. if(fabs(angle-PI*2)<1e-5)
  50. return true;
  51. return false;
  52. }
  53.  
  54. */
  55.  
  56. double xmul(point p0,point p1,point p2){
  57. return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
  58. }
  59.  
  60. bool online(point p1,point p2,point p){
  61. if(zero(xmul(p1,p2,p))&&((p.x-p1.x)*(p.x-p2.x)<eps&&(p.y-p1.y)*(p.y-p2.y)<eps))
  62. return true;
  63. return false;
  64. }
  65.  
  66. inline bool across(Segment s1,Segment s2){
  67. if(xmul(s1.a,s1.b,s2.a)*xmul(s1.a,s1.b,s2.b)<eps)
  68. if(xmul(s2.a,s2.b,s1.a)*xmul(s2.a,s2.b,s1.b)<eps)
  69. return true;
  70. return false;
  71. }
  72.  
  73. bool whether_in(point cen){
  74. int cnt=0;
  75. Segment s,e;
  76. s.a=cen;s.b.y=cen.y;s.b.x=20000.0;
  77. for(int i=0;i<n;i++){
  78. e.a=p[i];e.b=p[i+1];
  79. if(online(p[i],p[i+1],cen)) return false;
  80. if(zero(p[i].y-p[i+1].y)) continue;
  81. if(online(s.a,s.b,p[i])){
  82. if(p[i].y>p[i+1].y) cnt++;
  83. }
  84. else if(online(s.a,s.b,p[i+1])){
  85. if(p[i+1].y>p[i].y) cnt++;
  86. }
  87. else if(across(s,e))
  88. cnt++;
  89. }
  90. return cnt&1;
  91. }
  92.  
  93. double count_d(point &tt){
  94. double mm=1e10; double tp;
  95. for(int i=0;i<n;i++){
  96. point A=tt-p[i];
  97. point B=p[i+1]-p[i];
  98. if(dot(A,B)<=0){
  99. tp=dist(A);
  100. // cout<<"1"<<' '<<tp<<endl;
  101. mm=min(mm,tp);
  102. continue;
  103. }
  104. A=tt-p[i+1];
  105. B=p[i]-p[i+1];
  106. if(dot(A,B)<=0){
  107. tp=dist(A);
  108. mm=min(mm,tp);
  109. // cout<<"2"<<' '<<tp<<endl;
  110. continue;
  111. }
  112. double area=multi(A,B);
  113. tp=fabs(area)/dist(p[i]-p[i+1]);
  114. // cout<<"3"<<' '<<tp<<endl;
  115. mm=min(mm,tp);
  116. }
  117. return mm;
  118. }
  119.  
  120. double getdouble(){
  121. double re=((rand()*rand())%1000000)*1.0/1e6;
  122. return re;
  123. }
  124.  
  125. int main(){
  126. srand(time(0));
  127. while(scanf("%d",&n),n){
  128. cot=0;
  129. for(int i=0;i<n;i++)
  130. scanf("%lf%lf",&p[i].x,&p[i].y);
  131. scanf("%lf",&R);
  132. p[n]=p[0];
  133. point tmp;
  134. for(int i=0;i<n;i++){
  135. tmp.x=(p[i].x+p[i+1].x)/2;
  136. tmp.y=(p[i].y+p[i+1].y)/2;
  137. tar[cot++]=tmp;
  138. }
  139. // cout<<cot<<endl;
  140. bool flag=false;
  141. for(int i=0;i<cot;i++){
  142. best[i]=0;
  143. }
  144. // cout<<"YES"<<endl;
  145. double T=50;
  146. for(double t=T;t>1e-4;t*=0.55){
  147. for(int i=0;i<cot;i++){
  148. for(int j=0;j<10;j++){
  149. double td=getdouble();
  150. if(rand()&1) td*=-1;
  151. tmp.x=tar[i].x+td*t;
  152. td=getdouble();
  153. if(rand()&1) td*=-1;
  154. tmp.y=tar[i].y+td*t;
  155. if(whether_in(tmp)){
  156. td=count_d(tmp);
  157. if(td>best[i]){
  158. best[i]=td;
  159. tar[i]=tmp;
  160. }
  161. if(td>=R||fabs(td-R)<1e-4){
  162. // cout<<tmp.x<<' '<<tmp.y<<endl;
  163. // cout<<td<<endl;
  164. flag=true;
  165. break;
  166. }
  167. }
  168. }
  169. if(flag) break;
  170. }
  171. if(flag) break;
  172. }
  173. if(flag) printf("Yes\n");
  174. else printf("No\n");
  175. }
  176. return 0;
  177. }

  

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. poj3233Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23187   Accepted: ...

  2. 应用JavaScript搭建一个简易页面图片无缝滚动效果

    页面图片无缝滚动JavaScript原理:移动的区块包含图片内容,区块相对父级元素进行定位脱离文档流.再令区块的left值每隔固定的时间进行等量减少(或增大)从而实现区块的匀速运动.由于每次间隔移动的 ...

  3. BZOJ 1877 拆点费用流

    思路: 呃  水题不解释 行么,, //By SiriusRen #include <queue> #include <cstdio> #include <cstring ...

  4. usaco 过路费 Cow Toll Paths, 2009 Dec

    Description 翰家有 N 片草地,编号为 1 到 N ,彼此之间由 M 条双向道路连接,第 i 条道路连接了 Ai 和Bi,两片草地之间可能有多条道路,但没有道路会连接同一片草地,现有的道路 ...

  5. Android Fragment与Activity交互的几种方式

    这里我不再详细介绍那写比较常规的方式,例如静态变量,静态方法,持久化,application全局变量,收发广播等等. 首先我们来介绍使用Handler来实现Fragment与Activity 的交互. ...

  6. ListView使用、ListView优化和遇到的问题

    1.先写遇到的问题: a.ListView只显示一个item. listview只显示一个item,并且做了listview的点击事件监听打印 Bean 对象的属性和哈希值,发现只有显示的那个 Bea ...

  7. APP开发中的弹窗体系,UI设计师不能忽视的地方

    1. 弹窗的定义 弹窗分为模态弹窗和非模态弹窗两种. 弹窗分类 模态弹窗:很容易打断用户的操作行为,用户必须回应,否则不能进行其他操作. 非模态弹窗:不会影响用户的操作,用户可以不对其进行回应,非模态 ...

  8. C# 获取所有网卡信息

    private void Form1_Load(object sender, EventArgs e) { //获取说有网卡信息 NetworkInterface[] nics = NetworkIn ...

  9. Python3编写自动签到服务程序

    公司加班的餐补需要登录网站签到领取,有时候会忘记,于是自己用Python写了小程序来自动签到.刚开始只是做了自己用,直接写的黑框程序,后来给同事用,就打包成exe.再后来有人说要写成window服务会 ...

  10. dubbo之泛化实现

    实现泛化调用 泛化接口调用方式主要用于客户端没有 API 接口及模型类元的情况,参数及返回值中的所有 POJO 均用 Map 表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过 Gene ...