hdu1114 Piggy-bank (Full backpack)

Source: Internet
Author: User


http://acm.hdu.edu.cn/showproblem.php?pid=1114

Test instructions: The piggy bank can put some small money into it, but it doesn't know how much money is in it after a long time, unless you break it. Now given the weight of the empty jar and the maximum capacity, and then give the value and weight of each coin, we have to confirm the minimum amount of money in the jar without breaking it.


Idea: very close to life. There is no limit to the number of coins, so it is a complete backpack. The condition must be filled to find the minimum value, then the initialization becomes full (backpack ix) and Max becomes min. If the full-capacity DP value is not changed after the item has been traversed, the coins cannot form this capacity. Because of the data 10000, so there is no two-dimensional.


#include <stdio.h> #include <algorithm> #include <stdlib.h> #include <string.h> #include <

Iostream> using namespace std;

typedef long Long LL;
const int N = 10005;

const int INF = 0X3F3F3F3F;

int dp[n];
    int main () {//Freopen ("In.txt", "R", stdin);
    int T, V, N, EMP, fil;
    int cost[n], weight[n];
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &emp, &fil); 
        scanf ("%d", &n);//number of items for (int i = 1; I <= n; i++) scanf ("%d%d", &weight[i], &cost[i]);
        V = fil-emp;
        Dp[0] = 0;//filled for (int i = 1; I <= V; i++) dp[i] = INF; for (int i = 1, i <= N; i++) for (int j = cost[i]; j <= V; j + +) {Dp[j] = min (
            DP[J], dp[j-cost[i]]+weight[i]);
        }/* for (int i = 0; I <= V; i++) printf ("%d", dp[i]); printf ("\ n"); */if (dp[v] = = INF) printf ("This is impossible.\n ");
    else printf ("The minimum amount of money on the Piggy-bank is%d.\n", dp[v]);
} return 0;
 }


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.