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. Java基础-IO流对象之File类

    Java基础-IO流对象之File类 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.IO技术概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下 ...

  2. python---基础知识回顾(十)进程和线程(py2中自定义线程池和py3中的线程池使用)

    一:自定义线程池的实现 前戏: 在进行自定义线程池前,先了解下Queue队列 队列中可以存放基础数据类型,也可以存放类,对象等特殊数据类型 from queue import Queue class ...

  3. Vue入坑教程(二)——项目结构详情介绍

    之前已经介绍了关于Vue的脚手架vue-cli的安装,以及一些文件目录介绍.具体可以查看<vue 入坑教程(一)--搭建vue-cli脚手架> 下面简单说一下具体的文件介绍 (一) pac ...

  4. Django 2.0.1 官方文档翻译: 编写你的第一个 Django app,第七部分(Page 12)

    编写你的第一个 Django app,第七部分(Page 12)转载请注明链接地址 本节教程承接第六部分(page 11)的教程.我们继续开发 web-poll应用,并专注于自定义django的自动生 ...

  5. 把一个IEEE754浮点数转换为IBM370浮点数的C#代码

    把一个IEEE754浮点数转换为IBM370浮点数的C#代码. 在这个网页上有古老的IBM370浮点格式的说明. // http://en.wikipedia.org/wiki/IBM_Floatin ...

  6. spring-mvc Mybatis插件打印SQL

    代码: package com.chainup.exchange.service.adapter; import com.chainup.exchange.service.impl.AccountSe ...

  7. TED_Topic6:How to raise a black son in America

    By Clint Smith As kids, we all get advice from parents and teachers that seems strange, even confusi ...

  8. c++刷题(24/100)正则匹配与位运算

    题目1:正则表达式匹配 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字 ...

  9. 【译】第十四篇 Integration Services:项目转换

    本篇文章是Integration Services系列的第十四篇,详细内容请参考原文. 简介在前一篇,我们查看了SSIS变量,变量配置和表达式管理动态值.在这一篇,我们使用SQL Server数据商业 ...

  10. 64_t7

    texlive-ulqda-bin-svn13663.0-33.20160520.fc26.2..> 24-May-2017 15:57 33102 texlive-ulqda-doc-svn2 ...