多重背包--java

多重背包

有N种物品和一个容量为V的背包。第i种物品最多有n[i]件可用,每件费用是c[i],价值 是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大
母函数的思想也是如此 给你 价值, 物品数量的限制, 然后凑,

hdu2191

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {

            int n = sc.nextInt();
            int m = sc.nextInt();

            int v[] = new int[101];
            int w[] = new int[101];
            int num[] = new int[101];
            for (int i = 0; i < m; i++) {
                v[i] = sc.nextInt();
                w[i] = sc.nextInt();
                num[i] = sc.nextInt();
            }

            int dp[] = new int[10001];
            for (int i = 0; i <= m; i++)
                dp[i] = 0;
            for (int i = 0; i < m; i++) {
                for (int k = 0; k <= num[i]; k++) {
                    for (int j = n; j >= v[i]; j--) {
                        if (dp[j] < dp[j - v[i]] + w[i]) {
                            dp[j] = dp[j - v[i]] + w[i];

                        }
                    }
                }
            }

            System.out.println(dp[n]);
        }
    }
}

原文地址:https://www.cnblogs.com/ls-pankong/p/10490891.html

时间: 2024-10-05 07:30:40

多重背包--java的相关文章

c++01背包一维数组版及java多重背包

#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=1010; int c[maxn],w[maxn]; int dp[maxn]; int main(){ int n,v; cin>>n>>v; for(int i=1;i<=n;i++) cin>>c[i]; for(int i=1;i<=n;i++) cin>>w[i];

hdu 2079 选课时间(题目已修改,注意读题) 多重背包

选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3162    Accepted Submission(s): 2472 Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别

Holding Bin-Laden Captive!(1.多重背包 2.母函数)

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41 Accepted Submission(s): 29   Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disap

HDU1171--Big Event in HDU(多重背包)

Big Event in HDU   Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1139 Accepted Submission(s): 444 Problem Description Nowadays, we all know that Computer College is the biggest department in HD

HDU 2191 多重背包

悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22865    Accepted Submission(s): 9661 Problem Description 急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市

HDU 2844 二进制优化的多重背包

Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11596    Accepted Submission(s): 4634 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One

HDU 1059 多重背包+二进制优化

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16909    Accepted Submission(s): 4729 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

Big Event in HDU(多重背包套用模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 28483    Accepted Submission(s): 10027 Problem Description Nowadays, we all know

HDU 2159 FATE (二维多重背包)

FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9352    Accepted Submission(s): 4413 Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级.现在的问