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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define INIFINITY 10000000
#define eps 0.000001
typedef struct Point
{
double x;
double y;
}Point;
typedef struct Edge
{
Point a;
Point b;
}Edge;
int N;
Edge Fence[150];
Point P;
double Ans=INIFINITY;
double Dist(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double DistEP(Edge S,Point X)
{
if(S.a.x==S.b.x){
if(S.b.y<X.y)
return Dist(S.b,X);
else if(S.a.y>X.y)
return Dist(S.a,X);
else
return fabs(X.x-S.a.x);
}
else{
if(S.b.x<X.x)
return Dist(S.b,X);
else if(S.a.x>X.x)
return Dist(S.a,X);
else
return fabs(X.y-S.a.y);
}
}
void DFS(double x,double y,double cent)
{
int i;
double TmpAns=0;
Point Tp;
Tp.x=x;
Tp.y=y;
for(i=0;i<N;i++)
TmpAns+=DistEP(Fence[i],Tp);
if(TmpAns<Ans){
Ans=TmpAns;
P.x=Tp.x;
P.y=Tp.y;
DFS(x+cent,y+cent,cent);
DFS(x+cent,y-cent,cent);
DFS(x-cent,y-cent,cent);
DFS(x-cent,y+cent,cent);
DFS(x+cent,y,cent);
DFS(x,y+cent,cent);
DFS(x-cent,y,cent);
DFS(x,y-cent,cent);
}
}
int main()
{
int i;
double temp;
FILE *fin,*fout;
fin=fopen("fence3.in","r");
fout=fopen("fence3.out","w");
fscanf(fin,"%d",&N);
for(i=0;i<N;i++){
fscanf(fin,"%lf %lf %lf %lf",&Fence[i].a.x,&Fence[i].a.y,&Fence[i].b.x,&Fence[i].b.y);
if(Fence[i].a.x==Fence[i].b.x){
if(Fence[i].a.y>Fence[i].b.y){
temp=Fence[i].b.y;
Fence[i].b.y=Fence[i].a.y;
Fence[i].a.y=temp;
}
}
if(Fence[i].a.y==Fence[i].b.y){
if(Fence[i].a.x>Fence[i].b.x){
temp=Fence[i].b.x;
Fence[i].b.x=Fence[i].a.x;
Fence[i].a.x=temp;
}
}
}
DFS(0,0,1);
Ans+=0.1;
DFS(P.x,P.y,0.1);
fprintf(fout,"%.1lf %.1lf %.1lf\n",P.x,P.y,Ans);
return 0;
}
发现了计算机的强大。。逐渐找点逼近搜索答案。
感觉还是水题= =
本文深入探讨了农民约翰如何利用算法优化电篱笆的电力供应,通过计算每条篱笆到电力源的最佳连接路径长度,并确定最优电力源位置。详细分析了输入格式、算法实现及输出结果,展示了计算机科学在实际应用中的强大能力。

1445

被折叠的 条评论
为什么被折叠?



