zoukankan      html  css  js  c++  java
  • 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; 
    }
  • 相关阅读:
    中文转码问题总结
    Linux命令总结
    Maven实战系列文章目录
    JXL API总结
    docker 中安装mysql8之后无法远程连接的问题caching-sha2-password
    springboot查数据并以csv格式现在到本地
    aop
    java.lang.ClassNotFoundException: org.aspectj.lang.JoinPoint
    shiro框架中获取username、ip等信息
    cron
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/5811805.html
Copyright © 2011-2022 走看看