51Nod - 1085 背包问题

https://vjudge.net/problem/51Nod-1085
01背包模板题

#include<iostream>
#include<algorithm>
#include<cstring>
#define open ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
using namespace std;
const int N=1010,M=10010;
int n,m;
int w[N],v[N];
int f[N][M];
int main()
{
	open;
	while(cin>>n>>m)
	{
		for(int i=1;i<=n;i++) cin>>v[i]>>w[i];
		
		memset(f,0,sizeof f);
		for(int i=1;i<=n;i++)
			for(int j=0;j<=m;j++)
				if(v[i]>j)
					f[i][j]=f[i-1][j];
				else
					f[i][j]=max(f[i-1][j],f[i-1][j-v[i]]+w[i]);
		cout<<f[n][m]<<"\n";
	}
	return 0;
}
posted @ 2021-07-30 10:39  斯文~  阅读(13)  评论(0)    收藏  举报

你好!