CSU 1548 Road And Bridge acm 三分

来源:互联网 发布:自然知彼官方旗舰店 编辑:程序博客网 时间:2024/06/15 13:27

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 100100 501 150 90 250 52030 120

Sample Output

50000.0080100.00


首先将所有的河流都合并在一边,然后在陆地与边上任找一点,计算相应的花费,用三分的方法得到最小的花费

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
double n, x, y, c1, c2, p[1010][2], sum, m;
double dis(double x1, double y1,double x2,double y2){
 double mm = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
 return mm;
}
double cal(double mm){
 double nn = c2*dis(0, 0, sum, mm)+c1*dis(sum,mm,x,y);
 return nn;
}
int main(){
 while (cin >> n >> x >> y >> c1 >> c2){
  for (int i = 0; i < n; i++){
   cin >> p[i][0] >> p[i][1];
  }
  sum = 0;
  for (int i = 0; i < n; i++){
   sum += p[i][1];
  }
  double low = 0, high = y, mid = (low + high) / 2, midmid;
  while (low + 1e-6 < high){
   mid = (low + high) / 2;
   midmid = (mid + high) / 2;
   if (cal(mid) < cal(midmid))high = midmid;
   else low = mid;
  }
  cout << fixed << setprecision(2) << cal(mid) << endl;
 }
 return 0;
}



0 0
原创粉丝点击