RQNOJ 169 最小乘车费用:水dp

题目链接:https://www.rqnoj.cn/problem/169

题意:

  给出行驶1~10公里的费用(所有车一样),可以倒车,问行驶n公里的最小费用。

题解:

  大水题。。。 (=´ω`=)

  表示状态:

    dp[i] = min cost

    i:行驶了i公里

  找出答案:

    ans = dp[n]

  如何转移:

    now: dp[i]

    dp[i+j] = min dp[i] + c[j]

    枚举倒车行驶j公里

  边界条件:

    dp[0] = 0

    others = -1

AC Code:

 1 // state expression:
 2 // dp[i] = min cost
 3 // i: drove i km
 4 //
 5 // find the answer:
 6 // ans = dp[n]
 7 //
 8 // transferring:
 9 // now: dp[i]
10 // dp[i+j] = min dp[i] + c[j]
11 //
12 // boundary:
13 // dp[0] = 0
14 // others = -1
15 #include <iostream>
16 #include <stdio.h>
17 #include <string.h>
18 #define MAX_N 105
19 #define MAX_L 15
20 
21 using namespace std;
22 
23 int n;
24 int c[MAX_L];
25 int dp[MAX_N];
26 
27 void read()
28 {
29     for(int i=1;i<=10;i++)
30     {
31         cin>>c[i];
32     }
33     cin>>n;
34 }
35 
36 void solve()
37 {
38     memset(dp,-1,sizeof(dp));
39     dp[0]=0;
40     for(int i=0;i<n;i++)
41     {
42         if(dp[i]!=-1)
43         {
44             for(int j=1;j<=10;j++)
45             {
46                 if(dp[i+j]==-1 || dp[i+j]>dp[i]+c[j])
47                 {
48                     dp[i+j]=dp[i]+c[j];
49                 }
50             }
51         }
52     }
53 }
54 
55 void print()
56 {
57     cout<<dp[n]<<endl;
58 }
59 
60 int main()
61 {
62     read();
63     solve();
64     print();
65 }
原文地址:https://www.cnblogs.com/Leohh/p/7461307.html