CSU_1548_DesignRoad
2015-07-31 16:56
337 查看
Design road
Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 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 parallelto 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; }
相关文章推荐
- xp下使用thrift
- iOS学习----------正则表达式
- 第 2 章 使用数据库管理用户权限
- 引子
- 关于CLR、CIL、CTS、CLS、CLI、BCL和FCL。
- CSS让div背景透明
- java 堆栈 静态
- apache用户认证访问机制(转)
- Oracle修改字段长度以及计算天数
- 编译器括号检查原理(C)
- 【PHP】PHP中__get()和__set()的用法实例详解
- 黑马程序员----java基础面向对象特点及封装
- 用WINRAR将.bat文件转为自启动
- 观看maven视频记录
- 梯度下降法
- 第 3 章 自定义数据库表结构
- js如何使得新打开的窗口居中
- 二维码zxing源码分析(一)camera部分
- Ueditor使用方法
- 【发问】代表ODBC、Ibatis 发问 Hibernate、Linq、Entity、JPA