【算法】【动态规划】Bus Fare

问题描述:

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.
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值