您的位置:首页 > 其它

【算法】【动态规划】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.
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: