結果

問題 No.527 ナップサック容量問題
ユーザー かにかに
提出日時 2017-07-11 20:58:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,088 bytes
コンパイル時間 2,186 ms
コンパイル使用メモリ 198,148 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 17:05:04
合計ジャッジ時間 3,435 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 7 ms
5,376 KB
testcase_07 WA -
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 WA -
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 7 ms
5,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 4 ms
5,376 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 4 ms
5,376 KB
testcase_29 WA -
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 2 ms
5,376 KB
testcase_35 WA -
testcase_36 AC 2 ms
5,376 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:43:31: warning: 'buf' may be used uninitialized [-Wmaybe-uninitialized]
   43 |                         dp[i] = buf;
main.cpp:37:13: note: 'buf' was declared here
   37 |         int buf;
      |             ^~~

ソースコード

diff #

#include "bits/stdc++.h"

#define rep(a,b) for(int a = 0;a < b;a++)
#define REP(i, x, n) for(int i = x; i < n; i++)
#define P(a) cout << a << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int dx[] = { 1, -1 , 0 , 0 };
int dy[] = { 0,  0,  1, -1 };
ll MOD = 1000000007;
unsigned long long str_to_int(std::string str) {
	unsigned long long ret;
	std::stringstream ss; ss << str;
	ss >> ret;
	return ret;
}

int main() {
	int n,V,sum = 0;
	cin >> n;
	vector<int> v(n), w(n);
	rep(i, n) {
		cin >> v[i] >> w[i];
		sum += w[i];
	}
	cin >> V;
	vector<int> dp(sum+1, -1);
	dp[0] = 0;
	for (int i = 0; i < n; i++) {
		for (int j = sum; j >= 0; j--) {
			if (dp[j] != -1) {
				dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]);
			}
		}
	}
	int buf;
	rep(i, sum + 1) {
		if (dp[i] != -1) {
			buf = dp[i];
		}
		else {
			dp[i] = buf;
		}
	}
	int minans = 1e9, maxans = -1;
	for (int i = 1; i <= sum; i++) {
		if (dp[i] == V) {
			maxans = max(maxans, i);
			minans = min(minans, i);
		}
	}
	P(minans);
	if (dp[sum] == V) {
		P("inf");
	}
	else {
		P(maxans);
	}
}
0