usaco 5.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

题意:给你n条线段,要你求出一个点,使得这个点到n条线段的距离的和最短

分析:一看到这题我就想到用模拟退火来做,把线段当成点的话,就成了求费马点的问题,也就是只要写一个点到线段的距离的函数就行,剩下的就是套用求费马点的模板即可

不过这题注意会出现线段是一个点的情况,害我wa了一次

代码:

/*
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\n",o.x,o.y,ans);
    }
    return 0;
}


你可能感兴趣的:(vector,Integer,each,output,Shapes,Numbers)