usaco6.4.2 Electric Fences

来源:互联网 发布:多功能水枪 数据 编辑:程序博客网 时间:2024/06/07 14:39

一 原题

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



二 分析

题意是在直角坐标系中给定F条线段,每条线段的端点都在[0, 100]之间,要求找到一个点,使得这个点到所有线段的距离之和最小。要求点的坐标精确到小数点后一位。

思路是先用较大的步幅找到结果的大致位置,逐步缩小步幅,最终确定答案。详见代码。


三 代码

运行结果:
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.


AC代码:
/*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;}



原创粉丝点击