HDOJ 2159

FATE

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5737    Accepted Submission(s): 2626


Problem Description
最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度。当忍耐度降到0或者0以下时,xhd就不会玩这游戏。xhd还说了他最多只杀s只怪。请问他能升掉这最后一级吗?
 

Input
输入数据有多组,对于每组数据第一行输入n,m,k,s(0 < n,m,k,s < 100)四个正整数。分别表示还需的经验值,保留的忍耐度,怪的种数和最多的杀怪数。接下来输入k行数据。每行数据输入两个正整数a,b(0 < a,b < 20);分别表示杀掉一只这种怪xhd会得到的经验值和会减掉的忍耐度。(每种怪都有无数个)
 

Output
输出升完这级还能保留的最大忍耐度,如果无法升完这级输出-1。
 

Sample Input
   
   
   
   
10 10 1 10 1 1 10 10 1 9 1 1 9 10 2 10 1 1 2 2
 

Sample Output
   
   
   
   
0 -1 1
 

Author
Xhd
 

Source
2008信息工程学院集训队——选拔赛
 

Recommend
linle
 
二维背包。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <iomanip>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1|1
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
#define mid ((l + r) >> 1)
#define mk make_pair
const int MAXN = 100 + 50;
const int maxw = 100 + 20;
const int MAXNNODE = 1000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 10007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
const D e = 2.718281828459;
int  dp[MAXN][MAXN];
int v[MAXN] , c[MAXN];
int  n , m  , k , s;
int main()
{
    //ios::sync_with_stdio(false);
#ifdef Online_Judge
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif // Online_Judge
    while(~scanf("%d%d%d%d" , &n , &m , &k , &s))
    {
        FORR(i , 1 ,  k)scanf("%d%d" , &v[i]  , &c[i]);
        clr(dp , 0);

        FORR(i , 1 , k)FORR(j , 1 , s)///杀几只怪
            FORR(t , c[i] , m)
                if(dp[j - 1][t - c[i]] + v[i] > dp[j][t])
                    dp[j][t] = dp[j - 1][t - c[i]] + v[i];

        if(dp[s][m] < n)printf("-1\n");
        else
        {
            int _min = m;
            FORR(i , 1 , s)FORR(j , 0  ,m)
                if(dp[i][j] >= n&& j < _min)_min = j;
            printf("%d\n" , m - _min);
        }
    }
    return 0;
}


你可能感兴趣的:(dp,ACM)