zoukankan      html  css  js  c++  java
  • 一本通 1270:【例9.14】混合背包

    混合背包

    混合背包模板题。

    #include <iostream>
    #include <cstdio>
    using namespace std;
    //Mystery_Sky
    //
    #define M 100000
    int c[M], w[M];
    int f[M], new_c[M], new_w[M], num[M];
    bool it[M];
    int v, m, tot;
    int main() {
    	scanf("%d%d", &v, &m);
    	for(int i = 1; i <= m; i++) scanf("%d%d%d", &c[i], &w[i], &num[i]);
    	for(int i = 1; i <= m; i++) {
    		if(num[i] == 0) {
    			it[++tot] = 1;
    			new_c[tot] = c[i];
    			new_w[tot] = w[i];
    			continue;
    		}
    		if(num[i] == 1) {
    			new_c[++tot] = c[i];
    			new_w[tot] = w[i];
    			continue;
    		}
    		for(int j = 1; j <= num[i]; j<<=1) {
    			num[i] -= j;
    			new_c[++tot] = c[i] * j;
    			new_w[tot] = w[i] * j;
    		}
    		if(num[i]) {
    			new_c[++tot] = c[i] * num[i];
    			new_w[tot] = w[i] * num[i];
    			num[i] = 0;
    		}
    	}
    	for(int i = 1; i <= tot; i++) {
    		if(it[i]) {
    			for(int j = new_c[i]; j <= v; j++)
    				f[j] = max(f[j], f[j-new_c[i]] + new_w[i]);
    		}
    		else 
    			for(int j = v; j >= new_c[i]; j--)
    				f[j] = max(f[j], f[j-new_c[i]] + new_w[i]);
    	}
    	printf("%d
    ", f[v]);
    	return 0;
    }
    
    唯愿,青春不辜负梦想,未来星辰闪耀
  • 相关阅读:
    移动网络优化
    移动网络架构与数据传输
    移动网络简介与RRC
    CSS之外边距折叠
    网络协议之TLS
    Smarty 模板引擎简介
    FormData介绍
    相对路径与绝对路径
    OAuth2.0
    Redis学习手册(List数据类型)
  • 原文地址:https://www.cnblogs.com/Benjamin-cpp/p/10846655.html
Copyright © 2011-2022 走看看