ZOJ-2224

来源:互联网 发布:windows toolkit win7 编辑:程序博客网 时间:2024/06/16 09:40

完全背包DP,每个物品可选次数不受限制,与01和多重背包不同之处就在于背包大小的循环顺序从小到大遍历,具体原因见背包九讲,了解算法之后并不难写

#include<stdio.h>#include<string.h>static int max(int a, int b){return a > b ? a : b;}int main(){int N, bond[10], interest[10], dp[50000];scanf("%d", &N);while (N--){int amount, year, d, i, j;scanf("%d %d %d", &amount, &year, &d);for (i = 0; i < d; i++)scanf("%d %d", &bond[i], &interest[i]);int y, size;for (y = 0; y < year; y++){memset(dp, 0, sizeof(dp));size = amount - amount % 1000;for (i = 0; i < d; i++)for (j = bond[i]; j <= size; j += 1000)dp[j / 1000] = max(dp[j / 1000],dp[(j - bond[i]) / 1000] + interest[i]);amount += dp[size / 1000];}printf("%d\n", amount);}return 0;}


0 0
原创粉丝点击