Dynamic planning: Hdu1712-acboy needs your help (packet knapsack problem) __ Dynamic programming-Simple DP

Source: Internet
Author: User
Acboy needs your Help Time limit:1000/1000ms (java/other) Memory limit:32768/32768k (java/other) Total Submission (s): 4 accepted Submission (s): 1Problem Description Acboy has N courses this term, and him plans to spend in most M days on study. Of course,the profit he would gain from different course to on the "the" and "depending on it." How to arrange the "M" for the "N courses to maximize" profit?

Input the input consists of multiple data sets. A data set starts with a line containing two positive integers n and M, n are the number of courses, M is the the days Acboy ha S. Next follow a matrix a[i][j], (1<=i<=n<=100,1<=j<=m<=100). A[I][J] Indicates if Acboy spend J days in ith course he'll get profit of value a[i][j]. N = 0 and M = 0 ends the input.
Output for each data set, your program should output a line which contains the number of the max profit Acboy would gain.
Sample Input
2 2 1 2 1 3 2 2 2 1 2 1 2 3 3 2 1 3 2 1 0-0
Sample Output
3 4 6

Solution: 1, this is the first time to see the packet knapsack problem, packet knapsack problem, a requirement is that in many groups, each time only from a group to select a number, and then get the best solution from multiple groups. This is different from the 0-1 knapsack problem, the multiple backpack and the full knapsack problem. Three kinds of basic knapsack problem (0-1, multiple, complete) can be regarded as a group, is randomly selected in a single group, to find the best solution. So you have to be able to differentiate when you encounter multiple packs of knapsack problems.
2, because many groups of knapsack problem compared to three kinds of basic knapsack problem, one more condition-group number. So add a layer of loops, the number of groups. Formula
For all groups K for
    v = v ... 0
        for all I groups K
            F[v]=max{f[v],f[v-c[i]]+w[i]}

Note that in the three-tier cycle the capacity must be placed in the second tier, otherwise it becomes a 0-1 knapsack problem.
* *
before the time to do this problem to read a person's blog, he wrote the formula to the reverse, V ... V written to the inner layer, has
been WA, is also silent, in this blog to correct this error
*

/#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+100;
const int MAXN2 = 1E2+10;
int MAPS[MAXN2][MAXN2];
int DP[MAXN];
struct NUM
{
    int va,cost;
} NUM[MAXN];
int main ()
{
    int n,m;
    while (scanf ("%d%d", &n,&m) && n+m)
    {
        memset (DP));
        for (int i=1;i<=n;i++)
            for (int j=1;j<=m;j++)
                scanf ("%d", &maps[i][j]);

        for (int i=1;i<=n;i++) for (int
            j=m;j>=1;j--) for
                (int k=1;k<=j;k++)
                    dp[j] = max (dp[j],dp[j-k) +maps[i][k]);
        printf ("%d\n", Dp[m]);
    }




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.