题目链接:hdoj 1171
多重背包问题,由于数据量较小,所以可以不用二进制优化。
代码:
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
#define M(a) memset(a,0,sizeof(a))
#define Max(a,b) ((a>b)?a:b)
#define Min(a,b) ((a<b)?a:b)
#define debug 0
const int maxn = 50 + 5;
int ans[250005], w[maxn], num[maxn], n;
int total;
void Do()
{
int t = total / 2;
for (int i = 1; i <= n; i++)
for (int k = 0; k <= t; k++)
for (int j = 0; j*w[i] <= k&&j <= num[i]; j++)
{
ans[k] = Max(ans[k], ans[k - j*w[i]] + j*w[i]);
}
printf("%d %d\n", total - ans[t], ans[t]);
}
int main()
{
#if debug
freopen("in.txt", "r", stdin);
#endif//debug
while (~scanf("%d", &n))
{
if (n < 0)
break;
M(ans);
total = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &w[i], &num[i]);
total += w[i] * num[i];
}
Do();
}
return 0;
}