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);
}
}