CSU - 1548 三分找最值
2015-12-07 19:43
309 查看
Design road
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
Sample Output
Hint
Source
题意:有河流的地方需要建桥每米花费c2,没有的需要建路每米花费c1,使得从(0,0)到(x,y)花费最少。
思路:合并所有河流记为sum,然后在[0,y]里面通过三分寻找一个值y1,使得从(0,0)到(sum,y1)加上(sum,y1)到(x,y)的花费最少。(把河流和道路分开易于寻找最值)
代码:
Time Limit: 2000MS | Memory Limit: 262144KB | 64bit IO Format: %lld & %llu |
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
题意:有河流的地方需要建桥每米花费c2,没有的需要建路每米花费c1,使得从(0,0)到(x,y)花费最少。
思路:合并所有河流记为sum,然后在[0,y]里面通过三分寻找一个值y1,使得从(0,0)到(sum,y1)加上(sum,y1)到(x,y)的花费最少。(把河流和道路分开易于寻找最值)
代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <set> using namespace std; #define eps 1e-3 double distance(double x1,double y1,double x2,double y2)//求两点之间的距离 { return sqrt((double)(x1-x2)*(x1-x2) + (double)(y1-y2)*(y1-y2)); } int main() { #ifdef CDZSC_June freopen("t.txt","r",stdin); #endif int n; double x,y,c1,c2,s,w,sum; while(~scanf("%d%lf%lf%lf%lf",&n,&x,&y,&c1,&c2)) { sum = 0; for(int i =0; i<n; i++) { scanf("%lf%lf",&s,&w); sum += w;//合并河流 } sum = x - sum;//获取需要建路的宽度 double L = 0,R = y,mid,midmid; while(R - L > eps) { mid = (L+R)/2; midmid = (mid+R)/2; double s1 = distance(0,0,sum,mid)*c1+distance(sum,mid,x,y)*c2; double s2 = distance(0,0,sum,midmid)*c1+distance(sum,midmid,x,y)*c2; //midmid必定大于mid,所以当s1>s2时,意味着最小值在靠近s2的那一边,所以令L = mid来缩小最小值的寻找范围,反之同理 if(s1 > s2){ L = mid; } else{ R = midmid; } } printf("%.2f\n",distance(0,0,sum,mid)*c1+distance(sum,mid,x,y)*c2); } return 0; }
相关文章推荐
- 最短路+最小花费环-HDU-4370-0 or 1
- homerHEVC代码阅读(14)——基础结构之ref_pic_set_t、rate_control_t、rate_distortion_t
- 在线学习网站之freecodecamp
- 关于cout和printf的压栈顺序问题
- 【SSH项目实战】国税协同平台-12.角色权限管理2
- [LeetCode]017-Letter Combinations of a Phone Number
- Linux命令学习笔记(2)----more命令
- 【SSH项目实战】国税协同平台-11.角色权限管理1
- iOS渲染
- 句子逆序
- GeoHash算法详解
- shell取消键盘回显
- 【SSH项目实战】国税协同平台-10.自定义返回结果类型
- C++的运算符重载 http://blog.csdn.net/zgl_dm/article/details/1767201
- 设置和获取按钮的背景图片
- Android Studio第三期 - 加载bar
- tyvj1038忠诚
- 【SSH项目实战】国税协同平台-9.全局异常映射
- Codeforces 602B Approximating a Constant Range
- phpmyadmin显示数据库中文正确php页面输出??乱码的解决方法