usaco traini 5.2.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(1 <= F <= 150)段电网的位置(电网总是和坐标轴平行,并且端点的坐标总是整数,0 <= X,Y <= 100)。你的程序要计算连接电源和每段电网所需的电线的最小总长度,还有电源的最佳坐标。

电源的最佳坐标可能在农夫约翰的农田中的任何一个位置,并不一定是整数。

[编辑]格式

PROGRAM NAME: fence3

INPUT FORMAT

第一行包括 F ——电网的数量。 下面的 F 行每行包括两个 X,Y 对,表示这段电网的两个端点。

(ps:数据中有电网是点的情况,即 68 97 68 97,这貌似与题目叙述不符,请注意 //from Error)(路人甲:貌似照做就行了,不用管,。。。)

OUTPUT FORMAT

只有一行,输出三个浮点数,每个保留1位小数。假定你的电脑的输出库会正确地对小数进行四舍五入。

这三个数是:

电源最佳坐标的 X 值, 电源最佳坐标的 Y 值,和 需要的电线的总长度(要最小)。

[编辑]SAMPLE INPUT (file fence3.in)

3
0 0 0 1
2 0 2 1
0 3 2 3

[编辑]SAMPLE OUTPUT (file fence3.out)

1.0 1.6 3.7

【序言】这个题目真的是太神了!它适合任何非主流的算法(特别是随机化算法)。从此,我对模拟退火、爬山法等有了更深的了解。

【优化】因为只要保留一位小数,我们可以把它扩大10倍当整数计算,输出时再缩小即可。

【验证方法】假设我们已经知道了某个点X的坐标,现在要求电线的总长度。那么我们可以用O(N)的效率求出。每次枚举一条线段Y,如果X到Y可以有垂直线段,电线的长度就是这垂线段;否则枚举线段的两个端点,最短值肯定是其中之一。>>>>>>>>最关键的是,如何知道X的坐标呢?

【算法一】枚举。当然不是纯枚举,否则效率是1000*1000*120。比如,我们可以先求出这些点的土包,于是在土包外的点可以略去。但是这样要考虑的太多了,于是我没编。

【算法二】二分。我们每次随机一个y(假设这是正确的坐标值),根据这个y来二分坐标x。怎么二分呢?设目前x的范围是L..R,我先算一遍(L,y),再做一遍(R,Y),比较哪个的电线长度小。如果L小,我就把L和mid带进去继续迭代。等做完后,我们几乎可以认为当前得到的x是最优值。然后根据x的值重新二分枚举y。                                                         >>>>>>>当然这样会有点问题,我们多随机几次初始值即可。

【代码(会WA几个点,人品好可以A)】

/*
PROG:fence3
ID:juan1973
LANG:C++
*/
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<cmath>
#include<ctime>
#define INF 2100000000
using namespace std;
const int maxn=155;const int times=100;
int x3[maxn],x4[maxn],y3[maxn],y4[maxn],i,n,x,y,cnt;
double ans,ansx,ansy;
double dis(int x3,int y3,int x4,int y4) {return sqrt(double((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)));}
double check(int x,int y)
{
  double now=0,p1,p2;
  for (int i=1;i<=n;i++)
    if (x3[i]==x4[i])
    {
      if ((y>=y3[i]&&y<=y4[i])||(y>=y4[i]&&y<=y3[i])) now+=abs(x-x3[i]);
      else 
      {
        p1=dis(x3[i],y3[i],x,y);
        p2=dis(x4[i],y4[i],x,y);
        if (p1<p2) now+=p1;else now+=p2;
      } 
    }
    else
    {
      if ((x>=x3[i]&&x<=x4[i])||(x>=x4[i]&&x<=x3[i])) now+=abs(y-y3[i]);
      else 
      {
        p1=dis(x3[i],y3[i],x,y);
        p2=dis(x4[i],y4[i],x,y);
        if (p1<p2) now+=p1;else now+=p2;
      } 
    }
  if (now<ans) {ans=now;ansx=x;ansy=y;}
  return now;
}
int erfen_x(int l,int r)
{
  if (r-l<=10)
  {
    double min=INF;int p;
    for (int i=l;i<=r;i++)
    {
      int temp=check(i,y);
      if (temp<min) {min=temp;p=i;}
    }
    return p;
  }
  int mid=(l+r)/2;
  double ll=check(l,y);double rr=check(r,y);
  if (ll<rr) return erfen_x(l,mid);
  return erfen_x(mid+1,r);
}
int erfen_y(int l,int r)
{
  if (r-l<=10)
  {
    double min=INF;int p;
    for (int i=l;i<=r;i++)
    {
      int temp=check(x,i);
      if (temp<min) {min=temp;p=i;}
    }
    return p;
  }
  int mid=(l+r)/2;
  double ll=check(x,l);double rr=check(x,r);
  if (ll<rr) return erfen_y(l,mid);
  return erfen_y(mid+1,r);
}
int main()
{
  freopen("fence3.in","r",stdin);
  freopen("fence3.out","w",stdout);
  scanf("%ld",&n);
  for (i=1;i<=n;i++)
  {
    scanf("%ld%ld%ld%ld",&x3[i],&y3[i],&x4[i],&y4[i]);
    x3[i]*=10;x4[i]*=10;y3[i]*=10;y4[i]*=10;
  }
  srand((int)time(0));
  ans=INF;
  while (cnt<times)
  {
    cnt++;
    y=rand()%1000;
    x=erfen_x(0,1000);
    y=erfen_y(0,1000);
  }
  ans/=10;
  ansx=ansx/10;ansy=ansy/10;
  printf("%.1f %.1f %.1f\n",ansx,ansy,ans);
  return 0;
}

【算法三】为了提高正确率,我还尝试了类似于模拟退火的贪心法。迭代时每次带入一个矩阵,然后把它化成相同(或是相差最小)的四个小矩阵。随机枚举每个矩阵中的一些坐标并计算,同时找出最优解所在的矩阵并继续迭代。在时间允许的情况下,每次随机的点要尽量的多(我设了1000次)。

【代码(次次AC)】

/*
PROG:fence3
ID:juan1973
LANG:C++
*/
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<cmath>
#include<ctime>
#define INF 2100000000
using namespace std;
const int maxn=155;const int times=1000;
int x3[maxn],x4[maxn],y3[maxn],y4[maxn],i,n,x,y,cnt;
double ans,ansx,ansy;
double dis(int x3,int y3,int x4,int y4) {return sqrt(double((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)));}
int mm(int a,int b){return a>b?a:b;}
double check(int x,int y)
{
  double now=0,p1,p2;
  for (int i=1;i<=n;i++)
    if (x3[i]==x4[i])
    {
      if ((y>=y3[i]&&y<=y4[i])||(y>=y4[i]&&y<=y3[i])) now+=abs(x-x3[i]);
      else 
      {
        p1=dis(x3[i],y3[i],x,y);
        p2=dis(x4[i],y4[i],x,y);
        if (p1<p2) now+=p1;else now+=p2;
      } 
    }
    else
    {
      if ((x>=x3[i]&&x<=x4[i])||(x>=x4[i]&&x<=x3[i])) now+=abs(y-y3[i]);
      else 
      {
        p1=dis(x3[i],y3[i],x,y);
        p2=dis(x4[i],y4[i],x,y);
        if (p1<p2) now+=p1;else now+=p2;
      } 
    }
  return now;
}
void went(int left_x,int left_y,int right_x,int right_y)
{  
  if (left_x==right_x&&left_y==right_y) 
  {
    ansx=left_x;ansy=left_y;ans=check(ansx,ansy);return;
  }
  int mid_x=(left_x+right_x)/2;
  int mid_y=(left_y+right_y)/2;
  int x,y,z,k;double max=INF,temp;
  for (z=1;z<=times;z++) {x=left_x+rand()%(mid_x-left_x+1);y=left_y+rand()%(mid_y-left_y+1);temp=check(x,y);if (temp<max) {max=temp;k=0;}}
  for (z=1;z<=times;z++) {x=mid_x+1+rand()%(mm(right_x-mid_x,1));y=left_y+rand()%(mid_y-left_y+1);temp=check(x,y);if (temp<max) {max=temp;k=1;}}
  for (z=1;z<=times;z++) {x=left_x+rand()%(mid_x-left_x+1);y=mid_y+1+rand()%(mm(right_y-mid_y,1));temp=check(x,y);if (temp<max) {max=temp;k=2;}}
  for (z=1;z<=times;z++) {x=mid_x+1+rand()%(mm(right_x-mid_x,1));y=mid_y+1+rand()%(mm(right_y-mid_y,1));temp=check(x,y);if (temp<max) {max=temp;k=3;}}
  switch (k)
  {
    case 0:{went(left_x,left_y,mid_x,mid_y);break;}
    case 1:{went(mid_x+1,left_y,right_x,mid_y);break;}
    case 2:{went(left_x,mid_y+1,mid_x,right_y);break;}
    case 3:{went(mid_x+1,mid_y+1,right_x,right_y);break;}
  }
}
  
int main()
{
  freopen("fence3.in","r",stdin);
  freopen("fence3.out","w",stdout);
  scanf("%ld",&n);
  srand((int)time(0));
  for (i=1;i<=n;i++)
  {
    scanf("%ld%ld%ld%ld",&x3[i],&y3[i],&x4[i],&y4[i]);
    x3[i]*=10;x4[i]*=10;y3[i]*=10;y4[i]*=10;
  }
  went(0,0,1000,1000);
  printf("%.1f %.1f %.1f\n",ansx/10,ansy/10,ans/10);
  return 0;
}
【注意点】在相减的时候可能会出现0,所以我开了个函数来保护。(红色处)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值