PACKING(揹包+O2/O3氧化)


問題 K: PACKING

問題 K: PACKING

時間限制: 1 Sec  內存限制: 128 MB
提交: 233  解決: 19
[提交] [狀態] [命題人:admin]

 

題目描述

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).  
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. 

 

輸入

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. 

 

樣例輸入

複製樣例數據

2
4 9 4
3 4 5 6
5 7 9 10
6 9 11
3 4 5 6 3 4
2 3 4 5 3 3

樣例輸出

Problem 1: 22
Problem 2: 16

 兩個揹包,體積分別爲w1,w2,求最大價值

最簡單的思路就是搜索唄,每件物品只有三種決策:放到1,放到2,不放

 

int dfs(int id,int x,int y){
    if(id>n) return 0;
    int ans=dfs(id+1,x,y);
    if(x>=a[id].w) ans=max(ans,a[id].v+dfs(id+1,x-a[id].w,y));
    if(y>=a[id].w) ans=max(ans,a[id].v+dfs(id+1,x,y-a[id].w));
    return ans;
} 

但是沒辦法記憶化啊,數組開不了那麼大,那就改寫成遞推,第一維滾動優化

還是TLE

dyt大佬學弟說max用三目運算符加O2(O3)優化就能過 ,果然三目運算符和吸氧一個也不能少(快讀沒用)

#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
int dp[2][1005][1005];
int w[105],v[105];
int n,w1,w2;
inline int read()
{
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
int main(){
    int T;
    int cas=0;
    T=read(); 
    while(T--){
    	n=read();
    	w1=read();
    	w2=read();
		for(int j=0;j<=w1;j++){
			for(int k=0;k<=w2;k++){
				dp[0][j][k]=0;
			}
		}
    	for(int i=1;i<=n;i++) w[i]=read();
    	for(int i=1;i<=n;i++) v[i]=read();
    	int p=0;
    	for(int i=1;i<=n;i++){
    		p^=1;
    		for(int j=0;j<=w1;j++){
    			for(int k=0;k<=w2;k++){
    				dp[p][j][k]=dp[p^1][j][k];
    				if(j>=w[i]) dp[p][j][k]=max(dp[p][j][k],dp[p^1][j-w[i]][k]+v[i]);
    				if(k>=w[i]) dp[p][j][k]=max(dp[p][j][k],dp[p^1][j][k-w[i]]+v[i]);
				}
			}
		}
		printf("Problem %d: %d\n",++cas,dp[p][w1][w2]);
	}
	return 0;
}

用java那比C++長的時長也能過 

import java.util.*;
import java.math.*;
 
public class Main {
    static int n,w1,w2; 
    static int w[]=new int[105];
    static int v[]=new int[105];
    static int dp[][][]=new int[2][1005][1005];
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();
        for(int cas=1;cas<=T;cas++) {
            n=sc.nextInt();
            w1=sc.nextInt();
            w2=sc.nextInt();
            for(int j=0;j<=w1;j++){
                for(int k=0;k<=w2;k++){
                    dp[0][j][k]=0;
                }
            }
            for(int i=1;i<=n;i++) w[i]=sc.nextInt();
            for(int i=1;i<=n;i++) v[i]=sc.nextInt();
            int p=0;
            for(int i=1;i<=n;i++){
                p^=1;
                for(int j=0;j<=w1;j++){
                    for(int k=0;k<=w2;k++){
                        dp[p][j][k]=dp[p^1][j][k];
                        if(j>=w[i]) dp[p][j][k]=Math.max(dp[p][j][k],dp[p^1][j-w[i]][k]+v[i]);
                        if(k>=w[i]) dp[p][j][k]=Math.max(dp[p][j][k],dp[p^1][j][k-w[i]]+v[i]);
                    }
                }
            }
            System.out.println("Problem "+cas+": "+dp[p][w1][w2]);
        }
    }  
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章