ZOJ 3623 - Battle Ships(完全背包)

来源:互联网 发布:雪肌精乳液好用吗 知乎 编辑:程序博客网 时间:2024/05/16 17:44

Description

Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which hasL longevity. The player has a military factory, which can produce N kinds of battle ships. The factory takes ti seconds to produce thei-th battle ship and this battle ship can make the tower loss li longevity every second when it has been produced. If the longevity of the tower lower than or equal to 0, the player wins. Notice that at each time, the factory can choose only one kind of battle ships to produce or do nothing. And producing more than one battle ships of the same kind is acceptable.

Your job is to find out the minimum time the player should spend to win the game.

Input

There are multiple test cases.
The first line of each case contains two integers N(1 ≤ N ≤ 30) andL(1 ≤ L ≤ 330), N is the number of the kinds of Battle Ships,L is the longevity of the Defense Tower. Then the following N lines, each line contains two integerst i(1 ≤ t i ≤ 20) and li(1 ≤li ≤ 330) indicating the produce time and the lethality of the i-th kind Battle Ships.

Output

Output one line for each test case. An integer indicating the minimum time the player should spend to win the game.

Sample Input

1 11 12 101 12 53 1001 103 2010 100

Sample Output

245

                                                                  

思路:

一开始的想法是将破坏值作为dp数组的下标,但是死活无法得到正确答案,思路是很有漏洞的。

应该转化思路,将时间作为dp 的下标,dp[i] 表示在时间i 时造成的破坏值。状态转移方程是:dp[j] = max(dp[j] , dp[j - t[i]] + (j - t[i])*l[i] );最后一个循环枚举时间(1<=i<=600)输出dp[i]第一个值大于等于p 的i值。

CODE:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int inf = 0xffffff;int t[35], l[35], dp[400];int main(){    //freopen("in", "r", stdin);    int n, p;    while(~scanf("%d %d", &n, &p)){        memset(dp, 0, sizeof(dp));        for(int i = 0; i < n; ++i){            scanf("%d %d", &t[i], &l[i]);        }        for(int i = 0; i < n; ++i){            for(int j = t[i]; j <= t[i] + p; ++j){                dp[j] = max(dp[j], dp[j - t[i]] + (j - t[i]) * l[i]);            }        }        for(int i = 0; i <= 660; i++){            if(dp[i] >= p){                printf("%d\n", i);                break;            }        }    }    return 0;}


0 0
原创粉丝点击