POJ 1384 (full backpack)

Source: Internet
Author: User

The main question: give the initial weight of the jar and the weight after filling it, then give the types of coins (currency and weight), and ask how much you can put at least after filling the jar.

Idea: Mainly pay attention to initialize, then write the state equation, Dp[v]=min (dp[v],dp[v-w[j]]+c[j]);

The code is as follows:

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#define MAX 1000000
using namespace Std;
int dp[10005];
int n,w1,w2,t,c[10005],w[10005];
int main ()
{
scanf ("%d", &t);
while (t--) {
Memset (Dp,0,sizeof (DP));
Memset (C,0,sizeof (c));
Memset (W,0,sizeof (w));
scanf ("%d%d", &AMP;W1,&AMP;W2);
scanf ("%d", &n);
for (int i=0;i<n;i++) {
scanf ("%d%d", &c[i],&w[i]);
}
for (int i=0;i<=w2-w1;i++) {
Dp[i]=max;
}
dp[0]=0;
for (int v=0;v<=w2-w1;v++) {
for (int j=0;j<=n;j++) {
if (V>=w[j])
Dp[v]=min (Dp[v],dp[v-w[j]]+c[j]);
}
}
if (Dp[w2-w1]==max) printf ("This is impossible.\n");
else printf ("The minimum amount of money on the Piggy-bank is%d.\n", DP[W2-W1]);
}
return 0;
}

POJ 1384 (full 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.