FATE hdu 2159 二維的完全背包

來源:互聯網
上載者:User

/*<br />以下分析有參考Baidu 的,剛開始學背包,感覺不順手啊。</p><p>二維的完全背包,狀態轉移方程:dp[i][j] = max(dp[i][j], dp[i - df[t].care][j - 1] + df[t].expr)<br />dp[i][j] 消耗i的忍耐度獲得的最大經驗值,只要dp[i][s] >= n 就可以找到答案</p><p>dp[i][j]表示殺i個怪剩j忍耐值獲得的最大經驗值 狀態方程:dp[i][j] = max(dp[i][j], dp[i - 1][j - df[t].care] + df[t].expr)<br />結果為m - j 其中j為 dp[s][j] >= n (1 <= j <= m)而且j最小</p><p>*/<br />#include <iostream><br />#include <cstdio><br />#include <algorithm><br />using namespace std;</p><p>struct node<br />{<br />int expr;<br />int care;<br />}df[105];<br />int dp[105][105];<br />int main()<br />{<br />int n, m, k, s;<br />while(cin >> n >> m >> k >> s)<br />{<br />for(int i = 1; i <= k ; i++)<br />cin>>df[i].expr >> df[i].care;<br />memset(dp, 0, sizeof(dp));<br />int jj = -1;<br />int i, j, t;<br />/*<br />for(i = 1; i <= m; i++)<br />{</p><p>for(j = 1; j <= s; j++)<br />for(t = 1; t <= k; t++)<br /> if(i >= df[t].care)<br />dp[i][j] = max(dp[i][j], dp[i - df[t].care][j - 1] + df[t].expr);<br />if(dp[i][s] >= n)<br />{<br />jj = i;<br />break;<br />//goto loop;</p><p>}<br />}<br />*/<br />for(i = 1; i <= s; i++)<br />{<br />for(j = 1; j <= m; j++)<br />for(t = 1; t <= k; t++)<br />if(j >= df[t].care)<br />dp[i][j] = max(dp[i][j], dp[i - 1][j - df[t].care] + df[t].expr);<br />for(int j = 1; j <= m; j++)<br />if(dp[s][j] >= n)<br />{<br />jj = j;<br />//break;<br />goto loop;</p><p>}<br />}</p><p>loop:<br />if(jj == -1)<br />printf("%d/n", jj);<br />else<br />printf("%d/n", m - jj);</p><p>}<br />}

聯繫我們

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