usaco6.4.2 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



二 分析

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

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


三 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: fence3
LANG: C++

Compiling...
Compile: OK

Executing...
   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 automated congratulations.


AC代码:
/*
ID:maxkibb3
LANG: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 & b
double 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;
}




                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值