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

——————————————————————————————————————————题解
题解用了一个不是很像模拟退火的方法,因为模拟退火需要以一个随机的概率接受不优的解
用先跳20步,尝试跳10次,再缩减10倍,往复5次
然后可以得到一个最优解,这个办法挺随机的
 /*
LANG: C++
PROG: fence3
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define siji(i,x,y) for(int i=(x); i<=(y) ; ++i)
#define ivorysi
#define o(x) ((x)*(x))
using namespace std;
typedef long long ll;
int f;
struct data{
int xs,ys,xt,yt;
}seg[];
void init() {
scanf("%d",&f);
siji(i,,f) {
scanf("%d%d%d%d",&seg[i].xs,&seg[i].ys,&seg[i].xt,&seg[i].yt);
if(seg[i].xt<seg[i].xs) swap(seg[i].xt,seg[i].xs);
if(seg[i].yt<seg[i].ys) swap(seg[i].yt,seg[i].ys);
}
}
double dist(double x,double y) {
double res=0.0;
siji(i,,f) {
double xid=min(fabs(seg[i].xt-x),fabs(seg[i].xs-x));
if(x>=seg[i].xs && x<=seg[i].xt) xid=;
double yid=min(fabs(seg[i].yt-y),fabs(seg[i].ys-y));
if(y>=seg[i].ys && y<=seg[i].yt) yid=;
res+=sqrt(o(xid)+o(yid));
}
return res;
}
void solve() {
init();
//if(n==7) {cout<<"12198297600"<<endl;return;}
double elecx=0.0,elecy=0.0;
double direx[]={1.0,0.0,-1.0,0.0},direy[]={0.0,1.0,0.0,-1.0};
double T=20.0;
double bestnum=dist(0.0,0.0);
siji(b,,) {
if(b%==) T*=0.1;
int best=-;
siji(i,,) {
elecx+=direx[i]*T;
elecy+=direy[i]*T;
double temp=dist(elecx,elecy);
if(temp<bestnum)
bestnum=temp,best=i;
elecx-=direx[i]*T;
elecy-=direy[i]*T;
}
if(best!=-) {
elecx+=direx[best]*T;
elecy+=direy[best]*T;
}
bestnum=dist(elecx,elecy);
}
printf("%.1lf %.1lf %.1lf\n",elecx,elecy,bestnum);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("fence3.in","r",stdin);
freopen("fence3.out","w",stdout);
#else
freopen("f1.in","r",stdin);
//freopen("f1.out","w",stdout);
#endif
solve();
return ;
}
 

USACO 6.4 Electric Fences的更多相关文章

  1. 洛谷P2735 电网 Electric Fences

    P2735 电网 Electric Fences 11通过 28提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 在本题中,格点是 ...

  2. USACO 6.5 Closed Fences

    Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N c ...

  3. 洛谷 P2735 电网 Electric Fences Label:计算几何--皮克定理

    题目描述 在本题中,格点是指横纵坐标皆为整数的点. 为了圈养他的牛,农夫约翰(Farmer John)建造了一个三角形的电网.他从原点(0,0)牵出一根通电的电线,连接格点(n,m)(0<=n& ...

  4. USACO 3.4 Electric Fence

    Electric FenceDon Piele In this problem, `lattice points' in the plane are points with integer coord ...

  5. USACO 3.4 Electric Fence 皮克定理

    题意:在方格纸上画出一个三角形,求三角形里面包含的格点的数目 因为其中一条边就是X轴,一开始想的是算出两条边对应的数学函数,然后枚举x坐标值求解.但其实不用那么麻烦. 皮克定理:给定顶点坐标均是整点( ...

  6. LuoGu P2735 电网 Electric Fences

    题目传送门 这个东西,本来我是用求出两条一次函数解析式然后判断在x坐标下的y坐标值来做的 首先因为没考虑钝角三角形,WA了 然后又因为精度处理不好又WA了 一气之下,只能去网上查了查那个皮克定理 首先 ...

  7. luoguP2735 电网 Electric Fences

    一道校内模拟赛遇见的题 ** 不会正解就真的很麻烦的 数学题 ** 有一种东西叫 皮克定理 发现的千古神犇: 姓名:George Alexander Pick(所以叫皮克定理呀 国籍:奥地利(蛤!竟然 ...

  8. USACO 6.4 章节

    The Primes 题目大意 5*5矩阵,给定左上角 要所有行,列,从左向右看对角线为质数,没有前导零,且这些质数数位和相等(题目给和) 按字典序输出所有方案... 题解 看上去就是个 无脑暴搜 题 ...

  9. USACO6.4-Electric Fences:计算几何

    Electric Fences Kolstad & Schrijvers Farmer John has decided to construct electric fences. He ha ...

随机推荐

  1. Idea+TestNg配置test-output输出(转)

    说明:testNG的工程我是使用eclipse创建的,直接导入到idea中,运行test时不会生产test-output,只能在idea的控制台中查看运行结果,然后到处报告,经过不懈的百度终于找到怎么 ...

  2. MongoDB - Introduction to MongoDB, Databases and Collections

    MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases. Data ...

  3. spring——获取ClassLoader

    org.springframework.util包下的ClassUtils类有个静态方法:getDefaultClassLoader() 可以获取当前类加载器,如下: public static Cl ...

  4. 浅谈ASP.net中的DataSet对象

    在我们对数据库进行操作的时候,总是先把数据从数据库取出来,然后放到一个"容器"中,再通过这个"容器"取出数据显示在前台,而充当这种容器的角色中当属DataSet ...

  5. ASP.NET项目与IE10、IE11不兼容的解决办法

    1.解决办法 机器级别修复, 服务器所有ASP.NET程序受益 需要去微软下载对应asp.NET版本的修补程序 .NET 4 -http://support.microsoft.com/kb/2600 ...

  6. 谁说码农不懂浪漫?js写的'老婆生日快乐'特效

    一直被老婆抱怨不懂浪漫,老婆的生日又来了,老婆指着闺蜜空间上贴的老公做的胡萝卜心形浪漫晚餐告诉我:必须送她一份用心的礼物.我绞尽脑汁想出这么一法子,还是得用我们码农的独特方式,经过一天多的努力,终于做 ...

  7. [csp-201509-3]模板生成系统

    #include<bits/stdc++.h> using namespace std; ; string a[N],b[N],c[N]; int main() { //freopen(& ...

  8. # 20155222 2016-2017-2 《Java程序设计》第5周学习总结

    20155222 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 java中所有错误都会被包装为对象,如果你愿意,可以尝试(try)执行程序并捕捉代表错误的对 ...

  9. CALayer的上动画的暂停和恢复

    CHENYILONG Blog CALayer上动画的暂停和恢复 #pragma mark 暂停CALayer的动画-(void)pauseLayer:(CALayer*)layer{CFTimeIn ...

  10. 2019年湖南多校第一场||2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)

    第一场多校就打的这么惨,只能说自己太菜了,还需继续努力啊- 题目链接: GYM链接:https://codeforces.com/gym/101933 CSU链接:http://acm.csu.edu ...