結果

問題 No.617 Nafmo、買い出しに行く
ユーザー reitetsuoh@gmail.comreitetsuoh@gmail.com
提出日時 2018-05-15 17:25:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,798 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 71,292 KB
実行使用メモリ 175,028 KB
最終ジャッジ日時 2023-09-10 21:03:18
合計ジャッジ時間 2,684 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
16,520 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 22 ms
21,576 KB
testcase_03 AC 52 ms
50,956 KB
testcase_04 AC 5 ms
6,952 KB
testcase_05 AC 73 ms
86,956 KB
testcase_06 AC 137 ms
175,028 KB
testcase_07 AC 133 ms
174,980 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 101 ms
125,672 KB
testcase_11 AC 119 ms
151,512 KB
testcase_12 AC 74 ms
82,208 KB
testcase_13 AC 52 ms
50,568 KB
testcase_14 AC 62 ms
65,612 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//ダイナミック計画法
//ケースに最大の重さはmax_weightであるの最大の価値のものを入れる
//const int MAX_N: 最大N
//const int MAX_W: 重さは最大範囲値
//const vector<int> &weight:重さの配列
//const vector<int> &value:価値の配列
//const int max_weight: 入れられる最大の重さ
//例:重さ   価値
//    5kg    20円
//    4kg    10円
//    3kg    12円
//最大10kgのケースに最大の価値を入れる
//結果: 5kg[20]+3kg[12]=32円
//weight[]={5,4,3}
//value[]={20,10,12}
int DynamicPlanMethod(const int MAX_N,const int MAX_W,vector<int> &weight,const vector<int> &value,const int max_weight)
{
	//ダイナミック計画法で解を求める
	// DPテーブル
	// dp[i][j]はi番目以降の品物から重さの和がj以下なるように選んだときの価値の和の最大値を表す。
	vector<vector<int>> dp(MAX_N+1,vector<int>(MAX_W+1));
	for (int j = 0; j <= MAX_W; j++) {
		dp[MAX_N][j] = 0;
	}
	for (int i = MAX_N - 1; i >= 0; i--) {
		for (int j = 0; j <= max_weight; j++) {
			if (j < weight[i]){
				dp[i][j] = dp[i + 1][j];
			}else{
				dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - weight[i]] + value[i]);
			}
		}
	}
	return dp[0][max_weight];
}
//ダイナミック計画法
int main(int argc, char* argv[])
{
	int N,K;
	cin>>N>>K;
	const int MAX_W=K;
	const int MAX_N=N;
	vector<int> weight(MAX_N);
	vector<int> value(MAX_N);
	int max_weight=K;
	int i;
	int A;
	for (i=0;i<N;i++){
		cin>>A;
		weight[i]=A;
		value[i]=A;
	}
	cout<<DynamicPlanMethod(MAX_N,MAX_W,weight,value,max_weight)<<endl;
	return 0;
}
0