結果

問題 No.617 Nafmo、買い出しに行く
ユーザー ohreitetsuohreitetsu
提出日時 2018-05-22 21:49:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,775 bytes
コンパイル時間 867 ms
コンパイル使用メモリ 72,940 KB
実行使用メモリ 175,200 KB
最終ジャッジ日時 2023-09-11 01:22:36
合計ジャッジ時間 2,631 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
16,444 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 22 ms
21,552 KB
testcase_03 AC 50 ms
50,928 KB
testcase_04 AC 6 ms
7,256 KB
testcase_05 AC 76 ms
86,812 KB
testcase_06 AC 137 ms
175,200 KB
testcase_07 AC 135 ms
174,940 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 103 ms
125,716 KB
testcase_11 AC 119 ms
151,168 KB
testcase_12 AC 75 ms
82,432 KB
testcase_13 AC 54 ms
50,432 KB
testcase_14 AC 63 ms
65,652 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 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_N=N;
	const int MAX_W=K;
	int max_weight=K;
	int i;
	vector<int> weight(MAX_N);
	vector<int> value(MAX_N);
	for (i=0;i<MAX_N;i++){
		cin>>weight[i];
		value[i]=weight[i];
	}
	int w= DynamicPlanMethod(MAX_N,MAX_W,weight,value,max_weight);
	cout<<w<<endl;
	return 0;
}
0