結果

問題 No.527 ナップサック容量問題
ユーザー lapilapi
提出日時 2019-04-06 10:57:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,324 bytes
コンパイル時間 824 ms
コンパイル使用メモリ 102,176 KB
実行使用メモリ 42,880 KB
最終ジャッジ日時 2023-09-05 21:38:42
合計ジャッジ時間 2,828 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,984 KB
testcase_01 AC 3 ms
5,432 KB
testcase_02 AC 3 ms
4,520 KB
testcase_03 AC 4 ms
5,320 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 14 ms
19,740 KB
testcase_06 AC 27 ms
38,168 KB
testcase_07 AC 23 ms
32,024 KB
testcase_08 AC 13 ms
17,884 KB
testcase_09 AC 2 ms
4,420 KB
testcase_10 AC 27 ms
38,164 KB
testcase_11 AC 19 ms
27,952 KB
testcase_12 AC 15 ms
21,836 KB
testcase_13 AC 27 ms
38,172 KB
testcase_14 AC 11 ms
17,804 KB
testcase_15 AC 17 ms
23,844 KB
testcase_16 AC 4 ms
5,316 KB
testcase_17 AC 15 ms
21,788 KB
testcase_18 AC 17 ms
23,888 KB
testcase_19 AC 4 ms
7,516 KB
testcase_20 AC 12 ms
17,692 KB
testcase_21 AC 31 ms
42,880 KB
testcase_22 AC 21 ms
30,072 KB
testcase_23 AC 30 ms
42,288 KB
testcase_24 AC 8 ms
11,548 KB
testcase_25 AC 20 ms
30,056 KB
testcase_26 AC 19 ms
27,932 KB
testcase_27 AC 19 ms
27,936 KB
testcase_28 AC 16 ms
23,824 KB
testcase_29 AC 30 ms
42,432 KB
testcase_30 AC 5 ms
7,452 KB
testcase_31 AC 15 ms
21,804 KB
testcase_32 AC 17 ms
25,872 KB
testcase_33 AC 15 ms
21,788 KB
testcase_34 AC 18 ms
25,888 KB
testcase_35 AC 17 ms
25,996 KB
testcase_36 AC 3 ms
5,252 KB
testcase_37 AC 14 ms
19,788 KB
testcase_38 AC 17 ms
23,820 KB
testcase_39 AC 18 ms
23,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <numeric>
#include <regex>
#include <tuple>
#include<iomanip>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
#define MOD 1000000007 // 10^9 + 7
#define INF 1000000000 // 10^9
#define LLINF 1LL<<60


int v[101], w[101];
int dp[101][100001]; // dp[i][j] : i個目までの商品で価値jを作るときの最小の重さ


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

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

	for (int i = 0; i <= N; i++) {
		for (int j = 0; j <= 100000;j++) dp[i][j] = INF;
	}
	
	

	dp[0][0] = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j <= 100000; j++) {
			if (j + v[i + 1] <= 100000) dp[i + 1][j + v[i + 1]] = min(dp[i + 1][j + v[i + 1]], dp[i][j] + w[i + 1]);
			dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
		}
	}

	int ansmin = dp[N][V];
	
	// dp[N][V+1]~dp[N][100000]までの最小値を求める
	int ans = INF;
	for (int j = V + 1; j <= 100000; j++) {
		ans = min(ans, dp[N][j]);
	}
	
	if (ansmin == 0) cout << 1 << endl;
	else cout << ansmin << endl;


	if (ans < INF) cout << ans - 1 << endl;
	else cout << "inf" << endl;


	return 0;
}
0