标签:std csharp clu for one lse str bank arp
裸地DP,方程为:dp[j]=min(dp[j],dp[j-b[i]]+a[i])。代码:
#include<iostream> #include<cmath> #include<cstring> #define INF 0x3f3f3f3f using namespace std; int n,m,k; int dp[100001],a[110000],b[1100000]; int main() { cin>>n>>m; cin>>k; m=m-n;//剩余空间 for(int i=1;i<=k;i++) { cin>>a[i]>>b[i]; } memset(dp,INF,sizeof(dp)); dp[0]=0; for(int i=1;i<=k;i++) { for(int j=b[i];j<=m;j++)//从b[i]开始 { dp[j]=min(dp[j],dp[j-b[i]]+a[i]); } } if(dp[m]!=INF) { cout<<"The minimum amount of money in the piggy-bank is "<<dp[m]<<"."; } else { cout<<"This is impossible."; } }
嗯~~
标签:std csharp clu for one lse str bank arp
原文地址:https://www.cnblogs.com/dai-jia-ye/p/9359433.html