[ACM] CSU 1548 Design Road (three points)

Source: Internet
Author: User

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548

First contact three points, test instructions and code reference on the Web.

Test instructions: Road: From (0,0) ~ (x, y),n number indicates that there are n rows at the beginning of the second line to indicate n River,TX is the starting position of the river,Ty Is the width of the river, where there is water to repair the bridge, and x,y means the end of the road,C1 means the cost of roads per meter,C2 said the cost of repairing the bridge per meter, ask you to spend the least amount of the last!

Idea: first put the place with the river together, then its width will know, then the height of three points, find the least cost point

The three points here are the minimum values.

Introduction to the three-part method

in the interval with two mid will divide the interval into three parts, such a search algorithm called three-point lookup , that is, three-part method, the three-part method is often used to solve the maximum value of a single-peak function.  

There is also an understanding, that is, on the basis of the binary search, in the left or right interval, and then another two points.

The difference between three points and two points

The dichotomy method is suitable for monotone functions, and the single-peak function with two points is obviously not very good, for some of the mono-peak function, can be converted to a monotone function, thus using two points, but many cases of derivation is very troublesome, then need to use three points.

const int Inf=0x3f3f3f3f;int n;double x,y,c1,c2,sum,ans;const double eps=1e-8;double dis (double x1,double y1,double x2, Double y2) {    return hypot (x1-x2,y1-y2);} void Fen3 (double l,double R)//minimum value {    double mid1=l+ (r-l)/3;    Double mid2=l+ (r-l) *2/3;    Double S1=dis (0,0,SUM,MID1) *c2+dis (sum,mid1,x,y) *c1;    Double S2=dis (0,0,SUM,MID2) *c2+dis (sum,mid2,x,y) *c1;    if (Fabs (S1-S2) <eps)    {        ans=s2;        return;    }    if (S1>S2)        Fen3 (mid1,r);    else        Fen3 (L,MID2);} int main () {while    (scanf ("%d%lf%lf%lf%lf", &N,&X,&Y,&C1,&C2)!=eof)    {        sum=0;        for (int i=1;i<=n;i++)        {            double tx,ty;            scanf ("%lf%lf", &tx,&ty);            sum+=ty;        }        Fen3 (0,y);        printf ("%.2f\n", ans);    }    return 0;}


[ACM] CSU 1548 Design Road (three points)

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.