标签:
动态规划的题总是 看完别人的代码自己理解着打,今天自己终于自己打一回,遇到了一点问题,不过解决后发现现在理解更透彻了。
1 /*01背包HDU2546*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<string.h> 5 using namespace std; 6 int cost[1010]; 7 int dp[1010]; 8 int main() 9 { 10 int n,money; 11 while(cin>>n&&n) 12 { 13 14 for(int i=0; i<n; i++) 15 cin>>cost[i]; 16 sort(cost,cost+n); 17 cin>>money; 18 if(money<5){ 19 cout<<money<<endl; 20 continue; 21 } 22 int temp=money-5; 23 memset(dp,0,sizeof(dp)); 24 for(int i=0; i<n-1; i++) 25 for(int j=temp; j>=1; j--) 26 if(cost[i]<=j) 27 { 28 dp[j]=max(dp[j],dp[j-cost[i]]+cost[i]); 29 // cout<<dp[j]<<endl; 30 } 31 cout<<money-dp[temp]-cost[n-1]<<endl; 32 } 33 }
标签:
原文地址:http://www.cnblogs.com/VectorLin/p/5200560.html