本文主要是介绍hdu1114,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
简单dp
#include <iostream>using namespace std;int dp[10005];int main()
{int v[505];int w[505];int e,f;int T;cin>>T;while (T--){cin>>e>>f;memset(v,0,sizeof(v));memset(w,0,sizeof(w));memset(dp,0,sizeof(dp));int n;cin>>n;for (int i=1;i<=n;i++)cin>>v[i]>>w[i];int sum=f-e;for (int i=1;i<=sum;i++){for (int t=1;t<=n;t++){if ((i>=w[t]&&dp[i]==0)||(i>=w[t]&&dp[i]>dp[i-w[t]]+v[t])){if (dp[i-w[t]]!=0||i-w[t]==0)dp[i]=dp[i-w[t]]+v[t];}}}/*for (int i=1;i<=sum;i++)cout<<dp[i]<<" ";cout<<endl;*/if (dp[sum]!=0)printf("The minimum amount of money in the piggy-bank is %d.\n",dp[sum]);elseprintf("This is impossible.\n");}
}
这篇关于hdu1114的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!