您的位置:首页 > 其它

CSU_1548_DesignRoad

2015-07-31 16:56 337 查看


Design road

Time Limit: 2 Sec Memory Limit: 256 MB

Submit: 306 Solved: 153

[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


HINT


Source

首先这个题目说明了,所有的河都在要到达地点的前面,

而且与常识一致,河流与河流之间没有重叠。

那么其实要求最小的花费,由于在河里走的全部路径,以及在陆地上走的全部路径分别是平行的

因此可以把河流拼起来处理

三分求下表达式的最小值就可以了

切记这种最后涉及精度问题的,能早用高精度的类型就早用了……

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
using namespace std;
const double AC=1e-6;
const int M=1005;

double fu(double yw,double x,double y,double ri,double c1,double c2)//这里早用double早就过了
{
double ww=(double)c2*sqrt(ri*ri+yw*yw);
double wr=(double)c1*sqrt((y-yw)*(y-yw)+(x-ri)*(x-ri));
return ww+wr;
}

double ts(int x,int y,int ri,int c1,int c2)
{
double lo=0,hi=y;
double mid,midmid;
while(lo+AC<hi)
{
mid=(hi+lo)/2.0;
midmid=(mid+hi)/2.0;
//cout<<lo<<" "<<hi<<" "<<mid<<" "<<midmid<<" "<<fu(mid,x,y,ri,c1,c2)<<endl;
if(fu(mid,x,y,ri,c1,c2)>=fu(midmid,x,y,ri,c1,c2))
lo=mid;
else
hi=midmid;
}
return mid;
}

int main()
{
int n,x,y,c1,c2;
int river,rx,rw;
while(scanf("%d%d%d%d%d",&n,&x,&y,&c1,&c2)!=EOF)
{
river=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&rx,&rw);
river+=rw;
}
printf("%.2lf\n",fu(ts(x,y,river,c1,c2),x,y,river,c1,c2));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: