您的位置:首页 > 其它

【HDOJ】2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

2014-04-28 14:02 288 查看
多重背包。

#include <stdio.h>
#include <string.h>

int ps, hs, cs, dp[105];
int n, m;

void completePac(int p, int h) {
int i, tmp;

for (i=p; i<=n; ++i) {
tmp = dp[i-p] + h;
if (dp[i] < tmp)
dp[i] = tmp;
}
}

void onezeroPac(int p, int h) {
int i, tmp;

for (i=n; i>=p; --i) {
tmp = dp[i-p] + h;
if (dp[i] < tmp)
dp[i] = tmp;
}
}

void multiPac(int p, int h, int c) {
int k;

if (p*c >= n) {
completePac(p, h);
return ;
}
k = 1;
while (k < c) {
onezeroPac(k*p, k*h);
c -= k;
k *= 2;
}
if (c)
onezeroPac(c*p, c*h);
}

int main() {
int c;
int i;

scanf("%d", &c);

while (c--) {
scanf("%d %d", &n, &m);
memset(dp, 0, sizeof(dp));
for (i=0; i<m; ++i) {
scanf("%d %d %d", &ps, &hs, &cs);
multiPac(ps, hs, cs);
}
printf("%d\n", dp
);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