結果

問題 No.527 ナップサック容量問題
ユーザー nenuonnenuon
提出日時 2017-06-21 14:11:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,101 bytes
コンパイル時間 646 ms
コンパイル使用メモリ 89,784 KB
実行使用メモリ 43,172 KB
最終ジャッジ日時 2024-04-10 09:01:17
合計ジャッジ時間 2,064 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 7 ms
10,024 KB
testcase_06 AC 22 ms
32,340 KB
testcase_07 AC 17 ms
21,940 KB
testcase_08 AC 6 ms
8,396 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 22 ms
32,856 KB
testcase_11 AC 10 ms
17,944 KB
testcase_12 AC 7 ms
11,152 KB
testcase_13 AC 23 ms
34,132 KB
testcase_14 AC 5 ms
7,432 KB
testcase_15 AC 8 ms
13,616 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 6 ms
10,416 KB
testcase_18 AC 8 ms
12,696 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 4 ms
8,088 KB
testcase_21 AC 29 ms
43,172 KB
testcase_22 AC 13 ms
20,992 KB
testcase_23 AC 28 ms
40,964 KB
testcase_24 AC 3 ms
6,944 KB
testcase_25 AC 13 ms
19,872 KB
testcase_26 AC 10 ms
16,724 KB
testcase_27 AC 11 ms
17,196 KB
testcase_28 AC 10 ms
13,656 KB
testcase_29 AC 30 ms
42,396 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 8 ms
10,832 KB
testcase_32 AC 10 ms
14,540 KB
testcase_33 AC 7 ms
11,276 KB
testcase_34 AC 10 ms
15,180 KB
testcase_35 AC 10 ms
15,384 KB
testcase_36 AC 1 ms
6,944 KB
testcase_37 AC 6 ms
10,124 KB
testcase_38 AC 9 ms
13,996 KB
testcase_39 AC 8 ms
12,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;

int main(){
  int n;
  cin >> n;
  int v[n], w[n];
  for (int i = 0; i < n; i++){
    cin >> v[i] >> w[i];
  }
  int V;
  cin >> V;
  int dp[n+1][1000*n+1];
  for (int i = 0; i < n + 1; i++){
    for (int j = 0; j < 1000 * n + 1; j++){
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++){
    for (int j = 0; j < 1000 * n + 1; j++){
      dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
      if(j + w[i] >= 1000 * n + 1) continue;
      dp[i+1][j+w[i]] = max(dp[i+1][j+w[i]], dp[i][j]+v[i]);
    }
  }
  int mx = 0, mn = 9999999;
  int sum = 0;
  for (int i = 0; i < n; i++){
    sum += v[i];
  }
  for (int i = 1; i < 1000 * n + 1; i++){
    if(dp[n][i] == V){
      mx = max(mx, i);
      mn = min(mn, i);
    }
  }
  printf("%d\n", mn);
  if(V == sum){
    puts("inf");
  } else {
    printf("%d\n", mx);
  }
  return 0;
}
0