公路乘车(完全背包)

来源:互联网 发布:js如何定义类 编辑:程序博客网 时间:2024/05/08 13:17

公路乘车

 描述 Description 

 一个特别的单行街道在每公里处有一个汽车站。顾客根据他们乘坐汽车的公里使来付费。例如下表就是一个费用的单子。没有一辆车子行驶超过10公里,一个顾客打算行驶n公里(1<=n<=100),它可以通过无限次的换车来完成旅程。最后要求费用最少。

输入格式 Input Format 

 第一行十个整数分别表示行走1到10公里的费用(<=500)。注意这些数并无实际的经济意义,即行驶10公里费用可能比行驶一公里少。 第二行一个整数n表示,旅客的总路程数。

输出格式 Output Format 

 仅一个整数表示最少费用。 

样例输入 Sample Input 

 12  21  31  40   49  58  69  79  90  101 

15

样例输出 Sample Output

 147

#include <iostream>#include <algorithm>#include <cstring>using namespace std;#define M 999999int main(int ac,char *av[]){    int dp[101],i,j,fee;    memset(dp,M,sizeof(dp));    dp[0]=0;    for(i=1;i<=10;i++)    {        cin>>fee;        for(j=i;j<=100;j++)        dp[j]=min(dp[j],dp[j-i]+fee);    }    int n;    cin>>n;    cout<<dp[n]<<endl;    return 0;}


原创粉丝点击