标签:name ble bsp col ring span cstring 更新 scan
一、01背包:
n个物品,每个物品有其重量和价值。你的背包装物品的总重量不能超过 m ,求获得的最大价值。
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> using namespace std; #define maxn 1100 int main() { int t; scanf("%d", &t); for (int ca = 1; ca <= t; ca++) { int n, m; int f[maxn][maxn], v[maxn], w[maxn]; memset(f, 0, sizeof(f)); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &v[i]); for (int i = 1; i <= n; i++) scanf("%d", &w[i]); for (int i = 1; i <= n; i++) for (int j = 0; j <= m; j++) { f[i][j] = f[i-1][j]; if (j >= w[i]) f[i][j] = max(f[i-1][j], f[i-1][j-w[i]]+v[i]); } printf("%d\n", f[n][m]); } }
标签:name ble bsp col ring span cstring 更新 scan
原文地址:https://www.cnblogs.com/ruthank/p/9424794.html