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;
}
本文来自博客园,作者:斯文~,转载请注明原文链接:https://www.cnblogs.com/zhiweb/p/15483287.html

浙公网安备 33010602011771号