CSU - 1548 Design road

Design road

Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu

Submit Status

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 100
100 50
1 150 90 250 520
30 120

Sample Output

50000.00
80100.00

题意:
修路和桥,找到花费最小的路径

思路:
三分法搜索

注意:
精度要求

ans1-ans2<0.00000001

若是改成0.001,则WA。

# include<cstdio>
# include<iostream>
# include<math.h>
using namespace std;

double ans(double a,double b)
{
    return sqrt(a*a+b*b);
}
int main()
{
    int N;
    double x,y,c1,c2;
    double x1,x2,y1,y2,pp,ppp;
    int i;

    while(cin>>N>>x>>y>>c1>>c2)
    {

        x1=x2=y1=y2=0;
        for(i=0;i<N;i++)
        {

        cin>>pp>>ppp;
            x2+=ppp;
        }
        x1=x-x2;
        double y3,y1,y2,y21,ans1=0,ans2=0;
        y3=y;y2=y/2;y1=0;y21=(y1+y2)/2;
        while(true)
        {
            ans1=c1*ans(x1,y2)+c2*ans(x2,y-y2);
            ans2=c1*ans(x1,y21)+c2*ans(x2,y-y21);
            if(ans1-ans2<0.00000001 && ans1-ans2>-0.00000001  )
            break;
            if( ans1>ans2 )
            {
                y3=y2;
            }
            if(ans2>ans1 )
            {
                y1=y21;
            }
            //else if(ans2==ans1)
            {
            //  y3+=0.5;
            }
            y2=(y1+y3)/2;
            y21=(y1+y2)/2;

        }
        printf("%.2f\n",ans2>ans1?ans1:ans2);
    }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值