[Jobdu] 題目1455:珍惜現在,感恩生活

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   strong   資料   for   

題目描述:

為了挽救災區同胞的生命,心繫災區同胞的你準備自己採購一些糧食支援災區,現在假設你一共有資金n元,而市場有m種大米,每種大米都是袋裝產品,其價格不等,並且只能整袋購買。請問:你用有限的資金最多能採購多少公斤糧食呢?

輸入:

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

輸出:

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

範例輸入:
18 22 100 44 100 2
範例輸出:
400

經典的背包問題,動態規劃。

 1 #include <iostream> 2 using namespace std; 3   4 int C; 5 int n, m; 6 int p, h, c; 7 int dp[101]; 8 int res; 9  10 int max(int a, int b) {11     return a > b ? a : b;12 }13  14 void init() {15     res = 0;16     for (int i = 0; i < n + 1; ++i) {17         dp[i] = -1;18     }19     dp[0] = 0;20 }21  22 void refresh() {23     for (int i = 0; i < c; ++i) {24         for (int j = n; j >= p; --j) {25             if (dp[j-p] >= 0) {26                 dp[j] = max(dp[j], dp[j-p] + h);27             }28             res = max(res, dp[j]);29         }30     }31 }32  33 int main() {34     cin >> C;35     for (int i = 0; i < C; ++i) {36         init();37         cin >> n >> m;38         for (int j = 0; j < m; ++j) {39             cin >> p >> h >> c;40             refresh();41         }42         cout << res << endl;43     }44     return 0;45 }46 /**************************************************************47     Problem: 145548     User: hupo25049     Language: C++50     Result: Accepted51     Time:10 ms52     Memory:1520 kb53 ****************************************************************/

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.