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.
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.
For each the case, your program will output the least cost P on separate line, the P will be to two decimal places .
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#define eps 1e-8
int n;
double x,y,c1,c2,W;
double ff(double ay) {
return sqrt((x-W)*(x-W)+ay*ay)*c1+sqrt((y-ay)*(y-ay)+W*W)*c2;
}
int main() {
//freopen("in.txt","r",stdin);
while(~scanf("%d%lf%lf%lf%lf",&n,&x,&y,&c1,&c2)) {
double ax,aw;
W=0;
for(int i=0; i<n; i++) {
scanf("%lf%lf",&ax,&aw);
W+=aw;
}
double left=0,right=y*1.0;
double mid1,mid2;
for(int i=0; i<100; i++) {
mid1=(right+left)/2;
mid2=(left+mid1)/2;
if(ff(mid1)<ff(mid2)) {
left=mid2;
} else {
right=mid1;
}
}
printf("%.2f\n",ff(left));
}
return 0;
}