题目:

http://ace.delos.com/usacoprob2?a=GPizy6nCKUy&S=fence3

http://pingce.ayyz.cn:9000/usaco/data/20110129214306/fence3.html

Electric Fences
Kolstad & Schrijvers

Farmer John has decided to construct electric fences. He has fenced his fields into a number of bizarre shapes and now must find the optimal place to locate the electrical supply to each of the fences.

A single wire must run from some point on each and every fence to the source of electricity. Wires can run through other fences or across other wires. Wires can run at any angle. Wires can run from any point on a fence (i.e., the ends or anywhere in between) to the electrical supply.

Given the locations of all F (1 <= F <= 150) fences (fences are always parallel to a grid axis and run from one integer gridpoint to another, 0 <= X,Y <= 100), your program must calculate both the total length of wire required to connect every fence to the central source of electricity and also the optimal location for the electrical source.

The optimal location for the electrical source might be anywhere in Farmer John's field, not necessarily on a grid point.

PROGRAM NAME: fence3

INPUT FORMAT

The first line contains F, the number of fences.
F subsequent lines each contain two X,Y pairs each of which denotes the endpoints of a fence.

SAMPLE INPUT (file fence3.in)

3
0 0 0 1
2 0 2 1
0 3 2 3

OUTPUT FORMAT

On a single line, print three space-separated floating point numbers, each with a single decimal place. Presume that your computer's output library will round the number correctly.

The three numbers are:

  • the X value of the optimal location for the electricity,
  • the Y value for the optimal location for the electricity, and
  • the total (minimum) length of the wire required.

SAMPLE OUTPUT (file fence3.out)

1.0 1.6 3.7

题解:  题意是在平面上找到一点使其到所给出的所有线段距离和最短,随机化算法即可,我写的模拟退火。注意由于最后对答案的精度要求不高,所以随机化算法的次数和步数都可以不太大,每次的步长也可以调大些。
View Code

  1 /*
  2 ID:zhongha1
  3 PROB:fence3
  4 LANG:C++
  5 */
  6
  7 #include<cstdio>
  8 #include<cstdlib>
  9 #include<cstring>
 10 #include<ctime>
 11 #include<cmath>
 12 #include<algorithm>
 13
 14 using namespace std;
 15
 16 const int TIME=1;
 17 const double max_t=10000.0;
 18 const double ratio=0.998;
 19 const double per_t=25;
 20 const double eps=1e-2;
 21
 22 int n;
 23
 24 double ans=1e+20,ansx,ansy,x11[201],x22[201],y11[201],y22[201];
 25
 26 double calc(double x,double y)
 27 {
 28     double v=0;
 29     for (int a=1;a<=n;a++)
 30         if (fabs(x11[a]-x22[a])<=eps)
 31         {
 32             double miny=min(y11[a],y22[a]);
 33             double maxy=max(y11[a],y22[a]);
 34             if (y<miny) v+=sqrt((x-x11[a])*(x-x22[a])+(y-miny)*(y-miny));
 35             else
 36             {
 37                 if (y>maxy) v+=sqrt((x-x11[a])*(x-x22[a])+(y-maxy)*(y-maxy));
 38                 else v+=fabs(x-x11[a]);
 39             }
 40         }
 41         else
 42         {
 43             double minx=min(x11[a],x22[a]);
 44             double maxx=max(x11[a],x22[a]);
 45             if (x<minx) v+=sqrt((y-y11[a])*(y-y22[a])+(x-minx)*(x-minx));
 46             else
 47             {
 48                 if (x>maxx) v+=sqrt((y-y11[a])*(y-y22[a])+(x-maxx)*(x-maxx));
 49                 else v+=fabs(y-y11[a]);
 50             }
 51         }
 52     return v;
 53 }
 54
 55 void solve()
 56 {
 57     for (int a=1;a<=TIME;a++)
 58     {
 59         double nowx=rand()%110;
 60         double nowy=rand()%110;
 61         double nowv=calc(nowx,nowy);
 62         for (double t=max_t;t>eps;t*=ratio)
 63         {
 64             for (int b=1;b<=per_t;b++)
 65             {
 66                 double deltax=(double)(rand()%100)/100.0;
 67                 double deltay=(double)(rand()%100)/100.0;
 68                 if (rand()%2==0) deltax=-deltax;
 69                 if (rand()%2==0) deltay=-deltay;
 70                 double newv=calc(nowx+deltax,nowy+deltay);
 71                 double delta=nowv-newv;
 72                 if (newv<nowv || (double)rand()/RAND_MAX<exp((double)delta))
 73                 {
 74                     nowv=newv;
 75                     nowx=nowx+deltax;
 76                     nowy=nowy+deltay;
 77                 }
 78                 if (nowv<ans)
 79                 {
 80                     ans=nowv;
 81                     ansx=nowx;
 82                     ansy=nowy;
 83                 }
 84             }
 85         }
 86     }
 87 }
 88
 89 int main()
 90 {
 91     freopen("fence3.in","r",stdin);
 92     freopen("fence3.out","w",stdout);
 93
 94     srand(time(0));
 95     scanf("%d",&n);
 96     for (int a=1;a<=n;a++)
 97         scanf("%lf%lf%lf%lf",&x11[a],&y11[a],&x22[a],&y22[a]);
 98     solve();
 99     printf("%.1lf %.1lf %.1lf\n",ansx,ansy,ans);
100
101     return 0;
102 }


