csu 1548(三分)

1548: Design road

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 383  Solved: 200
[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 100
100 50
1 150 90 250 520
30 120

Sample Output

50000.00
80100.00


题意:有一个人要从(0,0)->(x,y),中间有n条河,每条河都有一个宽度和一个起始位置,并且上一条河与下一条河无交点,修一公里路花费 c1 ,一公里河花费 c2,问人从起点到终点的最小费用?
题解:我们将所有的和平移到最右边,那么这里就只要枚举河岸就OK了,单峰极值,三分求解。(0,0)->(X,t)为河,(X,t)->(x,y)为路.花费为 sqrt(X*X+t*t)*c2+sqrt((x-X)*(x-X)+(y-t)*(y-t))*c1
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int n;
double x,y,c1,c2,sum,X;
const double eps = 1e-8;
double Calc(double t)
{
    return sqrt(X*X+t*t)*c2+sqrt((x-X)*(x-X)+(y-t)*(y-t))*c1;
}
double solve(double MIN,double MAX)
{
    double Left, Right;
    double mid, midmid;
    double mid_value, midmid_value;
    Left = MIN;
    Right = MAX;
    while (Left +eps < Right)
    {
        mid = (Left + Right) / 2;
        midmid = (mid + Right) / 2;
        mid_value = Calc(mid);
        midmid_value = Calc(midmid);
        if (mid_value <= midmid_value) Right = midmid;
        else Left = mid;
    }
    return Left;
}
int main()
{
    while(scanf("%d%lf%lf%lf%lf",&n,&x,&y,&c1,&c2)!=EOF)
    {
        X = 0;
        for(int i=1; i<=n; i++)
        {
            double a,b;
            scanf("%lf%lf",&a,&b);
            X+=b;
        }
        double k = solve(0,y);
        printf("%.2lf\n",Calc(k));
    }
}

 

posted @ 2016-08-21 10:28  樱花庄的龙之介大人  阅读(164)  评论(0编辑  收藏  举报