Dp2_ K Excellent Solution

Source: Internet
Author: User

Reprint: http://m.blog.csdn.net/blog/a511310132/13465985

For solving the problem of suboptimal solution and K-class, if the corresponding optimal solution problem can write the state transfer equation and solve it with the dynamic programming, then the second optimal solution can often be solved by the same complexity, and the first K solution is more than one coefficient K for the complexity of solving the optimal solution. The basic idea is that each state is represented as an ordered queue, and the max/min in the state transition equation is transformed into the merging of ordered queues. Here is still an example of a 01-pack to explain. First, we look at the state transfer equation of the 01 knapsack to find the optimal solution: F[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}. If a k solution is required, then the state F[i][v] should be an array of size K F[I][V][1..K]. Where F[i][v][k] represents the first I item, the backpack size is V, the value of the K-optimal solution. "F[i][v" is an array of size K "This sentence, the students familiar with C language may be better understood, or can also be simply understood as the original equation added one dimension. Obviously F[I][V][1..K] This K number is arranged from large to small, so we think of it as an orderly queue. Then the original equation can be interpreted as: F[i][v] This ordered queue is composed of f[i-1][v] and F[i-1][v-c[i]]+w[i] These two ordered queues are merged. An ordered queue f[i-1][v] or f[i-1][v][1..k],f[i-1][v-c[i]]+w[i] is understood to be an ordered queue after adding w[i] to each number of f[i-1][v-c[i] [1..K]. The complexity of merging the two ordered queues and storing the first K entries of the results in F[I][V][1..K] is O (K). The final answer is F[n][v][k]. The total complexity is O (vnk).

Why is this method correct? In fact, the process of solving a correct state transition equation iterates through all the available policies and covers all the scenarios of the problem. But because it is the optimal solution, other schemes that do not achieve optimal on any one strategy are ignored. If each state is represented as an array of size k, and in this array, it is ordered to save the first k optimal value that the state is desirable to. The max operation for either state, then, is equivalent to merging two large-to-small ordered queues. Also pay attention to the definition of "K-Optimal solution", the different strategies but the same weight of the two scenarios are considered the same solution or a different solution. If the former, maintain an orderly queue to ensure that the number in the queue is not duplicated.

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5 #defineSC (x) scanf ("%d", &x)6 #defineCL (x, y) memset (x, y, sizeof (x))7 using namespacestd;8 intMain ()9 {Ten     intT; One     intv[101], w[101]; A     inta[101], b[101]; -     intN, vol, I, J, K, K; -     intx, y, Z; the     intp[1011][ -]; -scanf"%d", &t); -      while(t--) -     { +scanf"%d%d%d", &n, &vol, &K); -          for(i =1; I <= N; i++) + SC (v[i]); A          for(i =1; I <= N; i++) at SC (w[i]); -CL (P,0); -CL (A,-1); -CL (b,-1); -a[0] =0; -b[0] =0; in          for(i =1; I <= N; i++) -         { to              for(j = Vol; j >= W[i]; j--) +             { -                  for(k =1; K <= K; k++) the                 { *A[K] = P[j-w[i]][k] +V[i]; $B[K] =P[j][k];Panax Notoginseng                 } -x =1; they =1; +z =1; A                  while(Z <= k && (x <= k | | y <= k))//K-Optimal solution before merging and rebuilding the                 { + //cout << a[x] << "<< b[x] << Endl; -                     if(A[x] >B[y]) $P[J][Z] = A[x], x + +; $                     Else -P[J][Z] = B[y], y++; -                     if(P[j][z]! = p[j][z-1]) thez++; -                 }Wuyi             } the         } -  Wuprintf"%d\n", P[vol][k]); -     } About     return 0; $}
View Code

Dp2_ K Excellent Solution

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.