公路乘车(基础背包入门)

 

Description

 

一个特别的单行街道在每公里处有一个汽车站。顾客根据他们乘坐汽车的公里使来付费。例如下表就是一个费用的单子。

kilometres

1

2

3

4

5

6

7

8

9

10

price

12

21

31

40

49

58

69

79

90

101

没有一辆车子行驶超过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

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #include <stack>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 const int maxn=1e5+10;
17 using namespace std;
18  
19 int dp[1005];
20 int w[1005];
21 int v[1005];
22  
23 int main()
24 {
25     for(int i=1;i<=10;i++)
26     {
27         w[i]=i;
28         scanf("%d",&v[i]);
29     }
30     int m;
31     scanf("%d",&m);
32     memset(dp,0,sizeof(dp));
33     for(int i=w[1];i<=m;i++)//初始化边界
34             dp[i]=v[1]*i;
35     for(int i=2;i<=10;i++)//完全背包问题 
36     {
37         for(int j=w[i];j<=m;j++)
38         {
39            dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
40         }
41     }
42     printf("%d\n",dp[m]);
43     return 0;
44 }

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-12-06 14:52  jiamian22  阅读(274)  评论(0编辑  收藏  举报