usaco 5.2 Electric Fences(아날로그 후퇴)
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:
SAMPLE OUTPUT (file fence3.out)
1.0 1.6 3.7
제목: n선 구간을 드릴게요. 이 점에서 n선 구간까지의 거리와 최단을 구하세요.
분석: 이 문제를 보자마자 나는 아날로그 퇴화로 할 생각을 했다. 선을 점으로 삼으면 마점을 구하는 문제가 되었다. 즉, 점에서 선까지의 거리의 함수를 하나만 쓰면 되고 나머지는 마점을 구하는 틀로 하면 된다.
그러나 이 문제는 선이 하나의 점인 상황을 주의하여 나로 하여금 한 번 괴롭혔다
코드:
/*
ID: 15114582
PROG: fence3
LANG: C++
*/
#include<cmath>
#include<cstdio>
#include<iostream>
using namespace std;
const int mm=222;
const double eps=1e-2;
typedef double diy;
struct point
{
diy x,y;
point(){}
point(diy _x,diy _y):x(_x),y(_y){}
}s[mm],t[mm];
point Vector(point s,point t)
{
return point(t.x-s.x,t.y-s.y);
}
diy CrossProduct(point P,point Q)
{
return P.x*Q.y-P.y*Q.x;
}
diy DotProduct(point P,point Q)
{
return P.x*Q.x+P.y*Q.y;
}
diy MultiCross(point P,point Q,point R)
{
return CrossProduct(Vector(Q,P),Vector(Q,R));
}
diy MultiDot(point P,point Q,point R)
{
return DotProduct(Vector(Q,P),Vector(Q,R));
}
diy Dis(point P,point Q)
{
return sqrt((P.x-Q.x)*(P.x-Q.x)+(P.y-Q.y)*(P.y-Q.y));
}
diy PointSegDis(point P,point Q,point R)
{
if(Dis(P,Q)<eps)return Dis(P,R);
if(MultiDot(Q,P,R)<-eps)return Dis(P,R);
if(MultiDot(P,Q,R)<-eps)return Dis(Q,R);
return fabs(MultiCross(P,Q,R)/Dis(P,Q));
}
diy AllDis(point o,int n)
{
diy ret=0;
while(n--)ret+=PointSegDis(s[n],t[n],o);
return ret;
}
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
int i,n;
int main()
{
freopen("fence3.in","r",stdin);
freopen("fence3.out","w",stdout);
point o,tmp;
diy T,delta,ans,ret;
bool flag;
while(~scanf("%d",&n))
{
for(i=0;i<n;++i)
scanf("%lf%lf%lf%lf",&s[i].x,&s[i].y,&t[i].x,&t[i].y);
o=s[0];
ans=AllDis(o,n);
flag=1;
for(T=100,delta=0.99;T>eps;T*=delta,flag=1)
while(flag)
for(flag=i=0;i<4;++i)
{
tmp.x=o.x+dx[i]*T;
tmp.y=o.y+dy[i]*T;
ret=AllDis(tmp,n);
if(ans>ret)
{
ans=ret;
o=tmp;
flag=1;
}
}
printf("%.1lf %.1lf %.1lf
",o.x,o.y,ans);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자료구조_벡터(vector) in C++num_arr1 : 가장 기본적인 형태의 vector 선언이다. num_arr2 : vector의 크기를 정함과 동시에 선언한다. num_arr3 : vector의 크기를 정하면서, 해당 vector의 값을 무엇으로...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.