Usaco6.4-electric Fences: Calculating geometry

Source: Internet
Author: User

Electric Fences
Kolstad & Schrijvers

Farmer John had decided to construct electric fences. He hasfenced His fields into a number of bizarre shapes and now must findthe optimal place to locate the electrical supply To each of thefences.

A single wire must run from some point to each and every fenceto the source of electricity. Wires can run through other fencesor across other Wires. Wires can run at any angle. Wires can runfrom 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 <=) fences (fencesare always parallel to a grid axis and run from one Integ Er gridpointto another, 0 <= x, y <=), your program must calculateboth the total length of wire required to Conne CT every fence tothe central source of electricity and also, the optimal location forthe electrical source.

The optimal location for the electrical source might is Anywherein Farmer John ' s field, not necessarily on a grid point.

Program Name:fence3input FORMAT

The first line contains F, the number of fences.
F Subsequentlines each contain the x, y pairs each of the which denotes the endpointsof a fence.

SAMPLE INPUT (file fence3.in)
30 0 0 12 0 2 10 3 2 3
OUTPUT FORMAT

On a, print three space-separated floating point numbers, each with a single decimal place. Presume that your computer's output library would round the number correctly.

The three numbers is:

    • The X value of the optimal location for the electricity,
    • The Y value for the optimal location for the electricity, and
    • The total (minimum) is the length of the wire required.
SAMPLE OUTPUT (file fence3.out)
1.0 1.6 3.7


Test instructions: gives a line of n lines parallel to the axis, requiring a point to minimize the sum of all segments.

because the precision requirements are very low, as long as the retention of a decimal, so can be random, I was in the whole range of violence to find a test instructions to meet the point, and then in the x±1,y±1 range of violence to find a decimal place, the worst situation needs violence 100*100*150, is completely can bear.

In the violent decimal place should pay attention to the accuracy problem, another three points search also can do.

Executing ...
Test 1:test OK [0.014 secs, 3380 KB]
Test 2:test OK [0.016 secs, 3380 KB]
Test 3:test OK [0.049 secs, 3380 KB]
Test 4:test OK [0.024 secs, 3380 KB]
Test 5:test OK [0.070 secs, 3380 KB]
Test 6:test OK [0.046 secs, 3380 KB]
Test 7:test OK [0.043 secs, 3380 KB]
Test 8:test OK [0.051 secs, 3380 KB]
Test 9:test OK [0.057 secs, 3380 KB]
Test 10:test OK [0.046 secs, 3380 KB]
Test 11:test OK [0.049 secs, 3380 KB]
Test 12:test OK [0.076 secs, 3380 KB]

All tests OK.

1 /*2 lang:c++3 Task:fence34 */5 6#include <iostream>7#include <cmath>8#include <stdio.h>9 using namespacestd;Ten #defineX First One #defineY Second A  -typedef pair<Double,Double>Point ; - intN; the  - structFence - { - Point p1; + Point p2; -}fences[ the]; +  A DoubleDist_point (Point p1,point p2) at { -     returnsqrt ((Double) (P1. x-p2.x) * (P1. x-p2.x) + (P1. Y-P2.Y) * (P1. Y-P2. Y)); - } -  - BOOLisIn (Fence f,point p) - { in     if(f.p1.x==f.p2.x)//Vertical Direction -         returnF.P1.Y&LT;=P.Y && p.y<=f.p2.y; to     if(F.P1.Y==F.P2.Y)//Horizontal Direction +         returnf.p1.x<=p.x && p.x<=f.p2.x; - } the  * //calculate the distance from P points to each fence $ DoubleDist (point P)Panax Notoginseng { -     Doubleans=0; the      for(intI=0; i<n;i++) +     { A         if(IsIn (fences[i],p)) the         { +             if(FENCES[I].P1. X==fences[i].p2. X//Vertical Direction -             { $Ans+=fabs ((Double) p.x-FENCES[I].P1. X); $             } -             Else -             { theAns+=fabs ((Double) p.y-FENCES[I].P1. Y); -             }Wuyi         } the         Else -         { Wuans+=min (Dist_point (P,FENCES[I].P1), Dist_point (P,FENCES[I].P2)); -         } About     } $     returnans; - } -  -  A intMain () + { theFreopen ("fence3.in","R", stdin); -Freopen ("Fence3.out","W", stdout); $  theCin>>N; the      for(intI=0; i<n;i++) the     { thescanf"%lf%lf%lf%lf", &fences[i].p1. X,&fences[i].p1. Y,&fences[i].p2. x,&FENCES[I].P2. Y); -     } in  the Point p; the     DoubleD=1e100; About  the      for(intI=0; i<= -; i++) the          for(intj=0; j<= -; j + +) the         { +             if(D>dist (Double) I, (Double) (j) )) -             { theP=point ((Double) I, (Double) j);BayiD=dist (Point (Double) I, (Double) (j)); the             } the         } -  -  thePoint p2=p; theP2. x-=1; theP2. y-=1; the Point ans; -      for(;p 2. x<=p.x+1;p 2. x+=0.1) the          for(P2. y=p.y-1;p 2. y<=p.y+1;p 2. y+=0.1) the         { the             if(D>=dist (p2) -1e- A)94             { theans=P2; theD=Dist (p2); the             }98         } About  -printf"%.1f%.1f%.1f\n", ans. X,ans. Y,D);101 102     return 0;103}



Usaco6.4-electric Fences: Calculating geometry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.