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\n",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 }
 
时间: 2024-12-08 05:23:30

USACO 6.4 Electric Fences的相关文章

USACO 6.5 Closed Fences

Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N corners (3 < N < 200). The corners or vertices are each distinct and are listed in counter-clockwise order in an array {xi, yi}, i in (1..N). Every pa

洛谷 P2735 电网 Electric Fences Label:计算几何--皮克定理

题目描述 在本题中,格点是指横纵坐标皆为整数的点. 为了圈养他的牛,农夫约翰(Farmer John)建造了一个三角形的电网.他从原点(0,0)牵出一根通电的电线,连接格点(n,m)(0<=n<32000,0<m<32000),再连接格点(p,0)(p>0),最后回到原点. 牛可以在不碰到电网的情况下被放到电网内部的每一个格点上(十分瘦的牛).如果一个格点碰到了电网,牛绝对不可以被放到该格点之上(或许Farmer John会有一些收获).那么有多少头牛可以被放到农夫约翰的电网

P2735 电网 Electric Fences

题目描述 在本题中,格点是指横纵坐标皆为整数的点. 为了圈养他的牛,农夫约翰(Farmer John)建造了一个三角形的电网.他从原点(0,0)牵出一根通电的电线,连接格点(n,m)(0<=n<32000,0<m<32000),再连接格点(p,0)(p>0),最后回到原点. 牛可以在不碰到电网的情况下被放到电网内部的每一个格点上(十分瘦的牛).如果一个格点碰到了电网,牛绝对不可以被放到该格点之上(或许Farmer John会有一些收获).那么有多少头牛可以被放到农夫约翰的电网

USACO 3.4 Electric Fence 皮克定理

题意:在方格纸上画出一个三角形,求三角形里面包含的格点的数目 因为其中一条边就是X轴,一开始想的是算出两条边对应的数学函数,然后枚举x坐标值求解.但其实不用那么麻烦. 皮克定理:给定顶点坐标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积A和内部格点数目i.边上格点数目b的关系:A = i + b/2 - 1. 有了这条定理就好办了. 三角形面积直接用公式就能算出来. 对于从点(0,0)到点(x,y)的线段,该线段上的格点数目即gcd(x,y)+1 这样A和b都有了,套公式就行了.

【Electric Fences】电力篱笆

[Problem description] 农夫约翰已经决定建造电网.他已经把他的农田围成一些奇怪的形状,现在必须找出安放电源的最佳位置. 对于段电网都必须从电源拉出一条电线.电线可以穿过其他电网或者跨过其他电线.电线能够以任意角度铺设,从电源连接到一段电网的任意一点上(也就是,这段电网的端点上或者在其之间的任意一点上).这里所说的"一段电网"指的是呈一条线段状的电网,并不是连在一起的几段电网.若几段电网连在一起,那么也要分别给这些电网提供电力. 已知所有的 F(1 <= F &

USACO6.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 wi

USACO 3.3 Riding the Fences

Riding the Fences Farmer John owns a large number of fences that must be repaired annually. He traverses the fences by riding a horse along each and every one of them (and nowhere else) and fixing the broken parts. Farmer John is as lazy as the next

usaco Electric Fence

这种有小数的题目总会令我格外头疼. /* ID: modengd1 PROG: fence9 LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> #include <math.h> using namespace std; long long ans,x1,x2; int n,m,p; int main() { freopen("fence9.in"

POJ 2018 Best Cow Fences

斜率优化DP...<浅谈数形结合思想在信息学竞赛中的应用 安徽省芜湖一中 周源>例题... Best Cow Fences Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9311   Accepted: 2986 Description Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field c