poj-1384 Piggy-Bank

poj-1384 Piggy-Bank 地址:http://poj.org/problem?id=1384

题意:

知道盒子里面的物体的总重量,得到每一种硬币的价格和重量,求最少钱构成盒子物体总重量的钱的数量。

分析:

典型的不限重背包问题。当然维度也是在可以接受的范围, 直接设置dp数组进行min操作。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
const int maxn = 505;
const int max_val = 10000; 

int E,F, n, dp[max_val+5]; 

struct Node{
    int p,w;
}nd[maxn]; 

int cmp(const void* a, const void* b){
    Node* aa = (Node *)a;
    Node* bb = (Node *)b;
    return (bb->w*1.0)/(bb->p*1.0) - (aa->w*1.0)/(aa->p*1.0);
}

int main(){
    freopen("in.txt", "r", stdin); 

    int test_num, i, j;
    scanf("%d", &test_num);
    while(test_num--){
        scanf("%d %d", &E, &F);
        scanf("%d", &n);
        for(i=0; i<n; i++){
            scanf("%d %d", &nd[i].p, &nd[i].w);
        }
        memset(dp, 0x3f3f3f3f, sizeof(dp));
        dp[0] = 0;
        for(i=0; i<n; i++){
            for(j=nd[i].w; j<=(F-E); j++){
                if(dp[j - nd[i].w] != 0x3f3f3f3f ){
                    dp[j] = min(dp[j-nd[i].w]+nd[i].p, dp[j]);
                }
            }
        }
        if(dp[F-E] == 0x3f3f3f3f){
            printf("This is impossible.\n");
        }else{
            printf("The minimum amount of money in the piggy-bank is %d.\n", dp[F-E]);
        }
    }
    return 0;
}
时间: 2024-08-10 17:04:45

poj-1384 Piggy-Bank的相关文章

poj 1384 Piggy-Bank(完全背包)

http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7900 Accepted: 3813 Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income f

POJ 1384 Piggy-Bank 背包DP

所谓的完全背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 其实和一般01背包没多少区别,不过数量可以无穷大,那么就可以利用一个物品累加到总容量结尾就可以了. 本题要求装满的,故此增加个限制就可以了. #include <stdio.h> #include <stdlib.h> #include <string.h> inline int min(int a, int b) { return a < b? a : b; } c

poj 1384 Piggy-Bank(全然背包)

http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7900 Accepted: 3813 Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income f

POJ 1384 Piggy-Bank(完全背包)

http://poj.org/problem?id=1384 题意: 现在有n种硬币,每种硬币有特定的重量cost[i] 克和它对应的价值val[i]. 每种硬币可以无限使用. 已知现在一个储蓄罐中所有硬币的总重量正好为m克, 问你这个储蓄罐中最少有多少价值的硬币? 如果不可能存在m克的情况, 那么就输出" This is impossible.". 分析: 由于每种硬币可以无限使用, 所以是明显的完全背包问题. 本题的限制条件: 硬币总重量正好等于m. 本题的目标条件: 硬币总价值尽

poj 1384 Piggy-Bank (完全背包)

http://poj.org/problem?id=1384 题意:给出一个储蓄罐 的空和满时的重量 再给出n种硬币的 value 和 weight 问满足正好装满罐子时的最小价值是多少 思路 : if(dp[j]>dp[j-w[i]]+v[i]) dp[j]=dp[j-w[i]]+v[i]; #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include&l

POJ 1384 Intervals (线性差分约束,根据不等式建图,然后跑spfa)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1384 Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4841    Accepted Submission(s): 1815 Problem Description You are given n closed, in

POJ 1384 Piggy-Bank 完全背包分析

给定储蓄罐空的和满的重量,有n种硬币,硬币有价值和重量,给出各种硬币的价值p[i]和对应的重量w[i],求储蓄罐里面硬币的最小价值,如果没有符合要求的放硬币的方式,输出 “this is impossible”. 思路: 相当于完全背包求最小值,n中硬币对应n个物体,物体可以取无限次,存储罐里硬币重量(满罐减空罐)相当于背包的体积V. 法一: 直接扩展01背包的方程,用dp[i,v]表示取前i种硬币,存储罐重量最大为v时的最小价值.状态转移方程为:dp[i,v]=min(dp[i-1,v-k*w

ACM Piggy Bank

Problem Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has

POJ 1384(完全背包)

题目大意:给出罐子的初始重量和装满钱币后的重量,然后给出几组钱币的类型(币值和重量),问装满这个罐子后最少可以放多少钱. 思路:主要是注意初始化,然后写出状态方程,dp[v]=min(dp[v],dp[v-w[j]]+c[j]); 代码如下: #include <iostream>#include <stdio.h>#include <memory.h>#include <math.h>#define MAX 1000000using namespace s

poj 1384

Piggy-Bank Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member