UVa 11450 Wedding shopping (DP)

来源:互联网 发布:在淘宝上卖油画好卖吗 编辑:程序博客网 时间:2024/05/14 22:51

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2445


采用滚动数组实现。

此外,这题颇有点模拟的味道。


完整代码:

/*0.035s*/#include<bits/stdc++.h>using namespace std;bool dp[205];vector<int> tmp;///临时数组int main(){int t, m, c, k, cost, i, mx, tmpmx;bool finalok, ok;scanf("%d", &t);while (t--){scanf("%d%d", &m, &c);memset(dp, 0, sizeof(dp));finalok = dp[tmpmx = 0] = true;while (c--){scanf("%d", &k);ok = false;tmp.clear();while (k--){scanf("%d", &cost);if (finalok)for (i = 0; i <= mx && i + cost <= m; ++i)if (dp[i]) ok = true, tmpmx = max(tmpmx, i + cost), tmp.push_back(i + cost);}mx = tmpmx;memset(dp, 0, sizeof(dp));for (i = 0; i < tmp.size(); ++i) dp[tmp[i]] = true;if (finalok) finalok = ok;}if (finalok) printf("%d\n", mx);else puts("no solution");}return 0;}

0 0
原创粉丝点击