本文主要是介绍uva 10130 简单背包,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:
背包和
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>#define LL long longusing namespace std;
const int maxn = 1e3 + 10;
const int maxw = 30 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = 4 * atan(1.0);
const double ee = exp(1.0);int n;
int p[maxn];
int w[maxn];
int dp[maxn][maxw];int dfs(int c)
{memset(dp, 0, sizeof(dp));for (int i = 1; i <= n; i++){for (int j = 0; j <= c; j++){dp[i][j] = (i == 1 ? 0 : dp[i - 1][j]);if (w[i] <= j){dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + p[i]);}}}return dp[n][c];
}int main()
{#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALint ncase;scanf("%d", &ncase);while (ncase--){scanf("%d", &n);for (int i = 1; i <= n; i++){scanf("%d%d", &p[i], &w[i]);}int k;scanf("%d", &k);int ans = 0;for (int i = 0; i < k; i++){int x;scanf("%d", &x);ans += dfs(x);}printf("%d\n", ans);}return 0;
}
这篇关于uva 10130 简单背包的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!