CSU 1548 Road And Bridge acm 三分

本文介绍了一种解决从起点到终点经过多个平行于Y轴的河流的路径规划问题的方法。通过合并所有河流并利用三分法找到从陆地到河流最优路径的最低成本方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


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

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值