标签:dp
10 10 1 10 1 1 10 10 1 9 1 1 9 10 2 10 1 1 2 2
0 -1 1
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int dp[110][110]; const int inf = 0x3f3f3f3f; struct node { int w; int c; }guaiwu[110]; int main() { int n, m, k, s; while (~scanf("%d%d%d%d", &n, &m, &k, &s)) { memset (dp, 0, sizeof(dp)); int ans = inf; for (int i = 1; i <= k; ++i) { scanf("%d%d", &guaiwu[i].w, &guaiwu[i].c); } for (int i = 1; i <= k; ++i) { for (int j = 1; j <= s; ++j) { for (int l = guaiwu[i].c; l <= m; ++l) { dp[j][l] = max(dp[j][l], dp[j - 1][l - guaiwu[i].c] + guaiwu[i].w); if (dp[j][l] >= n) { ans = min(ans, l); } } } } if (dp[s][m] < n) { printf("-1\n"); continue; } printf("%d\n", m - ans); } return 0; }
标签:dp
原文地址:http://blog.csdn.net/guard_mine/article/details/41521971