zoukankan      html  css  js  c++  java
  • hdu2191 悼念512汶川大地震遇难同胞——珍惜如今,感恩生活 (这个仅仅是题目名字) (多重背包)

    版权声明:本文为博主原创文章,未经博主同意不得转载。

    https://blog.csdn.net/svitter/article/details/24904105

    本文出自:http://blog.csdn.net/svitter


    原题:http://acm.hdu.edu.cn/showproblem.php?

    pid=2191

    题意:多重背包问题。

    转换成为01背包解。多重背包转化为01背包的关键在于把件数从总体中孤立出来作为一个新的个体。也就是说无论分类,有多少件就有多少种。

    AC代码:

    //============================================================================
    // Name        : 动态规划.cpp
    // Author      :
    // Version     :
    // Copyright   : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================
    
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    
    using namespace std;
    #define max(a,b) a > b ? a : b
    
    struct Rice{
    	int pr;
    	int val;
    };
    
    int f[110];
    Rice r[4000];
    
    void ace(){
    	//work point
    	int t, i , j ,k;
    
    	//num
    	int n, m;
    	int p, h, c;
    	int num;//转换为01背包后的数量
    
    	scanf("%d", &t);
    	while(t--){
    		memset(f, 0, sizeof(f));
    		scanf("%d%d", &n, &m);
    
            num = 0;
    		for(i = 0; i < m; i++){
    			scanf("%d%d%d", &p, &h, &c);
    			for(j = num; j < num + c; j++){
    				r[j].pr = p;
    				r[j].val = h;
    			}
    			num = j;
    		}
    
    		//背包最小额
    		for(i = 0; i < num; i++){
    			for(j = n; j >= r[i].pr; j--){
    				f[j] =  max(f[j], f[j - r[i].pr] + r[i].val);
    			}
    		}
    
    		printf("%d
    ", f[n]);
    	}
    }
    
    int main() {
    	ace();
    	return 0;
    }
    


查看全文
  • 相关阅读:
    从零开始学架构(三)UML建模
    【网址收藏】博客园主题美化
    完美解决国内访问GitHub速度太慢的难题
    SpringBoot+slf4j线程池全链路调用日志跟踪 二
    SpringBoot+slf4j实现全链路调用日志跟踪 一
    2021年java最全面试宝典【核心知识整理】
    [中级]系统集成项目管理工程师历年真题及参考答案
    线程池ThreadPoolExecutor最全实战案例
    大厂git分支管理规范:gitflow规范指南
    IdentityServer4
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10686127.html
  • Copyright © 2011-2022 走看看