Consumer-有依赖背包板子题

    技术2024-12-07  23

    FJ is going to do some shopping, and before that, he needs some boxes to carry the different kinds of stuff he is going to buy. Each box is assigned to carry some specific kinds of stuff (that is to say, if he is going to buy one of these stuff, he has to buy the box beforehand). Each kind of stuff has its own value. Now FJ only has an amount of W dollars for shopping, he intends to get the highest value with the money. Input The first line will contain two integers, n (the number of boxes 1 <= n <= 50), w (the amount of money FJ has, 1 <= w <= 100000) Then n lines follow. Each line contains the following number pi (the price of the ith box 1<=pi<=1000), mi (1<=mi<=10 the number goods ith box can carry), and mi pairs of numbers, the price cj (1<=cj<=100), the value vj(1<=vj<=1000000) Output For each test case, output the maximum value FJ can get Sample Input 3 800 300 2 30 50 25 80 600 1 50 130 400 3 40 70 30 40 35 60 Sample Output 210

    有依赖背包板子题 不多说 上代码

    #include <algorithm> #include <iostream> #include <cstdio> #include <stack> #include <vector> #include <string> #include <cstring> #include <cmath> #include <queue> #include <set> #include <map> #include <list> #include <ctime> using namespace std; #define ll long long #define ld long double #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define EXP 1e-8 #define MOD 1000000007 #define N 50005 #define random(x) rand()%x+1 inline int next_int() { char c; while(c = getchar(), c < '0' || c > '9'); int r = c-'0'; while(c = getchar(), c >= '0' && c <= '9') r = r*10+c-'0'; return r; } ll n; ll w; ll dp[1000000]; ll dp2[1000000]; vector<ll> p[100]; vector<ll> v[100]; ll p_box[100]; ll len; ll tmp; ll tmp2; int main() { #ifdef Wang_Zhifeng freopen("in.txt", "r", stdin); //我**你个** 多组输入都不说一下 #endif while(~scanf("%lld%lld", &n, &w)) { memset(dp, 0, sizeof(dp)); memset(dp2, 0, sizeof(dp2)); for(int i = 0; i < 100; ++i) { p[i].clear(); v[i].clear(); p_box[i] = 0; } for(int i = 0; i < n; ++i) { scanf("%lld%lld", &p_box[i], &len); for(int j = 0; j < len; ++j) { scanf("%lld%lld", &tmp, &tmp2); p[i].push_back(tmp); v[i].push_back(tmp2); } } for(int i = 0; i < n; ++i) { for(int j = 0; j < p_box[i]; ++j) dp2[j] = 0; for(int j = w; j >= p_box[i]; --j)dp2[j] = dp[j-p_box[i]]; len = p[i].size(); for(int j = 0; j < len; ++j) { for(int k = w; k >= p_box[i]+p[i][j]; --k) { dp2[k] = max(dp2[k], dp2[k-p[i][j]]+v[i][j]); //printf("dp2 k:%d val:%lld\n", k, dp2[k]); } } for(int j = 0; j <= w; ++j) dp[j] = max(dp[j], dp2[j]); } ll ans = 0; for(int k = 0; k <= w; ++k) ans = max(ans, dp[k]); printf("%lld\n", ans); } return 0; }
    Processed: 0.010, SQL: 9