USACO 6.4 Electric Fences

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

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次
然后可以得到一个最优解,这个办法挺随机的
 /*
LANG: C++
PROG: fence3
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define siji(i,x,y) for(int i=(x); i<=(y) ; ++i)
#define ivorysi
#define o(x) ((x)*(x))
using namespace std;
typedef long long ll;
int f;
struct data{
int xs,ys,xt,yt;
}seg[];
void init() {
scanf("%d",&f);
siji(i,,f) {
scanf("%d%d%d%d",&seg[i].xs,&seg[i].ys,&seg[i].xt,&seg[i].yt);
if(seg[i].xt<seg[i].xs) swap(seg[i].xt,seg[i].xs);
if(seg[i].yt<seg[i].ys) swap(seg[i].yt,seg[i].ys);
}
}
double dist(double x,double y) {
double res=0.0;
siji(i,,f) {
double xid=min(fabs(seg[i].xt-x),fabs(seg[i].xs-x));
if(x>=seg[i].xs && x<=seg[i].xt) xid=;
double yid=min(fabs(seg[i].yt-y),fabs(seg[i].ys-y));
if(y>=seg[i].ys && y<=seg[i].yt) yid=;
res+=sqrt(o(xid)+o(yid));
}
return res;
}
void solve() {
init();
//if(n==7) {cout<<"12198297600"<<endl;return;}
double elecx=0.0,elecy=0.0;
double direx[]={1.0,0.0,-1.0,0.0},direy[]={0.0,1.0,0.0,-1.0};
double T=20.0;
double bestnum=dist(0.0,0.0);
siji(b,,) {
if(b%==) T*=0.1;
int best=-;
siji(i,,) {
elecx+=direx[i]*T;
elecy+=direy[i]*T;
double temp=dist(elecx,elecy);
if(temp<bestnum)
bestnum=temp,best=i;
elecx-=direx[i]*T;
elecy-=direy[i]*T;
}
if(best!=-) {
elecx+=direx[best]*T;
elecy+=direy[best]*T;
}
bestnum=dist(elecx,elecy);
}
printf("%.1lf %.1lf %.1lf\n",elecx,elecy,bestnum);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("fence3.in","r",stdin);
freopen("fence3.out","w",stdout);
#else
freopen("f1.in","r",stdin);
//freopen("f1.out","w",stdout);
#endif
solve();
return ;
}
 
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6