01背包,01背包問題

來源:互聯網
上載者:User

01背包,01背包問題

輸入

每個測試點(輸入檔案)有且僅有一組測試資料。

每組測試資料的第一行為兩個正整數N和M,表示獎品的個數,以及小Ho手中的獎券數。

接下來的n行描述每一行描述一個獎品,其中第i行為兩個整數need(i)和value(i),意義如前文所述。

測試資料保證

對於100%的資料,N的值不超過500,M的值不超過10^5

對於100%的資料,need(i)不超過2*10^5, value(i)不超過10^3

輸出

對於每組測試資料,輸出一個整數Ans,表示小Ho可以獲得的總喜好值。

範例輸入
5 1000144 990487 436210 673567 581056 897
範例輸出
2099

#include <stdio.h>#define maxn 100005int dp[maxn];int max(int a, int b) {return a > b ? a : b;}int main() {int N, M, i, w, v, j;scanf("%d%d", &N, &M);for(i = 0; i < N; ++i) {scanf("%d%d", &w, &v);for(j = M; j >= w; --j)dp[j] = max(dp[j], dp[j-w] + v);}printf("%d\n", dp[M]);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.