結果
| 問題 | No.357 品物の並び替え (Middle) | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2020-12-23 19:16:02 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 173 ms / 5,000 ms | 
| コード長 | 1,104 bytes | 
| コンパイル時間 | 2,204 ms | 
| コンパイル使用メモリ | 77,836 KB | 
| 実行使用メモリ | 41,856 KB | 
| 最終ジャッジ日時 | 2024-09-21 16:29:38 | 
| 合計ジャッジ時間 | 5,329 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 18 | 
ソースコード
import java.util.*;
public class Main {
    static int n;
    static int[][] score;
    static int[] dp;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int m = sc.nextInt();
        score = new int[n][n];
        for (int i = 0; i < m; i++) {
            score[sc.nextInt()][sc.nextInt()] = sc.nextInt();
        }
        dp = new int[1 << n];
        Arrays.fill(dp, -1);
        dp[0] = 0;
        System.out.println(dfw((1 << n) - 1));
    }
    
    static int dfw(int mask) {
        if (dp[mask] < 0) {
            for (int i = 0; i < n; i++) {
                if ((mask & (1 << i)) == 0) {
                    continue;
                }
                int sum = 0;
                for (int j = 0; j < n; j++) {
                    if (i == j || (mask & (1 << j)) == 0) {
                        continue;
                    }
                    sum += score[j][i];
                }
                dp[mask] = Math.max(dp[mask], dfw(mask ^ (1 << i)) + sum);
            }
        }
        return dp[mask];
    }
}
            
            
            
        