一 原题
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)
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: 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.
/*
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;
}