Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我用背包问题9讲上的多重背包的伪代码写的,怎么一直过不了,各位大牛看看,谢谢!

Posted by shangmin at 2009-06-19 14:25:05 on Problem 1276
vector<int> dp(100002);

class node
{
public:
        int count;   //每个币种的数目
	int num;     //面值
}Nodes[1001];

int t[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};

int main()
{
	int cashes, m;
	while(cin >> cashes >> m)
	{
		fill(dp.begin(), dp.begin() + cashes + 2, 0);
		for(int i = 1; i <= m; i++)
		{
			cin >> Nodes[i].count >> Nodes[i].num;
		}

		for(int i = 1; i <= m; i++)
		{
			if(Nodes[i].count * Nodes[i].num >= cashes)
			{
				for(int j = Nodes[i].num; j <= cashes; j++)
				{
					dp[j] = max(dp[j], dp[j - Nodes[i].num] + Nodes[i].num);
				}
			}
			else
			{
				int k = 0;
				int amount = Nodes[i].count;
				while(t[k] < amount)
				{
					for(int j = cashes; j >= t[k] * Nodes[i].num; j--)
					{
						dp[j] = max(dp[j], dp[j - t[k] * Nodes[i].num] + t[k] * Nodes[i].num);
					}
					amount -= t[k];
					k++;
				}
				for(int j = cashes; j >= Nodes[i].count * Nodes[i].num; j--)
				{
					dp[j] = max(dp[j], dp[j - Nodes[i].count * Nodes[i].num] + Nodes[i].count * Nodes[i].num);
				}
			}
		}
		cout << dp[cashes] << endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator