結果

問題 No.527 ナップサック容量問題
ユーザー rodearodea
提出日時 2018-03-02 19:07:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 778 bytes
コンパイル時間 783 ms
コンパイル使用メモリ 67,696 KB
実行使用メモリ 44,024 KB
最終ジャッジ日時 2023-09-02 13:12:00
合計ジャッジ時間 3,077 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,600 KB
testcase_01 AC 5 ms
5,004 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 5 ms
4,964 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 20 ms
19,412 KB
testcase_06 AC 41 ms
37,820 KB
testcase_07 AC 45 ms
31,636 KB
testcase_08 AC 18 ms
17,300 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 56 ms
37,752 KB
testcase_11 AC 29 ms
27,536 KB
testcase_12 AC 22 ms
21,432 KB
testcase_13 AC 41 ms
37,816 KB
testcase_14 AC 22 ms
17,436 KB
testcase_15 AC 33 ms
23,412 KB
testcase_16 AC 5 ms
4,988 KB
testcase_17 AC 28 ms
21,368 KB
testcase_18 AC 32 ms
23,512 KB
testcase_19 AC 6 ms
7,144 KB
testcase_20 AC 23 ms
17,336 KB
testcase_21 AC 64 ms
44,024 KB
testcase_22 AC 41 ms
29,656 KB
testcase_23 AC 62 ms
41,908 KB
testcase_24 AC 15 ms
11,456 KB
testcase_25 AC 38 ms
29,544 KB
testcase_26 AC 37 ms
27,612 KB
testcase_27 AC 36 ms
27,568 KB
testcase_28 AC 30 ms
23,480 KB
testcase_29 AC 64 ms
43,936 KB
testcase_30 AC 7 ms
7,032 KB
testcase_31 AC 29 ms
21,372 KB
testcase_32 AC 34 ms
25,504 KB
testcase_33 AC 30 ms
21,348 KB
testcase_34 AC 35 ms
25,460 KB
testcase_35 AC 36 ms
25,800 KB
testcase_36 AC 4 ms
4,508 KB
testcase_37 AC 25 ms
19,300 KB
testcase_38 AC 32 ms
23,484 KB
testcase_39 AC 32 ms
23,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<utility>
using namespace std;

int v[110], w[110];
int dp[110][100010]; 

pair<int, int> solve(int n, int V, pair<int, int>P)
{
	P = make_pair(1000000, 0);
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= 100001; j++)
		{
			if (j < w[i]) dp[i][j] = dp[i - 1][j];
			else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);

			if (dp[i][j] == V && i == n)
			{
				P.first = min(P.first, j);
				P.second = max(P.second, j);
			}
		}
	}
	return P;
}

int main()
{
	int n, V;
	pair<int, int> P;

	cin >> n;

	for (int i = 1; i <= n; i++) cin >> v[i] >> w[i];

	cin >> V;

	cout << solve(n, V, P).first << endl;
	if(solve(n, V, P).second != 100001) cout << solve(n, V, P).second << endl;
	else cout << "inf" << endl;
}
0