题目链接:E - Product Development (atcoder.jp)
因为最多为5,因此可以暴力枚举
int dp[10][10][10][10][10]; int a[110][10]; int n, k, p; signed main() { for (int i1 = 0; i1 <= 5; i1++) for (int i2 = 0; i2 <= 5; i2++) for (int i3 = 0; i3 <= 5; i3++) for (int i4 = 0; i4 <= 5; i4++) for (int i5 = 0; i5 <= 5; i5++) dp[i1][i2][i3][i4][i5] = 1145141919810; dp[0][0][0][0][0] = 0; cin >> n >> k >> p; for (int i = 1; i <= n; i++) { int x; cin >> x; for (int j = 1; j <= k; j++) { cin >> a[i][j]; } for (int j = k + 1; j <= 5; j++) { a[i][j] = p; } for (int i1 = p; i1 >=0 ; i1--) for (int i2 = p; i2 >=0 ; i2--) for (int i3 = p; i3 >=0 ; i3--) for (int i4 = p; i4 >=0 ; i4--) for (int i5 = p; i5 >=0 ; i5--) { dp[min(p, i1 + a[i][1])][min(p, i2 + a[i][2])][min(p, i3 + a[i][3])][min(p, i4 + a[i][4])][min(p, i5 + a[i][5])] = min(dp[min(p, i1 + a[i][1])][min(p, i2 + a[i][2])][min(p, i3 + a[i][3])][min(p, i4 + a[i][4])][min(p, i5 + a[i][5])], dp[i1][i2][i3][i4][i5] + x); } } if (dp[p][p][p][p][p] == 1145141919810) cout << -1; else cout << dp[p][p][p][p][p]; }
也可以使用动态规划。没太看懂应该是状态压缩吧(jiangly)的代码
int main() { IOS; int n, k, p; cin >> n >> k >> p; vector<int> pw(k + 1); pw[0] = 1; for(int i = 1; i <= k; i ++) pw[i] = pw[i - 1] * (p + 1); vector<ll> dp(pw[k], -1ll); dp[0] = 0; for(int i = 0; i < n; i ++) { int c; cin >> c; vector<int> a(k); for(int j = 0; j < k; j ++) cin >> a[j]; for(int s = pw[k] - 1; s >= 0; s --) { int t = 0; for(int j = 0; j < k; j ++) { int aa = s / pw[j] % (p + 1); int na = min(p, aa + a[j]); t += na * pw[j]; } if(dp[s] != -1 && (dp[t] == -1 || dp[t] > dp[s] + c)) { dp[t] = dp[s] + c; } } } cout << dp.back() << endl; return 0; }
标签:背包,pw,min,int,i5,i1,dp From: https://www.cnblogs.com/ZouYua/p/17745841.html