Hdu1114 piggy-Bank (DP basic full backpack)

Source: Internet
Author: User

Link: piggy-bank

I know the weight and current weight of a pig when the piggy bank is empty. I know the weight and value of several types of money. The pig contains several portions of money, evaluate the minimum value of money in pig.

Question:

DP, full backpack.

G [J] indicates the minimum cost of the Component Weight J.

G [J] = min (G [J], G [J-W [I] + V [I])

A full backpack can be used multiple times, so the cycle of J is coming.

 

Code:

 1 #include<cstdio> 2 #include<cmath> 3 #include<iostream> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<map> 8 #include<set> 9 using namespace std;10 #define ll __int6411 #define usint unsigned int12 #define mz(array) memset(array, 0, sizeof(array))13 #define minf(array) memset(array, inf, sizeof(array))14 #define REP(i,n) for(int i=0;i<(n);i++)15 #define FOR(i,x,n) for(int i=(x);i<=(n);i++)16 #define RE  freopen("1.in","r",stdin)17 #define WE  freopen("1.out","w",stdout)18 19 const int maxn=555;20 const int maxv=10001;21 const int inf=0x3f3f3f3f;22 23 int w[maxn],v[maxn];24 int g[maxv];25 26 int main() {27     int i,j,k,ans,t;28     int e,f,n;29     scanf("%d",&t);30     while(t--) {31         scanf("%d%d%d",&e,&f,&n);32         for(i=0; i<n; i++)33             scanf("%d%d",&v[i],&w[i]);34         minf(g);35         g[0]=0;36         REP(i,n)37             FOR(j,w[i],maxv-1)38                 g[j]=min(g[j],g[j-w[i]]+v[i]);39         if(g[f-e]==inf) printf("This is impossible.\n");40         else printf("The minimum amount of money in the piggy-bank is %d.\n",g[f-e]);41     }42     return 0;43 }
View code

 

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.