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条河流,河流平行y轴无限延伸坐标为x1,
宽为w,在陆地修路单位长度c1 rmb,在河上修桥 c2 rmb;
看完题之后就思考在第1条河哪个位置垂直过河,然后过河又要考虑是向着目标方向过河还是垂直方向过河,
过河之后又继续向目标前进,然后又要考虑上面思考的问题。就这样一直没想到如何解。
其实只要将所有河流宽度合并,变成一条河流,然后对河岸三分,找出建桥的点就行。
#include<bits/stdc++.h>
using namespace std;
double dis(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{
int n,x,y,c1,c2;
while(cin>>n>>x>>y>>c1>>c2)
{
int riverLen=0;
int a,b;
while(n--)
{
cin>>a>>b;
riverLen+=b;
}
double land=1.0*(x-riverLen);
double l=0,r=1.0*y;
while(r-l>1e-8){
double mid=1.0*(l+r)/2;
double mmid=1.0*(l+mid)/2;
double dm=dis(0,0,land,mid)*c1+dis(land,mid,1.0*x,1.0*y)*c2;
double dmm=dis(0,0,land,mmid)*c1+dis(land,mmid,1.0*x,1.0*y)*c2;
if(dm<dmm){
l=mmid;
}else{
r=mid;
}
}
printf("%.2lf\n",dis(0,0,land,l)*c1+dis(land,l,x,y)*c2);
}
}