zoukankan      html  css  js  c++  java
  • USACO 5.2.2 fence3

    题目:

    http://ace.delos.com/usacoprob2?a=GPizy6nCKUy&S=fence3

    http://pingce.ayyz.cn:9000/usaco/data/20110129214306/fence3.html

    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

    题解:
      题意是在平面上找到一点使其到所给出的所有线段距离和最短,随机化算法即可,我写的模拟退火。注意由于最后对答案的精度要求不高,所以随机化算法的次数和步数都可以不太大,每次的步长也可以调大些。

    View Code
      1 /*
      2 ID:zhongha1
      3 PROB:fence3
      4 LANG:C++
      5 */
      6 
      7 #include<cstdio>
      8 #include<cstdlib>
      9 #include<cstring>
     10 #include<ctime>
     11 #include<cmath>
     12 #include<algorithm>
     13 
     14 using namespace std;
     15 
     16 const int TIME=1;
     17 const double max_t=10000.0;
     18 const double ratio=0.998;
     19 const double per_t=25;
     20 const double eps=1e-2;
     21 
     22 int n;
     23 
     24 double ans=1e+20,ansx,ansy,x11[201],x22[201],y11[201],y22[201];
     25 
     26 double calc(double x,double y)
     27 {
     28     double v=0;
     29     for (int a=1;a<=n;a++)
     30         if (fabs(x11[a]-x22[a])<=eps)
     31         {
     32             double miny=min(y11[a],y22[a]);
     33             double maxy=max(y11[a],y22[a]);
     34             if (y<miny) v+=sqrt((x-x11[a])*(x-x22[a])+(y-miny)*(y-miny));
     35             else
     36             {
     37                 if (y>maxy) v+=sqrt((x-x11[a])*(x-x22[a])+(y-maxy)*(y-maxy));
     38                 else v+=fabs(x-x11[a]);
     39             }
     40         }
     41         else
     42         {
     43             double minx=min(x11[a],x22[a]);
     44             double maxx=max(x11[a],x22[a]);
     45             if (x<minx) v+=sqrt((y-y11[a])*(y-y22[a])+(x-minx)*(x-minx));
     46             else
     47             {
     48                 if (x>maxx) v+=sqrt((y-y11[a])*(y-y22[a])+(x-maxx)*(x-maxx));
     49                 else v+=fabs(y-y11[a]);
     50             }
     51         }
     52     return v;
     53 }
     54 
     55 void solve()
     56 {
     57     for (int a=1;a<=TIME;a++)
     58     {
     59         double nowx=rand()%110;
     60         double nowy=rand()%110;
     61         double nowv=calc(nowx,nowy);
     62         for (double t=max_t;t>eps;t*=ratio)
     63         {
     64             for (int b=1;b<=per_t;b++)
     65             {
     66                 double deltax=(double)(rand()%100)/100.0;
     67                 double deltay=(double)(rand()%100)/100.0;
     68                 if (rand()%2==0) deltax=-deltax;
     69                 if (rand()%2==0) deltay=-deltay;
     70                 double newv=calc(nowx+deltax,nowy+deltay);
     71                 double delta=nowv-newv;
     72                 if (newv<nowv || (double)rand()/RAND_MAX<exp((double)delta))
     73                 {
     74                     nowv=newv;
     75                     nowx=nowx+deltax;
     76                     nowy=nowy+deltay;
     77                 }
     78                 if (nowv<ans)
     79                 {
     80                     ans=nowv;
     81                     ansx=nowx;
     82                     ansy=nowy;
     83                 }
     84             }
     85         }
     86     }
     87 }
     88 
     89 int main()
     90 {
     91     freopen("fence3.in","r",stdin);
     92     freopen("fence3.out","w",stdout);
     93 
     94     srand(time(0));
     95     scanf("%d",&n);
     96     for (int a=1;a<=n;a++)
     97         scanf("%lf%lf%lf%lf",&x11[a],&y11[a],&x22[a],&y22[a]);
     98     solve();
     99     printf("%.1lf %.1lf %.1lf\n",ansx,ansy,ans);
    100 
    101     return 0;
    102 }


  • 相关阅读:
    python3.6入门到高阶(全栈) day013-014 内置函数
    python3.6入门到高阶(全栈) day012 生成器
    PHP数组基本排序算法和查找算法
    02 LAMP环境下安装WordPress
    02 React Native入门——Hello World
    01 React Native入门——环境安装部署
    02 “响应式Web设计”——媒体查询
    01 “响应式Web设计”——概述
    01 Portal for ArcGIS 10.7安装部署教程(windows环境)
    06 spring boot入门——AJAX请求后台接口,实现前后端传值
  • 原文地址:https://www.cnblogs.com/zhonghaoxi/p/2571432.html
Copyright © 2011-2022 走看看