HDU 3496 Watch The Movie -- 二维费用背包 费用刚好完全消耗

来源:互联网 发布:医疗与大数据 编辑:程序博客网 时间:2024/05/17 06:49
/*http://acm.hdu.edu.cn/showproblem.php?pid=3496 Watch The Movie二维费用的背包 其中第二费用为隐藏条件恰好第二费用刚好完全消耗,注意初始化。别太粗心了*/#include <cstdio>#include <iostream>#include <string>#include <cstring>#define CLR(c,v) (memset(c,v,sizeof(c)))using namespace std;const int inf = -(1<<30);const int INF =  (1<<30);const int M   =  1e3 + 10;int dp[M][110]; // 二维费用背包int v[M];int c[M];void ZeroOnePack(int cost,int cost2, int value, int max_cost, int max_cost2){for(int j = max_cost2 ; j >= cost2 ; j--){for (int i = max_cost ; i >= cost ; i--){if( dp[i][j] < dp[i-cost][j-cost2] + value){dp[i][j] = dp[i-cost][j-cost2] + value;}}}}int main (){int Ncase;cin >> Ncase;while(Ncase --){int n,max_cost2,max_cost;cin >> n >> max_cost2 >> max_cost;for(int i = 0 ; i <= max_cost ; i++){for(int j = 0 ; j <= max_cost2 ; j++){dp[i][j] = inf;}dp[i][0] = 0; // 注意初始化}for (int i = 1 ; i <= n ; i++){cin >> c[i] >> v[i] ; ZeroOnePack(c[i] , 1 , v[i] , max_cost , max_cost2);}if (dp[max_cost][max_cost2] < 0)dp[max_cost][max_cost2] = 0; //If DuoDuo can’t watch all of the movies that her uncle had bought for her, please output 0.cout << dp[max_cost][max_cost2] << endl;}return 0;}

原创粉丝点击