soj 3360 Buying hay (完全背包)


题意:
有n个物品,每个物品都是无限制多个.具有价值和花费.你需要购买总价值至少为t的物品,而且它们的花费最小.
分析:
裸的恰好装满的完全背包之上多了,必须要大于等于最大容量,只需对转移方程式稍加变形即可.

#include 
#include 
#include 
#include 
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxh = 1e4 + 10;
const int maxn = 105;

int p[maxn], c[maxn];
int dp[6 * maxh];

int main(void) {
    int n, h;
    while (~scanf("%d%d", &n, &h)) {
    for(int i = 0; i < n; i++) scanf("%d%d", &p[i], &c[i]);
    fill(dp, dp + 6 * maxh, INF);
        dp[0] = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= h; j++) {
            dp[j + p[i]] = min(dp[j + p[i]], dp[j] + c[i]);
        }
//        for (int j = 0; j <= h + 10; j++) cout << dp[j] << " ";
//        cout << endl;
    }
    int mins = INF;
    for (int i = h; i < 6 * maxh; i++) mins = min(mins, dp[i]);
        printf("%d\n", mins);}
    return 0;
}

你可能感兴趣的:(ACM_动态规划)