結果

問題 No.357 品物の並び替え (Middle)
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-27 12:45:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 170 ms / 5,000 ms
コード長 825 bytes
コンパイル時間 2,694 ms
コンパイル使用メモリ 77,712 KB
実行使用メモリ 56,248 KB
最終ジャッジ日時 2023-09-15 06:07:03
合計ジャッジ時間 5,937 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
55,848 KB
testcase_01 AC 122 ms
56,032 KB
testcase_02 AC 129 ms
55,852 KB
testcase_03 AC 133 ms
55,840 KB
testcase_04 AC 138 ms
56,068 KB
testcase_05 AC 132 ms
55,764 KB
testcase_06 AC 141 ms
55,936 KB
testcase_07 AC 123 ms
56,180 KB
testcase_08 AC 144 ms
55,696 KB
testcase_09 AC 152 ms
55,956 KB
testcase_10 AC 150 ms
55,792 KB
testcase_11 AC 138 ms
56,204 KB
testcase_12 AC 170 ms
55,836 KB
testcase_13 AC 157 ms
56,048 KB
testcase_14 AC 155 ms
55,564 KB
testcase_15 AC 138 ms
55,880 KB
testcase_16 AC 150 ms
56,028 KB
testcase_17 AC 157 ms
56,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    int[][] score = new int[n][n];
    for(int i = 0; i < m; i++) {
      int i1 = sc.nextInt();
      int i2 = sc.nextInt();
      int s = sc.nextInt();
      score[i2][i1] = s;
    }
    int p = (int)Math.pow(2, n);
    int[] dp = new int[p];

    for(int i = 1; i < p; i++) {
      for(int j = 0; j < n; j++) {
        if((i & (1 << j)) != 0) {
          int i1 = i - (1 << j);
          int t = dp[i1];
          for(int k = 0; k < n; k++) {
            if((i1 & (1 << k)) != 0) {
              t += score[j][k];
            } 
          }
          dp[i] = Math.max(dp[i], t); 
        }
      }
    }
    System.out.println(dp[p - 1]);
  }
}
0