hdu 3008 简单dp(背包的思想)

就是个最最基本的0/1背包

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define MAX 107

using namespace std;

int n,t,q;

int dp[MAX][MAX];
int w[MAX];
int c[MAX];

int main ( )
{
    while ( ~scanf ( "%d%d%d" , &n , &t , &q ) , n+t+q )
    {
        for ( int i = 1 ; i <= n ; i++ ) scanf ( "%d%d" , &c[i] , &w[i] );
        w[0] = 1 , c[0] = 0;
        memset ( dp , -1 , sizeof ( dp ) );
        int m = 100/q;
        if ( 100%q ) m++;
        dp[0][100] = 0;
        int i,j,k;
        bool flag = false;
        for ( i = 1 ; i <= m ; i++ )
        {
            for ( j = 0 ; j <= 100 ; j++ )
            {
                for ( k = 0 ; k <= n ; k++ )
                {
                    if ( j-t >= 0 && j-t+c[k] <= 100  )
                        if ( dp[i-1][j-t+c[k]] != -1 )
                            dp[i][j] = max ( dp[i][j] , dp[i-1][j-t+c[k]] + w[k] );
                    if ( dp[i][j] >= 100 ) 
                    {
                        flag = true;
                        break;
                    }
                }
                if ( flag ) break;
            }
            if ( flag ) break;
        }
        
        if ( i > m ) puts ( "My god" );
        else printf ( "%d\n" , i );
    }
}


你可能感兴趣的:(C++,动态规划,背包)