[csu oj] 1548: Design road(三分)

来源:互联网 发布:阿里云备案解释 编辑:程序博客网 时间:2024/06/05 12:49

1548: Design road

点击打开题目链接

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 62  Solved: 32
[Submit][Status][Web Board]

Description

You need to design road from (0, 0) to (x, y) in plane with the lowest cost. Unfortunately, there are N Rivers between (0, 0) and (x, y).It costs c1 Yuan RMB per meter to build road, and it costs c2 Yuan RMB per meter to build a bridge. All rivers are parallel to the Y axis with infinite length.

Input

There are several test cases.
Each test case contains 5 positive integers N,x,y,c1,c2 in the first line.(N ≤ 1000,1 ≤ x,y≤ 100,000,1 ≤ c1,c2 ≤ 1000).
The following N lines, each line contains 2 positive integer xi, wi ( 1 ≤ i ≤ N ,1 ≤ xi ≤x, xi-1+wi-1 < xi , xN+wN ≤ x),indicate the i-th river(left bank) locate xi with wi width.
The input will finish with the end of file.

Output

For each the case, your program will output the least cost P on separate line, the P will be to two decimal places .

Sample Input

1 300 400 100 100100 501 150 90 250 52030 120

Sample Output

50000.0080100.00

HINT


题意:修路和桥从(0,0)到(x,y),n个数表示有第二行开始有n行表示有n条河,xi是河的起始位置,wi是河的宽度,,C1表示修路每米的花费,C2表示修桥每米的花费,问你最后花费的最少金额?

思路:先把和合并成一条河,然后就三分河岸的高度即可(不过不知道为啥,题目中明明说明了输入只有整数,但设成double才AC,可能是精度问题)

代码:

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#include <stack>#include <queue>#include <map>#include <set>#include <math.h>#define max(a,b) (a)>(b)?(a):(b)#define min(a,b) (a)>(b)?(b):(a)#define eps 1e-10#define inf 0x3f3f3f3fusing namespace std;double xx,x,y,c1,c2;double sum;double cal(double yo){    double p=sqrt(xx*xx+yo*yo),r=sqrt((x-xx)*(x-xx)+(y-yo)*(y-yo));    return p*c1+r*c2;}double solve(double MIN,double MAX){    double Left=MIN,Right=MAX;    double mid=(Right+Left)/2,mmid=(mid+Right)/2;    double Cmid=cal(mid),Cmmid=cal(mmid);    ///double mid,mmid,Cmid,Cmmid;    while(fabs(Cmid-Cmmid)>=eps)    {        if(Cmid >Cmmid)            Left=mid;        else            Right=mmid;        mid=(Right+Left)/2;        mmid=(Right+mid)/2;        Cmid=cal(mid),Cmmid=cal(mmid);    }    return min(Cmid,Cmmid);}int main(){    int n;    int i;    while(~scanf("%d%lf%lf%lf%lf",&n,&x,&y,&c1,&c2))    {        sum = 0;        for(i = 0;i<n;i++)        {            double tx,ty;            scanf("%lf%lf",&tx,&ty);            sum+=ty;        }        xx=x-sum;        double ans=solve(0,y);        printf("%.2f\n",ans);    }    return 0;}


0 0
原创粉丝点击