結果

問題 No.90 品物の並び替え
ユーザー tentententen
提出日時 2022-10-13 13:10:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 72 ms / 5,000 ms
コード長 1,914 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 75,140 KB
実行使用メモリ 51,044 KB
最終ジャッジ日時 2023-09-08 19:05:14
合計ジャッジ時間 3,467 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,164 KB
testcase_01 AC 50 ms
49,428 KB
testcase_02 AC 42 ms
49,408 KB
testcase_03 AC 46 ms
49,268 KB
testcase_04 AC 48 ms
49,280 KB
testcase_05 AC 51 ms
49,480 KB
testcase_06 AC 50 ms
49,492 KB
testcase_07 AC 45 ms
49,812 KB
testcase_08 AC 43 ms
49,056 KB
testcase_09 AC 72 ms
51,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<HashMap<Integer, Integer>> scores = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            scores.add(new HashMap<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            scores.get(b).put(a, c);
        }
        int[] dp = new int[1 << n];
        for (int i = 1; i < (1 << n); i++) {
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                int sum = 0;
                for (int x : scores.get(j).keySet()) {
                    if ((i & (1 << x)) == 0) {
                        continue;
                    }
                    sum += scores.get(j).get(x);
                }
                dp[i] = Math.max(dp[i], dp[i ^ (1 << j)] + sum);
            }
        }
        System.out.println(dp[(1 << n) - 1]);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0