HDU 5234 Happy Birthday "Dynamic Planning"

Source: Internet
Author: User

Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=5234

Test instructions: Gives a n*m matrix and an integer k, which requires starting from the upper left corner only to the right

Or go to the left to start the traversal, on the way you can choose to add the current position of the number or not add the current bit

The number of the final addition and the maximum value of less than K.

Analysis: This problem is a general backpack, but did not study backpack and DP, just by

This feeling wrote the state transfer equation, WA several times a, feel DP is a very good thing, only

It is his own graph theory has not learned it has never done this, in fact, can learn.

First of all, let's talk about the first part of the backpack nine, the original is this:

There are n items and a backpack with a capacity of V. The volume of article I is c[i], the value is w[i]. The sum of the values is maximized by solving which items are loaded into the backpack.
State transition equation:
F[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}
This equation is very important, and basically all the equations for knapsack-related problems are derived from it.
Pseudo code:
For I=1..N
For V=v. 0
F[v]=max{f[v],f[v-c[i]]+w[i]};
If you do not put the article I item, then the problem is converted to "the first I-1 items into a backpack with a capacity of V",
Value of f[i-1][v];
If you put the article I item, then the problem translates into "The first I-1 items into the remaining capacity of v-c[i] in the backpack",
The maximum value that can be obtained at this time is f[i-1][v-c[i]] plus the value obtained by placing the item in article I w[i].


② Example Two:
Drug harvesting

time limit:   1000ms       Memory Limit:    65535kb 
submissions:   155       Accepted:   


Description Chen Chen is a gifted child, his dream is to become the world's greatest physician. To this end, he wanted to worship the most prestigious physician in the vicinity as a teacher. In order to judge his qualifications, the physician gave him a difficult problem. The physician took him to a cave that was full of herbs, and said to him, "children, there are some different herbs in this cave, and it takes time to take each strain, and each plant has its own value." I will give you some time, during which time you can pick up some herbs. If you are a smart kid, you should be able to get the total value of the herbs that are picked up. "
If you are Chen Chen, can you finish this task?"    The first line of the
input input has two integers t (1 <= T <= 1000) and M (1 <= M <= 100), separated by a space, T represents the total amount of time that can be used to pick up the medicine, and M represents the number of herbs in the cave. The next M-line consists of two integers from 1 to 100 (including 1 and 100), each representing the time of picking a herbal herb and the value of the herb.   The
output output includes one row, which contains only an integer that represents the maximum total value of the herbs that can be picked up within the prescribed time. &NBSP
Sample Input
3
1
1 2
Sample Output
3

#include <iostream>
# include<cstring>
# define MAX (A, b) a>b? A:B
using namespace std;
int main ()
{


    int dp[101][1001],m,t,w[101],val[101],i,j;
    cin>>t>>m;
    for (i=1;i<=m;i++)
        Cin>>w[i]>>val[i];
    memset (Dp,0,sizeof (DP));
    for (i=1;i<=m;i++)
     for (j=0;j<=t;j++)//j equivalent to the above-mentioned V-c[i]
         {
    if (J>=w[i])
        Dp[i][j]=max (dp[i-1][j],dp[i-1][ J-w[i]]+val[i]);//release vessel is not put the choice
    else dp[i][j]=dp[i-1][j];
     }
     cout<<dp[m][t]<<endl;
     return 0;
}

And this question and above should be the same, but two-dimensional, that the wood state transfer equation can be:DP [i][j][l]=max{dp[i-1][j][l-map[i][j]]+map[i][j],dp[i][j-1][l-map[i][j]]+map[i][j],dp[i-1][j][l],dp[j][ I-1][L]}Offer code:#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace Std;
const int MAXN=100+10;
int MAP[MAXN][MAXN],DP[MAXN][MAXN][MAXN];
int Max (int x,int y)
{
Return x>y?x:y;
}
int main ()
{
int n,m,k;
int l1,l2,l3,l4;
while (~SCANF ("%d%d%d", &n,&m,&k))
{
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
scanf ("%d", &map[i][j]);
Memset (Dp,0,sizeof (DP));
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
for (int l=0;l<=k;l++)
{
l1=l2=l3=l4=0;
L1=DP[I-1][J][L];
L2=DP[I][J-1][L];
if (L>=map[i][j])
{
L3=DP[I-1][J][L-MAP[I][J]]+MAP[I][J];
L4=DP[I][J-1][L-MAP[I][J]]+MAP[I][J];
Dp[i][j][l]=max (Max (L1,L2), Max (L3,L4));
}
Else
Dp[i][j][l]=max (L1,L2);
}
printf ("%d\n", Dp[n][m][k]);
}
return 0;
}

HDU 5234 Happy Birthday "Dynamic Planning"

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.