USACO Training Section 3.3 Shopping Offers
拿给出的每种方案作为一种物品
其他的单卖的物品也作为一种物品
拿它们去跑背包就行
注意编号对应上就行
代码:
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cctype> #include <cstdio> #include <locale> #include <map> using namespace std; const int MAXN = 110; struct ITEM { int tot, cst; int num[6]; }itm[MAXN]; int s, n, totnum; int num[6], id[1005], req[1005], f[6][6][6][6][6]; bool usf[1005]; int main() { scanf("%d", &s); int x = 0, y = 0; for (int i = 1; i <= s; ++i) { scanf("%d", &itm[i].tot); for (int j = 1; j <= itm[i].tot; ++j) { scanf("%d%d", &x, &y); if (!id[x]) { id[x] = ++totnum; num[totnum] = x; } itm[i].num[id[x]] = y; } scanf("%d", &itm[i].cst); } scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &x); if (!id[x]) { id[x] = ++totnum; num[totnum] = x; } scanf("%d", &req[id[x]]); itm[++s].num[id[x]] = 1; itm[s].tot = 1; scanf("%d", &itm[s].cst); } memset(f, 0x3f, sizeof(f)); f[0][0][0][0][0] = 0; for (int i = 1; i <= s; ++i) { for (int a = itm[i].num[1]; a <= req[1]; ++a) { for (int b = itm[i].num[2]; b <= req[2]; ++b) { for (int c = itm[i].num[3]; c <= req[3]; ++c) { for (int d = itm[i].num[4]; d <= req[4]; ++d) { for (int e = itm[i].num[5]; e <= req[5]; ++e) { f[a][b][c][d][e] = min(f[a][b][c][d][e], f[a - itm[i].num[1]][b - itm[i].num[2]][c - itm[i].num[3]][d - itm[i].num[4]][e - itm[i].num[5]] + itm[i].cst); } } } } } } printf("%d\n", f[req[1]][req[2]][req[3]][req[4]][req[5]]); return 0; }
禁止诸如开发者知识库/布布扣/码迷/学步园/马开东等 copy 他人博文乃至博客的网站转载
,用户转载请注明出处:https://www.cnblogs.com/xcysblog/