HDU 1114 Piggy-bank

Source: Internet
Author: User

The first thought of the method is very complex, with a try to hold the mentality submitted, really tle

Then search, is a complete backpack, a lot of do not remember, re-review a bit.

The solution of the first annotation is tle, the second and the third are feasible, but the ideas are different.

#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;const int inf= 0x7fffffff; #define N 10005int dp[n],p[505],w[505];void init () {//memset (dp,-1,sizeof (DP)); for (int i=0;i<n;i++) dp[ i]=inf;dp[0]=0;} int main () {#ifndef online_judgefreopen ("In.txt", "R", stdin), #endifint t,n,e,f;scanf ("%d", &t), while (t--) {init () ; scanf ("%d%d%d", &e,&f,&n), for (int i=1;i<=n;i++) scanf ("%d%d", &p[i],&w[i]);/*for (int i=1;i <=f-e;i++) {//This method is considered directly from the scale of N, and obviously does not use the advantages of DP for (int j=1;j<=n;j++) {for (int k=0;k*w[j]<=i;k++) {if (Dp[i-k*w[j] ]!=inf) dp[i]=min (dp[i],dp[i-k*w[j]]+p[j]*k);}} *//*for (int i=1;i<=n;i++) {////ex-i-1 to pre-I-type for (int j=w[i];j<=f-e;j++) {if (Dp[j-w[i]]!=inf) dp[j]=min (dp[j],dp[ J-w[i]]+p[i]);}} */for (int i=1;i<=f-e;i++) {//weight is I, n kinds of items can reach the minimum value, here do not consider the number of J items, because I is small to large, then each time we calculate the value is automatically followed by the number of k-1 values for (int j=1; j<=n;j++) {if (I-w[j]>=0&&dp[i-w[j]]!=inf) dp[i]=min (Dp[i],dp[i-w[j]]+p[j]);}} if (dp[f-e]==inf) printf ("This is impossible.\ n "), Else printf (" The minimum amount of money in the Piggy-bank is%d.\n ", dp[f-e]);} return 0;}


HDU 1114 Piggy-bank

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.