結果

問題 No.357 品物の並び替え (Middle)
ユーザー tentententen
提出日時 2021-12-03 12:53:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 160 ms / 5,000 ms
コード長 1,847 bytes
コンパイル時間 3,632 ms
コンパイル使用メモリ 74,952 KB
実行使用メモリ 55,676 KB
最終ジャッジ日時 2023-09-19 04:45:22
合計ジャッジ時間 5,294 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
49,456 KB
testcase_01 AC 47 ms
49,668 KB
testcase_02 AC 50 ms
49,452 KB
testcase_03 AC 51 ms
49,256 KB
testcase_04 AC 54 ms
49,572 KB
testcase_05 AC 53 ms
49,324 KB
testcase_06 AC 48 ms
49,252 KB
testcase_07 AC 46 ms
49,252 KB
testcase_08 AC 64 ms
51,548 KB
testcase_09 AC 112 ms
55,028 KB
testcase_10 AC 89 ms
52,160 KB
testcase_11 AC 86 ms
50,696 KB
testcase_12 AC 160 ms
55,676 KB
testcase_13 AC 122 ms
55,200 KB
testcase_14 AC 116 ms
55,028 KB
testcase_15 AC 67 ms
50,236 KB
testcase_16 AC 92 ms
52,028 KB
testcase_17 AC 94 ms
52,068 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>> points = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            points.add(new HashMap<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            points.get(b).put(a, c);
        }
        int[] total = 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 tmp = 0;
                for (int x : points.get(j).keySet()) {
                    if ((i & (1 << x)) != 0) {
                        tmp += points.get(j).get(x);
                    }
                }
                total[i] = Math.max(total[i], total[i ^ (1 << j)] + tmp);
            }
        }
        System.out.println(total[(1 << n) - 1]);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0