【算法】【动态规划】Bus Fare
2015-08-25 15:49
204 查看
问题描述:
In a city, a bus stops every kilometer. Also the bus fare is different from 1km to 10km, so you can use each section well to decrease the total fare to get to the destination.For example, let’s suppose a bus fare list as below:
According to the list, the minimum fare would be 147 for 15km: 1 ticket for 3km + 2 tickets for 6km. Or 3 tickets for 5km will have the same result. Other ways cost more. When the bus fare and distance are given, calculate the minimum fare that you can move
the given distance.
Time limit: 1 second (java: 2 seconds)
[Input]
Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 30) are given in a row.
The info of the bus fare, 10 integers are given on the first row per each test case. This is the bus fare from 1km to 10km respectively, and the maximum bus fare is 500. In addition, i km fare is not always bigger than (i-1)km. The distance to move, N is given
on the second row. (1 ≤ N ≤ 10000)
[Output]
Output the minimum fare to give the given distance on the first row per each test case.
[I/O Example]
Input
12 21 31 40 49 58 69 79 90 101
15
12 20 30 40 25 60 70 80 90 11
21
Output
147
34
分析:
动态规划,状态转移方程fare(n) = min{fare(n), fare(n-1) + c(1), ......., fare(n-i) + c(i)}源码:
*/ // In Practice, You should use the statndard input/output // in order to receive a score properly. // Do not use file input and output. Please be very careful. #include <cstdio> #include <iostream> using namespace std; #define min(a,b) a < b ? a : b int c[11]; int fare[10001] = { 0 }; int main(int argc, char** argv) { int tc, T; int t; // The freopen function below opens input.txt file in read only mode, and afterward, // the program will read from input.txt file instead of standard(keyboard) input. // To test your program, you may save input data in input.txt file, // and use freopen function to read from the file when using cin function. // You may remove the comment symbols(//) in the below statement and use it. // Use #include<cstdio> or #include<stdio.h> to use the function in your program. // But before submission, you must remove the freopen function or rewrite comment symbols(//). freopen("input.txt", "r", stdin); cin >> T; for (tc = 0; tc < T; tc++) { /********************************** * Implement your algorithm here. * ***********************************/ for (int i = 1; i < 11; i++) { cin >> c[i]; } cin >> t; for (int i = 1; i <= t; i++) fare[i] = 99999999; for (int i = 1; i < 11; i++) { for (int j = i; j <= t; j++) { fare[j] = min(fare[j], fare[j-i]+c[i]); } } // Print the answer to standard output(screen). cout << fare[t] << endl; } return 0;//Your program should return 0 on normal termination. }
相关文章推荐
- android——如何从一个activity中退出程序
- java内存区域
- C++ List 双向链表 实现 会用也要会写
- 文件读写操作笔记(2)
- Servlet编写登录界面
- 【jQuery Mobile学习】jQuery Mobile简介
- 解决子窗口(视图)截获父窗口消息的2个办法(MFC)
- 回顾交互设计史:回归人性,重拾质朴
- 【水题】【SCOI 2005】【bzoj 1088】扫雷Mine
- 华为上机试题6(时间相加)
- 使用Beautifulsoup去除特定标签
- Spark修炼之道(基础篇)——Linux大数据开发基础:第五节:vi、vim编辑器(一)
- 【数据结构&&等差数列】KMP简介和算法的实现(c++ && java)
- android沉浸式状态栏设置(4.4以上版本)
- Ubuntu12.04重复输入密码也进不去密码
- 零零碎碎写的脚本(六)创建磁盘分区并自动挂载
- Smarty模板的逻辑运算符号稍微做一下总结
- jsp使用ajax技术与后台通信
- spring 框架的xml文件如何读取properties文件数据
- Zabbix微信企业订阅号报警设置 推荐