Big backpack problem (01 backpack)

Source: Internet
Author: User

Big backpack Question: There are n a weight and price value respectively W[i] and V[i] project. The total weight of these products does not exceed the W project. Finds the maximum sum value for all selected scenario price values.

Among them, 1≤n≤40, 1≤w[i], v[i]≤10^15, 1≤w≤10^15.

The first feeling of this problem is the normal 01 backpack.

Just after reading the data range will be found. This time the value and weight can be very large values, compared to n is smaller. The complexity of solving a backpack with DP is O (NW) and therefore cannot be used to solve the problem. At this point we should use the characteristics of the smaller n to find other methods.

The scheme of selecting items always has a common 2^n, so it cannot be enumerated directly, but it is assumed that the items are divided into two halves and then enumerated. Because each part has a maximum of only 20. This is possible. We put the selection method in the first half of the corresponding weight and value of the sum as W1, v1, so that in the second half of the total weight w2≤w-w1 to make the V2 the largest selection method can be.

Therefore, we want to think about the efficient way to find max{v2|w2≤w '} from the enumeration (W2,V2) collection. First, obviously we can exclude all w2[i]≤w2[j] and V2[i] >= V2[j] J.

This can be done according to W2, V2 dictionary order.

The remaining elements then meet W2[i] < W2[j] <=> V2[i] < v2[j]. To calculate max{v2|w2 <= W '}, just look for the largest I to meet W2[i] <= W '. This can be done with a two-point search. If the number of elements remaining is m, the time of the search needs O (LOGM). Due to m≤2^ (N/2), the total time complexity of this algorithm is O (n * 2^ (N/2)), which can be solved within the implementation.

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 50;const long Long INF = 0x3fffffff;typedef long long ll;int N; LL W[n], v[n];    LL W;pair <ll, ll> pi[1 << (N/2)];void solve () {int n2 = N/2;        for (int i = 0; i < (1 << n2); i++) {LL SW = 0, SV = 0;                for (int j = 0; J < N2; J + +) {if ((i >> J) & 1) {SW + = W[j];            SV + = V[j];    }} Pi[i] = Make_pair (SW, SV);    } sort (pi, pi + (1 << n2));    int m = 1;        for (int i = 1; i < (1 << n2); i++) {if (Pi[m-1].second < Pi[i].second) {pi[m++] = Pi[i];    }} LL res = 0;        for (int i = 0; i < (1 << (N-N2)); i++) {LL SW = 0, SV = 0;                for (int j = 0; J < N-n2; J + +) {if ((i >> J) & 1) {SW + = w[n2 + j];            SV + = v[n2 + j]; }} if (sw <= W) {LL TV = (Lower_bound (pi, pi + M, Make_pair (W-SW, INF))-1)->second;        res = max (res, SV + TV); }} printf ("%lld\n", res);} int main () {while (~scanf ("%d%lld", &n, &w)) {for (int i = 0; i < n; i++) {scanf ("%lld%ll        D ", &w[i], &v[i]);    } solve (); } return 0;}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Big backpack problem (01 backpack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.