UPC packing(打包)(组合背包)

It was bound to happen.  Modernisation has reached the North Pole.  Faced with escalating costs for feeding Santa Claus and the reindeer, and serious difficulties with security, NP Management has decided to do away with the traditional sleigh and adopt delivery by drone (magic, superfast drone).  

这似乎一定会发生。现代化的脚步已经踏进了NP这个地区。面对着回馈圣诞老人和驯鹿的不断高升的成本,以及在安全方面的棘手问题,NP管理部已经决定停用传统的驯鹿而改用无人机运送货物。


Lack of investment capital means that the new system will start small, and hopefully grow in the years to come.  For the first test run in 2017 there will be only two drones and they will have limited carrying capacity.  PR is, of course, all important.  There will be disappointment, and NP Management has decided to focus on delivering only the most expensive toys to the richest children, so as to focus the worst of the disappointment on those who have the greatest experience of coping (the poor). 
Choosing the presents to deliver is your problem.  You are being asked to develop an algorithm to select the cargo to deliver, given weight limits for each of the drones and a list of candidate presents with weights and values.  Your goal is to maximise the value of gifts delivered. 

缺少资本的注入使得这个新系统的初始规模必然很小,以及可能在将来会成长。对于2017年开始的第一次测试只会有两架无人机并且他们只有有限的搭载容量,PR(公关)自然是很重要的。令人失望的是,NP管理部决定只运送最昂贵的玩具给最富裕的孩子们,这对于和穷人打惯交道的员工来说也是极大的失望。选择运送的礼物是你的任务。你被要求开发一个算法去选择寄送的货物。给定无人机承载量和货物的信息,你的任务是去最大化运送的礼物的最大值。

输入

Input will consist of a series of problems.  The first line of the input holds a single integer P being the number of problems.  Then for each problem there will be three lines of input.  The first line holds three integers:  N (1 <= N <= 100) being the number of candidate presents;  W1 and W2 (1 <= W1, W2 <= 1000) being the weight limits of the two drones respectively.  The second line holds N integers (1 <= wi  <= 100) being the weights of each of the candidate presents and the third line holds N integers (1 <= vi  <= 100) being the values of the presents (in thousand dollar units).  All lines are formatted with single spaces between numbers and no leading or trailing spaces. 

 

输出

For each problem your program should output one line with the text “Problem “ and the number of the problem (counting from 1) followed by a colon, a space and the total value of presents shipped by the drone pair.

这是一道组合背包的模板题。 (就是综合枚举多个背包的状态)

由于C++可能存在卡常数问题,所以双倍时间的JAVA就是一个不错的选择。

import java.util.*;
import java.math.*;
import java.text.*;
import java.io.*;
public class Main
{
    public static void main(String args[])throws IOException
    {
        BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
    //缓存输入流
        String tmp=input.readLine();
        int t=Integer.parseInt(tmp);
        for(int p=1;p<=t;p++)
        {
            tmp=input.readLine();
            String tmps[]=tmp.trim().split(" ");
            int n,limit1,limit2;
            n=Integer.parseInt(tmps[0]);
            limit1=Integer.parseInt(tmps[1]);
            limit2=Integer.parseInt(tmps[2]);
            //System.out.println(n+" "+limit1+" "+limit2);
            int weight[]=new int[50000];
            int value[]=new int[50000];
            tmp=input.readLine();
            tmps=tmp.trim().split(" ");
            for(int i=1;i<=n;i++)
                weight[i]=Integer.parseInt(tmps[i-1]);
            tmp=input.readLine();
            tmps=tmp.trim().split(" ");
            for(int i=1;i<=n;i++)
                value[i]=Integer.parseInt(tmps[i-1]);
            // for(int i=1;i<=n;i++)
            //     System.out.print(value[i]+" ");
            int status[][]=new int[2000][2000];
            for(int i=1;i<=limit1;i++)
                for(int j=1;j<=limit2;j++)
                    status[i][j]=-1;
            status[0][0]=0;
            for(int i=1;i<=n;i++)
            {
                for(int j=limit1;j>=0;j--)//第一个背包(这里不能把当前货物质量作为下限,因为会影响下面的枚举)
                {
                    for(int k=limit2;k>=0;k--)//第二个背包
                    {
                        if(j>=weight[i])
                            status[j][k]=Math.max(status[j][k],status[j-weight[i]][k]+value[i]);
                        if(k>=weight[i])
                            status[j][k]=Math.max(status[j][k],status[j][k-weight[i]]+value[i]);
                    }
                }
            }
            int ans=0;
            for(int i=0;i<=limit1;i++)
                for(int j=0;j<=limit2;j++)
                    ans=Math.max(ans,status[i][j]);
            System.out.println("Problem "+p+": "+ans);
        }
        input.close();
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值