結果

問題 No.357 品物の並び替え (Middle)
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-16 15:50:13
言語 Java
(openjdk 23)
結果
AC  
実行時間 144 ms / 5,000 ms
コード長 1,279 bytes
コンパイル時間 4,412 ms
コンパイル使用メモリ 78,892 KB
実行使用メモリ 54,444 KB
最終ジャッジ日時 2024-09-17 05:31:48
合計ジャッジ時間 6,036 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int n = Integer.parseInt(stdin.next());
        int m = Integer.parseInt(stdin.next());
        
        int[][] scores = new int[n][n];
        for (int i = 0; i < m; i++) {
            int item1 = Integer.parseInt(stdin.next());
            int item2 = Integer.parseInt(stdin.next());
            int score = Integer.parseInt(stdin.next());
            scores[item1][item2] = score;
        }
        
        int[] dp = new int[1 << n];
        for (int bit = 0; bit < dp.length; bit++) {
            for (int nxt = 0; nxt < n; nxt++) {
                if ((bit & (1 << nxt)) != 0) {
                    continue;
                }
                
                int score = dp[bit];
                for (int pre = 0; pre < n; pre++) {
                    if ((bit & (1 << pre)) != 0) {
                        score += scores[pre][nxt];
                    }
                }
                
                dp[bit | (1 << nxt)] = Math.max(dp[bit | (1 << nxt)], score);
            }
        }
        
        int ans = Arrays.stream(dp).max().getAsInt();
        System.out.println(ans);
    }
}
0