HDU -- 2639--01 backpack's K solution <different solutions for different decisions>

Source: Internet
Author: User

This question is very novel .....

When I used to perform a backpack, why didn't I think I could find the k-th optimal solution? I knew that I could just write a DP [vol] and directly find the optimal solution that I could achieve =-=

At the beginning, I ignored a little bit of DP [x] 1-vol stored in the optimal solution that can be stored in the current x volume... so if I choose to traverse this array to find the k-th solution, it will definitely not work.

It may not be because the optimal decision under current X has been replaced by another updated policy.

Then I saw the prompt on Baidu.

Obtain the sub-optimal solution and the K-Optimal Solution
For the problem of the sub-optimal solution and the K-optimal solution, if the corresponding optimal solution can write the state transition equation and solve it with dynamic planning, the sub-optimal solution can be solved with the same complexity, the k-th optimal solution is a factor k more than the complexity of the optimal solution.
The basic idea is to convert each State into an ordered queue and convert the max/min in the state transition equation into a merge of ordered queues. Here, we will continue to explain the 0th backpack as an example.
First, look at 01 backpack for the optimal solution state transition equation: F [I] [v] = max {f [I-1] [v], f [I-1] [V-C [I] + W [I]}. If the k-th optimization is required, the state f [I] [v] should be a K-sized array f [I] [V] [1. K]. F [I] [V] [k] indicates the value of the K-optimal solution when the size of the first item and backpack is v. "F [I] [v] is an array of K sizes", which may be easier to understand if you are familiar with C, alternatively, you can simply consider adding one dimension to the original equation. Obviously, F [I] [V] [1 .. k] is arranged in ascending order, so we regard it as an ordered queue.
Then the original equation can be interpreted: f [I] [v] This ordered queue is ordered by F [I-1] [v] and f [I-1] [V-C [I] + W [I] the queue is merged. Ordered queue f [I-1] [v] That f [I-1] [V] [1 .. k], F [I-1] [V-C [I] + W [I] is understood as in F [I-1] [V-C [I] [1 .. k. Merge the two ordered queues and store the results (the first K items) to f [I] [V] [1 .. k]. The complexity is O (k ). The final answer is f [N] [V] [K]. The total complexity is O (nvk ).
Why is this method correct? In fact, the process of solving a correct state transition equation traverses all available policies, which overwrites all solutions to the problem. However, the optimal solution is ignored because it is the optimal solution. If each State is represented as a K-sized array, and the first K Optimal Values of the State can be saved in an orderly manner in this array. Then, the max operation for any two States is equivalent to the merge of two ordered queues from large to small.
In addition, pay attention to the definition of "k-optimal solution". The two schemes with different policies but the same weights are considered as the same solution or different solutions. If it is the former, it is necessary to ensure that the number in the queue is not repeated during the maintenance of the ordered queue.


Merging here reminds me of merging and sorting. Although merging uses recursive division, the merging idea is the same.
Then I still have the question: How can I modify the code if two schemes with the same policy weights are considered as different solutions?
I tried to verify that it was incorrect =-= I think I can convince myself -.-
 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4  5 const int size = 110; 6 int weight[size]; 7 int value[size]; 8 int dp[size*10][40]; 9 int pre[40];10 int next[40];11 12 int main()13 {14     int t , n , m , k , ans , cnt;15     int x , y , z;16     cin >> t;17     while(t--)18     {19         cin >> n >> m >> k;20         cnt = ans = 0;21         memset( dp , 0 , sizeof(dp) );22         for( int i = 0 ; i<n ; i++ )23             cin >> value[i];24         for( int i = 0 ; i<n ; i++ )25             cin >> weight[i];26         for( int i = 0 ; i<n ; i++ )27         {28             for( int j = m ; j>=weight[i] ; j-- )29             {30                 for( int f = 1 ; f<=k ; f++ )31                 {32                     pre[f] = dp[j][f];33                     next[f] = dp[ j-weight[i] ][f] + value[i];34                 }35                 x = y = z = 1;36                 next[k+1] = pre[k+1] = -1;37                 while( z<=k && (x<=k || y<=k) )38                 {39                     if( pre[x]>next[y] )40                     {41                         dp[j][z] = pre[x++];42                     }43                     else44                     {45                         dp[j][z] = next[y++];46                     }47                     if( dp[j][z] != dp[j][z-1] )48                         z ++;49                 }50             }51         }    52         cout << dp[m][k] << endl;53     }54     return 0;55 }
View code

 

Note that next [k + 1] = pre [k + 1] =-1; this statement may be due to the fact that all k elements in the next array have been traversed, but we have not selected K solutions for DP [J] [Z] at the same time.
The Pre [y] array is still being traversed. If we do not mention that its next [k + 1] is set to-1, it will be a random value, which will cause the final result error..
At the same time, if the next pre array is all traversed, it will exit the loop directly. Even if the Z of the DP [J] [Z] array is <K, it will be saved with the initial 0.

 1                     if( pre[x]>next[y] ) 2                     { 3                         dp[j][z++] = pre[x++]; 4                     } 5                     else 6                     { 7                         dp[j][z++] = next[y++]; 8                     } 9                     //if( dp[j][z] != dp[j][z-1] )10                     //    z ++;
 

 

For the two solutions with the same weights for the policy I just mentioned, I have modified it. The next test is wrong.
Which of the following can help me solve this problem? I think this is a great help for understanding the concept of backpack 3q.









HDU -- 2639--01 backpack's K solution <different solutions for different decisions>

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.