USACO 6.4 Electric Fences
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的更多相关文章
- 洛谷P2735 电网 Electric Fences
P2735 电网 Electric Fences 11通过 28提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 在本题中,格点是 ...
- 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 ...
- 洛谷 P2735 电网 Electric Fences Label:计算几何--皮克定理
题目描述 在本题中,格点是指横纵坐标皆为整数的点. 为了圈养他的牛,农夫约翰(Farmer John)建造了一个三角形的电网.他从原点(0,0)牵出一根通电的电线,连接格点(n,m)(0<=n& ...
- USACO 3.4 Electric Fence
Electric FenceDon Piele In this problem, `lattice points' in the plane are points with integer coord ...
- USACO 3.4 Electric Fence 皮克定理
题意:在方格纸上画出一个三角形,求三角形里面包含的格点的数目 因为其中一条边就是X轴,一开始想的是算出两条边对应的数学函数,然后枚举x坐标值求解.但其实不用那么麻烦. 皮克定理:给定顶点坐标均是整点( ...
- LuoGu P2735 电网 Electric Fences
题目传送门 这个东西,本来我是用求出两条一次函数解析式然后判断在x坐标下的y坐标值来做的 首先因为没考虑钝角三角形,WA了 然后又因为精度处理不好又WA了 一气之下,只能去网上查了查那个皮克定理 首先 ...
- luoguP2735 电网 Electric Fences
一道校内模拟赛遇见的题 ** 不会正解就真的很麻烦的 数学题 ** 有一种东西叫 皮克定理 发现的千古神犇: 姓名:George Alexander Pick(所以叫皮克定理呀 国籍:奥地利(蛤!竟然 ...
- USACO 6.4 章节
The Primes 题目大意 5*5矩阵,给定左上角 要所有行,列,从左向右看对角线为质数,没有前导零,且这些质数数位和相等(题目给和) 按字典序输出所有方案... 题解 看上去就是个 无脑暴搜 题 ...
- USACO6.4-Electric Fences:计算几何
Electric Fences Kolstad & Schrijvers Farmer John has decided to construct electric fences. He ha ...
随机推荐
- 洛谷P2766 最长递增子序列问题
https://www.luogu.org/problemnew/show/P2766 注:题目描述有误,本题求的是最长不下降子序列 方案无限多时输出 n 网络流求方案数,长见识了 第一问: DP 同 ...
- 值得关注的sql-on-hadoop框架
http://www.infoq.com/cn/news/2014/06/sql-on-hadoop 数据的操作语言是SQL,因此很多工具的开发目标自然就是能够在Hadoop上使用SQL.这些工具有些 ...
- [转载]学习C语言基本思路与参考书籍
http://zhuanlan.zhihu.com/linjr/19694823 计算机行业发展非常快,大学里的教育基本都跟不上实际的社会需求.如果你所在的学校还在指定大家使用谭浩强的教材,或使用VC ...
- 实现asp.net的文件压缩、解压、下载
很早前就想做文件的解压.压缩.下载 了,不过一直没时间,现在项目做完了,今天弄了下.不过解压,压缩的方法还是看的网上的,嘻嘻~~不过我把它们综合了一下哦.呵呵~~ 1.先要从网上下载一个icsharp ...
- 当python模式遇见cedet
TAG: emacs, python, cedet, semantic, ctags DATE: 2013-08-20 我用Emacs 24写python程序. 发现屏幕不时有些闪动,MiniBuff ...
- codeforces9D How many trees?
传送门:http://codeforces.com/problemset/problem/9/D [题解] 树形dp,f(i,j)表示i个节点,高度为j的方案数,枚举左子树大小和哪一个子树高度为j-1 ...
- 【leetcode 简单】 第一百一十二题 重复的子字符串
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: True 解释 ...
- tmux终端工具
本文原始地址:http://www.cnblogs.com/chinas/p/7094172.html,转载请注明出处,谢谢!!! 1.介绍 tmux(终端复用工具):一个很有趣的工具,类似GNU S ...
- 34、Collections工具类简介
Collections工具类简介 就像数组中的Arrays工具类一样,在集合里面也有跟Arrays类似的工具类Collections package com.sutaoyu.Collections; ...
- asp.net 调用post方法并获取返回值
/// <summary> /// http协议 post数据 接受返回结果 /// </summary> /// <param ...