|
标准的二维数组的DP
- #include<iostream>
- using namespace std;
- const int mm=110;
- const int mn=1100;
- int W,N;
- int w[mm],v[mm];
- int f[mn][mm];
- int i,j,k;
- int main()
- {
- cin>>W>>N;
- for (i=1; i<=N; i++) cin>>w[i]>>v[i];
- for (k=1; k<=N; k++)
- for (int n=0; n<=W; n++)
- if (n<w[k])f[n][k]=f[n][k-1];
- else
- f[n][k]=max(f[n][k-1],f[n-w[k]][k-1]+v[k]);
- cout<<f[W][N];
- return 0;
- }
复制代码 |
|