二维费用背包
Code:
#include#include #include #include using namespace std;//Mystery_Sky//#define M 5000int f[M][M];int n, m, k;int a[M], b[M], c[M];int main() { scanf("%d%d%d", &n, &m, &k); for(int i = 1; i <= k; i++) scanf("%d%d", &a[i], &b[i]); for(int i = 1; i <= k; i++) { for(int j = n; j >= a[i]; j--) { for(int l = m; l >= b[i]; l--) { f[j][l] = max(f[j][l], f[j-a[i]][l-b[i]]+1); } } } int ans; for(ans = 0; ans <= m; ans++) if(f[n][ans] == f[n][m]) break; printf("%d %d\n", f[n][m], m - ans); return 0;}