转载于:https://www.cnblogs.com/zhonghaoxi/archive/2012/06/30/2571432.html

USACO 5.2.2 fence3相关推荐

  1. usaco Shaping Regions

    这就是usaco 前面的windows area的变形. /* ID:jinbo wu TASK:rect1 LANG:C++ */ #include<iostream> #include ...

  2. usaco Postal Vans(dp)

    是哈密顿回路,然后...就不知道怎么写了 ,以前写过类似的不过情况没这么多也没这么复 usaco training 6.1.1 Postal Vans 题解 标签: usaco training题解d ...

  3. usaco Beef McNuggets

    这两天贼烦,ccf炸了,还有一个烦心事.哎我都不知道自己能不能坚持下去了.马上期末考了.这段时间还是抓紧时间复习吧同时刷usaco的节奏要跟以前一样了,毕竟课少了. 题解: 只要你知道以下的数论结论, ...

  4. usaco前两章小结

    usaco 暑假老师有推荐做但是那个题目太长了,而且·大部分都是废话做起来特别慢,而且当时自己基本上什么都不懂,太难了所以看了题解做了两题就放弃了. 转眼就上学了,因为想学习acm所以就胡乱找题做但是 ...

  5. usaco ★Fractions to Decimals 分数化小数

    ★Fractions to Decimals 分数化小数 写一个程序,输入一个形如 N/D 的分数(N 是分子,D 是分母),输出它的小数形式. 如果小数有循环节的话,把循环节放在一对圆括号中.例如, ...

  6. usaco ★Bessie Come Home 回家

    ★Bessie Come Home 回家 现在是晚餐时间,而母牛们在外面分散的牧场中. 农民约翰按响了电铃,所以她们开始向谷仓走去. 你的工作是要指出哪只母牛会最先到达谷仓(在给出的测试数据中,总会有 ...

  7. usaco Sorting a Three-Valued Sequence 三值的排序

    一开始想贪心,但是一想这是搜索章节应该用bfs?(为什么这么想看我上一个usaco的题目),,,,,,,,,,结果我贪心做出来了.然后我百度一下了别人做的好像么没人用bfs我不知道可不可以做出来. 思 ...

  8. usaco Arithmetic Progressions(看了题解)

    usaco也开始限时了,这题是搜索加剪枝.剪枝很关键.(哎........怎么才能不看题解解题啊) /* ID: jinbo wu LANG: C++ TASK: ariprog */ #includ ...

  9. 【USACO training】Chapter 1 入门

    整理的算法模板合集: ACM模板 目录 Section 1.1 介绍 Section 1.2 提交解决方案,任务类型,特殊问题 1.2.1 AcWing 1339. 你的旅途由此开始(字符串模拟) 1 ...

  10. USACO 做题小结

    还记得之前,发过一篇阶段性总结与未来规划..结果由于最近rp爆发(保研成功+进wf)后者显然靠bin神,前者也是运气.因此,放松了一段时间.然后就开始刷usaco了,原因是不用花时间找解题报告在NOC ...

最新文章

  1. linux下源码安装vim,ubuntu 源码编译安装最新的vim 8.0
  2. Eclipse创建JavaWeb工程
  3. element ui 二级菜单_基于avue和element-ui集成解决方案avue-cli
  4. Matlab Compiler路径
  5. Android优秀开源项目大全
  6. JavaEE实战班第16天
  7. 用Ghost进行备份还原
  8. flex结合asp.net上传深入详细解说(转载)
  9. Springboot2.0从零开始搭建脚手架-初始化和整合MybatisPlus3.0+...
  10. 第 72 章 FAQ
  11. jquery版瀑布流
  12. java同步方法同步块_java 同步代码块与同步方法
  13. java rxtx 串口_Java使用RXTX进行串口SerialPort通讯
  14. 京东联盟高级API-批量创建多个推广位
  15. 计算机主机号截图,电脑如何截图?截图三种方法推荐
  16. 学生用计算机怎么玩俄罗斯方块,计算机专业项目二 俄罗斯方块
  17. 自己的第一个windows程序
  18. Teams下载安装教程
  19. Python破解12306图片验证码
  20. ExaGrid在2021年网络计算大奖评选中大获全胜

热门文章

  1. [CLS]预训练语言模型的前世今生[SEP]萌芽时代[SEP]
  2. 学术 | 如何写一篇合格的NLP论文
  3. 互联网核心应用(搜索/推荐/广告)算法峰会
  4. 每日算法系列【LeetCode 376】摆动序列
  5. pandas中read_csv的缺失值处理
  6. 距离Java开发者玩转 Serverless,到底还有多远?
  7. “三低”用户养活的互联网
  8. 6.2GPT意境级讲解
  9. NASNET-【论文理解】
  10. 力扣-1. 两数之和