zoukankan      html  css  js  c++  java
  • USACO 6.4 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

    ——————————————————————————————————————————题解
    题解用了一个不是很像模拟退火的方法,因为模拟退火需要以一个随机的概率接受不优的解
    用先跳20步,尝试跳10次,再缩减10倍,往复5次
    然后可以得到一个最优解,这个办法挺随机的
     1 /*
     2 LANG: C++
     3 PROG: fence3
     4 */
     5 #include <iostream>
     6 #include <cstdio>
     7 #include <algorithm>
     8 #include <cstring>
     9 #include <cmath>
    10 #define siji(i,x,y) for(int i=(x); i<=(y) ; ++i)
    11 #define ivorysi
    12 #define o(x) ((x)*(x))
    13 using namespace std;
    14 typedef long long ll;
    15 int f;
    16 struct data{
    17     int xs,ys,xt,yt;
    18 }seg[155];
    19 void init() {
    20     scanf("%d",&f);
    21     siji(i,1,f) {
    22         scanf("%d%d%d%d",&seg[i].xs,&seg[i].ys,&seg[i].xt,&seg[i].yt);
    23         if(seg[i].xt<seg[i].xs) swap(seg[i].xt,seg[i].xs);
    24         if(seg[i].yt<seg[i].ys) swap(seg[i].yt,seg[i].ys);
    25     }
    26 }
    27 double dist(double x,double y) {
    28     double res=0.0;
    29     siji(i,1,f) {
    30         double xid=min(fabs(seg[i].xt-x),fabs(seg[i].xs-x));
    31         if(x>=seg[i].xs && x<=seg[i].xt) xid=0;
    32         double yid=min(fabs(seg[i].yt-y),fabs(seg[i].ys-y));
    33         if(y>=seg[i].ys && y<=seg[i].yt) yid=0;
    34         res+=sqrt(o(xid)+o(yid));
    35     }
    36     return res;
    37 }
    38 void solve() {
    39     init();
    40     //if(n==7) {cout<<"12198297600"<<endl;return;}
    41     double elecx=0.0,elecy=0.0;
    42     double direx[4]={1.0,0.0,-1.0,0.0},direy[4]={0.0,1.0,0.0,-1.0};
    43     double T=20.0;
    44     double bestnum=dist(0.0,0.0);
    45     siji(b,1,50) {
    46         if(b%10==0) T*=0.1;
    47         int best=-1;
    48         siji(i,0,3) {
    49             elecx+=direx[i]*T;
    50             elecy+=direy[i]*T;
    51             double temp=dist(elecx,elecy);
    52             if(temp<bestnum) 
    53                 bestnum=temp,best=i;
    54             elecx-=direx[i]*T;
    55             elecy-=direy[i]*T;
    56         }
    57         if(best!=-1) {
    58             elecx+=direx[best]*T;
    59             elecy+=direy[best]*T;
    60         }
    61         bestnum=dist(elecx,elecy);
    62     }
    63     printf("%.1lf %.1lf %.1lf
    ",elecx,elecy,bestnum);
    64 }
    65 int main(int argc, char const *argv[])
    66 {
    67 #ifdef ivorysi
    68     freopen("fence3.in","r",stdin);
    69     freopen("fence3.out","w",stdout);
    70 #else
    71     freopen("f1.in","r",stdin);
    72     //freopen("f1.out","w",stdout);
    73 #endif
    74     solve();
    75     return 0;
    76 }
     
  • 相关阅读:
    《测试工作量的时间评估》方案梳理
    GitHub 生成密钥
    Jenkins+Jmeter持续集成(五、Ant+GitLab持续构建)
    Linux下查看文件和文件夹大小
    Java Runtime.exec()的使用
    如何启动/停止/重启MySQL
    浅析Java语言慢的原因
    chattr命令锁定账户敏感文件
    SOAP协议初级指南 (三)
    SOAP协议初级指南 (二)
  • 原文地址:https://www.cnblogs.com/ivorysi/p/6939552.html
Copyright © 2011-2022 走看看