• 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.
    ");
            }else{
                printf("The minimum amount of money in the piggy-bank is %d.
    ", dp[F-E]);
            }
        }
        return 0; 
    }
  • 相关阅读:
    Dijkstra(单队优化)
    最短路(hdu4725)(建点巧妙)
    次小生成树
    2019暑假集训DAY1(problem3.play)(单调栈思想)
    平衡树
    线段树区间加乘(模板)
    杜恩德的新博客,都来看看-duende99
    oracle 11g如何完全卸载
    如何连接别人电脑上的Oracle数据库--duende99
    Java中的Filter过滤器
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/5811805.html
Copyright © 2020-2023  润新知