HDU (1114)--piggy-bank (full backpack)

Source: Internet
Author: User

Well.. Recently practicing the basic DP

This problem is quite simple (haha), but I just want to say that there is a detail here.

First Test instructions:

There is a T-set sample, and then give the start weight of the piggy bank e, end the weight f (that is, when it is full of change), and then give you a number n, which means that there are now n kinds of coins.

Then the next n lines, each line has two numbers p,w,p represents the value of this type of change, W represents the weight of this type of change, the amount of change is unlimited.

Then you have to output the case where the current weight is f (i.e. the weight is just f) and the minimum amount of change is required.

Ideas:

Obviously, this is obviously a complete backpack. But the only thing to be aware of here is the initialization, because we're going to completely fill the piggy bank, so we initialize the dp[0]=0, and then because we're asking for the least amount of change, the rest is initialized to infinity.

In addition to the above situation, I believe that the rest is not difficult.

When Dp[v]==inf, it means that there is no solution at this time.

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < set> #include <map> #include <math.h>using namespace std; #define MAXN 10010#define inf 99999999int DP[MAXN ];int V[555],w[555];int Main () {int t;scanf ("%d", &t), while (t--) {int e,f;scanf ("%d%d", &e,&f); int v=f-e; int n;scanf ("%d", &n), for (int i=1;i<=n;i++) {scanf ("%d%d", &v[i],&w[i]);} Fill (dp,dp+maxn+1,inf);DP [0]=0;for (int i=1;i<=n;i++) {for (int j=w[i];j<=v;j++) {dp[j]=min (dp[j],dp[j-w[i]]+v [i]);}} if (dp[v]==inf) printf ("This was impossible.\n"), Else printf ("The minimum amount of money in the Piggy-bank is%d.\n", Dp[v]) ;}} /*310 11021 130 50*/



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU (1114)--piggy-bank (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.