結果

問題 No.2364 Knapsack Problem
ユーザー HIcoderHIcoder
提出日時 2023-07-05 00:13:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 3,000 ms
コード長 2,657 bytes
コンパイル時間 964 ms
コンパイル使用メモリ 97,764 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-25 23:27:38
合計ジャッジ時間 2,048 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<ll,int> P;
typedef pair<int,P> PP;
const ll MOD=1e9+7;

int main(){
    int N,M,W;
    cin>>N>>M>>W;

    vector<ll> A(N),B(N),C(M),D(M);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }
    for(int i=0;i<N;i++){
        cin>>B[i];
    }

    for(int i=0;i<M;i++){
        cin>>C[i];
    }
    for(int i=0;i<M;i++){
        cin>>D[i];
    }

    vector<vector<ll>> Weight((1<<N),vector<ll>(1<<M,0));

    for(int Sn=0;Sn<(1<<N);Sn++){
        
        for(int Sm=0;Sm<(1<<M);Sm++){
            ll sumw=0;

            for(int i=0;i<N;i++){
                if((Sn>>i)&1){
                    sumw+=A[i];
                }
            }


            for(int i=0;i<M;i++){
                if((Sm>>i)&1){
                    sumw-=C[i];
                }
            }

            Weight[Sn][Sm]=sumw;
        }
    }

    vector<vector<ll>> dp((1<<N),vector<ll>(1<<M,-INF));

    dp[0][0]=0;

    for(int Sn=0;Sn<(1<<N);Sn++){
        for(int Sm=0;Sm<(1<<M);Sm++){

            if(Weight[Sn][Sm]<0 || Weight[Sn][Sm]>W) continue;
            if(dp[Sn][Sm]==-INF) continue;

            
            for(int i=0;i<N;i++){
                
                if( ((Sn>>i)&1) ==0  && 0<=(Weight[Sn][Sm] + A[i]) && (Weight[Sn][Sm] + A[i])<=W ){

                    dp[Sn|(1<<i)][Sm]=max(dp[Sn|(1<<i)][Sm],dp[Sn][Sm]+B[i]);
                
                }
                
            }


            for(int j=0;j<M;j++){
                
                if( ((Sm>>j&1)) ==0  && 0<=(Weight[Sn][Sm] - C[j]) && (Weight[Sn][Sm] - C[j])<=W ){

                    dp[Sn][Sm|(1<<j)]=max(dp[Sn][Sm|(1<<j)],dp[Sn][Sm]-D[j]);
                
                }
                
            }

            /*
            for(int i=0;i<N;i++){

                for(int j=0;j<M;j++){

                    if( ((Sn>>i)&1)==0 && ((Sm>>j)&1)==0  ){

                
                        if( 0<=(Weight[Sn][Sm] + A[i]-C[j]) && (Weight[Sn][Sm] + A[i]-C[j])<=W ){

                            dp[Sn|(1<<i)][Sm|(1<<j)]=max(dp[Sn|(1<<i)][Sm|(1<<j)],dp[Sn][Sm]+B[i]-D[j]);
                        

                        }

                    }
                }
                
            }
            */

          

        }
    }

    ll ans=-INF;
    for(int Sn=0;Sn<(1<<N);Sn++){
        for(int Sm=0;Sm<(1<<M);Sm++){
            ans=max(ans,dp[Sn][Sm]);
        }
    }

    cout<<ans<<endl;

}
0