您的位置:首页 > 运维架构

UVa 11450 Wedding shopping (DP)

2014-02-25 16:28 387 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: