usaco6.4.2 Electric Fences
来源:互联网 发布:多功能水枪 数据 编辑:程序博客网 时间:2024/06/07 14:39
一 原题
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
SAMPLE INPUT (file fence3.in)
30 0 0 12 0 2 10 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
二 分析
三 代码
USER: Qi Shen [maxkibb3]TASK: fence3LANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.000 secs, 4328 KB] Test 2: TEST OK [0.000 secs, 4328 KB] Test 3: TEST OK [0.000 secs, 4328 KB] Test 4: TEST OK [0.000 secs, 4328 KB] Test 5: TEST OK [0.014 secs, 4328 KB] Test 6: TEST OK [0.014 secs, 4328 KB] Test 7: TEST OK [0.014 secs, 4328 KB] Test 8: TEST OK [0.014 secs, 4328 KB] Test 9: TEST OK [0.014 secs, 4328 KB] Test 10: TEST OK [0.014 secs, 4328 KB] Test 11: TEST OK [0.028 secs, 4328 KB] Test 12: TEST OK [0.028 secs, 4328 KB]All tests OK.YOUR PROGRAM ('fence3') WORKED FIRST TIME! That's fantastic-- and a rare thing. Please accept these special automatedcongratulations.
/*ID:maxkibb3LANG:C++PROB:fence3*/#include<iostream>#include<fstream>#include<vector>#include<cmath>#include<iomanip>using namespace std;struct Point { double x, y; Point() {} Point(double xx, double yy) : x(xx), y(yy) {}};const double rate = 0.98;const double eps = 1e-8;const double inf = 1e99;ifstream fin("fence3.in");ofstream fout("fence3.out");int n;vector<Point> s, t;Point o;double ans = inf, tem = 100;int dx[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};double cross(Point a, Point b, Point c) { return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);}double multi(Point a, Point b, Point c) { return (b.x - a.x) * (c.x - a.x) + (b.y - a.y) * (c.y - a.y);}double p2pDis(Point a, Point b) { return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y));}// line is determined by Point a & bdouble p2lDis(Point a, Point b, Point c) { if(p2pDis(a, b) < eps) return p2pDis(a, c); if(multi(a, b, c) < -eps) return p2pDis(a, c); if(multi(b, a, c) < -eps) return p2pDis(b, c); return fabs(cross(a, b, c) / p2pDis(a,b));}double getDis(Point x) { double ret = 0; for(int i = 0; i < n; i++) ret += p2lDis(s[i], t[i], x); return ret;}void search() { o = Point(s[0].x, s[0].y); ans = getDis(o); while(tem > eps) { bool flag = true; while(flag) { flag = false; for(int i = 0; i < 4; i++) { Point p; p.x = o.x + dx[i][0] * tem; p.y = o.y + dx[i][1] * tem; double dis = getDis(p); if(dis < ans) { ans = dis; o = p; flag = true; } } } tem = tem * rate; }}int main() { fin >> n; s.resize(n); t.resize(n); for(int i = 0; i < n; i++) { fin >> s[i].x >> s[i].y >> t[i].x >> t[i].y; } fin.close(); search(); fout << fixed << setprecision(1); fout << o.x << " " << o.y << " " << ans << endl; fout.close(); return 0;}
- usaco6.4.2 Electric Fences
- USACO6.4.2 Electric Fences(fence3)
- Electric Fences
- Section 5.2 Electric Fences
- USACO6.5.2 Closed Fences(fence4)
- usaco 5.2.2 Electric Fences
- USACO Electric Fences 解题报告
- 洛谷 P2735 电网 Electric Fences
- usaco 5.2 Electric Fences(模拟退火)
- USACOTraining 3.4.3 Electric Fences电网 题解
- usaco traini 5.2.2 Electric Fences 题解
- USACO 6.4.2 Electric Fences 数学+暴力
- USACO Section 5.2 Electric Fences - 有意思的枚举+计算几何
- USACO 3.4 Electric Fences电网 (数学.皮克定理)
- Fences
- usaco3.4.2 Electric Fence
- [USACO3.4.2]Electric Fence
- USACO3.4.2 Electric Fence (fence9)
- Scrapy安装报error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools
- 【观察】从智能鸿沟背后的挑战 看百分点的探索与创新
- sdut 1211
- ubuntu 下使用 upnp 配置路由器端口映射
- org.apache.catalina.core.StandardWrapperValve invoke的解决办法
- usaco6.4.2 Electric Fences
- 德语词汇笔记(一)
- 微服务注册与发现及如何使用Eureka
- [LeetCode]712. Minimum ASCII Delete Sum for Two Strings
- 机器学习实战篇-人脸识别(1)- 人脸定位
- ICMP协议
- nagios插件之检测tomcat日志的startAt值(待改进)
- 趣图:程序员调 Bug 的 5 个阶段
- 数据库的完整性