1503: 公路乘车
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 21 Solved: 13 Scores: 89.83
[ Submit][ Status][ BBS]
Description
一个特别的单行街道在每公里处有一个汽车站。顾客根据他们乘坐汽车的公里使来付费。例如下表就是一个费用的单子。 没有一辆车子行驶超过10公里,一个顾客打算行驶n公里(1< =n< =100),它可以通过无限次的换车来完成旅程。最后要求费用最少。
Input
第一行十个整数分别表示行走1到10公里的费用(< =500)。注意这些数并无实际的经济意义,即行驶10公里费用可能比行驶一公里少。
第二行一个整数n表示,旅客的总路程数。
Output
仅一个整数表示最少费用。
Sample Input
12 21 31 40 49 58 69 79 90 101
15
Sample Output
147
所以这题是完全背包。这个完全背包是求最小值得,所以整个数组先初始化成很大的值,然后dp[0]=0;接着完全背包就可以了。没有坑点。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
int main()
{
long long x[20];
while(cin >> x[0])
{
for(int i=1;i<10;i++)
cin >> x[i];
long long n;
cin >> n;
long long dp[1000];
for(int i=0;i<=n;i++)dp[i] = 9999999;
dp[0] = 0;
for(int i=0;i<10;i++)
{
for(int j = i+1;j<=n;j++)
dp[j] = min(dp[j],dp[j-(i+1)]+x[i]);
}
cout<<dp[n]<<endl;
}
return 0;
}