多重背包-未最佳化-HDU-2191

來源:互聯網
上載者:User
Input輸入資料首先包含一個正整數C,表示有C組測試案例,每組測試案例的第一行是兩個整數n和m(1<=n<=100, 1<=m<=100),分別表示經費的金額和大米的種類,然後是m行資料,每行包含3個數p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分別表示每袋的價格、每袋的重量以及對應種類大米的袋數。 


Output對於每組測試資料,請輸出能夠購買大米的最多重量,你可以假設經費買不光所有的大米,並且經費你可以不用完。每個執行個體的輸出佔一行。 


Sample Input

18 22 100 44 100 2
 

#include <iostream>using namespace std;int t,w[101],cost[101],nums[101],opt[101], m, n;int main() {cin >> t;while(t--){cin >> n >> m;for(int i=0; i<m; i++){cin >>  cost[i] >> w[i]  >> nums[i];}for(int i=0; i<=n; i++)opt[i] = 0;for(int i=0; i<m; i++)for(int l=0; l<nums[i]; l++)for(int j=n; j>=cost[i]; j--)opt[j] = max(opt[j], opt[j-cost[i]] + w[i]);cout << opt[n] << endl;}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.