二维背包问题,注意初始化和状态转移。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define INF 99999999
int a,b;
int dp[110][110];
int main()
{int n,m,p,i,j,k;while(~scanf("%d%d%d",&n,&m,&p)){for(i=0;i<=n;i++){for(j=0;j<=n;j++)dp[i][j]=INF;}dp[0][0]=0;for(i=0;i<p;i++){scanf("%d%d",&a,&b);for(j=n;j>=a;j--){for(k=m;k>=b;k--){dp[j][k]=min(dp[j][k],dp[j-a][k-b]+1);}}}int minn;minn=INF;for(i=0;i<=m;i++){minn=min(minn,dp[n][i]);}if(minn!=INF)cout<<minn<<endl;else cout<<"mty zhen mei yong!"<<endl;}
}