HDU -- 2191 -- we can finally say the DP water question sentence -.-

Source: Internet
Author: User

This is the recommend of the previous question ..

Although multiple backpacks are supported, binary splitting is not required because the data is too small.

I have written both... both 0ms ..

 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5  6 const int size = 110; 7 int weight[size*20]; 8 int val[size*20]; 9 int dp[size];10 11 int main()12 {13     cin.sync_with_stdio(false);14     int t , n , m , cnt , a , b , c;15     cin >> t;16     while(t--)17     {18         cin >> n >> m;19         cnt = 0;20         memset( dp , 0 , sizeof(dp) );21         for( int i = 1 ; i<=m ; i++ )22         {23             cin >> a >> b >> c;24             for( int j = 1 ; j<=c ; j<<=1 )25             {26                 val[cnt] = j * a;27                 weight[cnt++] = j*b;28                 c -= j;29             }30             if(c)31             {32                 val[cnt] = c*a;33                 weight[cnt++] = c*b;34             }35         }36         for( int i = 0 ; i<cnt ; i++ )37         {38             for( int j = n ; j>=val[i] ; j-- )39             {40                 dp[j] = max( dp[j] , dp[ j-val[i] ] + weight[i] );41             }42         }43         cout << dp[n] << endl;44     }45     return 0;46 }
View code

 

 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5  6 const int size = 110; 7 int weight[size*20]; 8 int val[size*20]; 9 int dp[size];10 11 int main()12 {13     cin.sync_with_stdio(false);14     int t , n , m , cnt , a , b , c;15     cin >> t;16     while(t--)17     {18         cin >> n >> m;19         cnt = 0;20         memset( dp , 0 , sizeof(dp) );21         for( int i = 1 ; i<=m ; i++ )22         {23             cin >> a >> b >> c;24             while(c--)25             {26                 weight[cnt] = b;27                 val[cnt++] = a;28             }29         }30         for( int i = 0 ; i<cnt ; i++ )31         {32             for( int j = n ; j>=val[i] ; j-- )33             {34                 dp[j] = max( dp[j] , dp[ j-val[i] ] + weight[i] );35             }36         }37         cout << dp[n] << endl;38     }39     return 0;40 }
View code

I have to be serious about this afternoon's online competition -.-

HDU -- 2191 -- we can finally say the DP water question sentence -.-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.