結果

問題 No.357 品物の並び替え (Middle)
ユーザー GrenacheGrenache
提出日時 2016-04-02 18:54:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 207 ms / 5,000 ms
コード長 1,202 bytes
コンパイル時間 3,713 ms
コンパイル使用メモリ 78,200 KB
実行使用メモリ 56,656 KB
最終ジャッジ日時 2024-04-10 10:39:06
合計ジャッジ時間 7,551 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
54,424 KB
testcase_01 AC 131 ms
53,996 KB
testcase_02 AC 142 ms
53,980 KB
testcase_03 AC 143 ms
54,388 KB
testcase_04 AC 151 ms
54,304 KB
testcase_05 AC 146 ms
54,316 KB
testcase_06 AC 142 ms
54,108 KB
testcase_07 AC 135 ms
54,100 KB
testcase_08 AC 155 ms
54,300 KB
testcase_09 AC 175 ms
56,492 KB
testcase_10 AC 189 ms
54,580 KB
testcase_11 AC 150 ms
54,088 KB
testcase_12 AC 207 ms
56,656 KB
testcase_13 AC 188 ms
56,572 KB
testcase_14 AC 177 ms
56,276 KB
testcase_15 AC 145 ms
54,144 KB
testcase_16 AC 187 ms
54,448 KB
testcase_17 AC 183 ms
54,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;


public class Main_yukicoder357 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();

        List<List<Pair>> prev = new ArrayList<>();
        for (int i = 0; i < n; i++) {
        	prev.add(new ArrayList<>());
        }

        for (int i = 0; i < m; i++) {
        	int item1 = sc.nextInt();
        	int item2 = sc.nextInt();
        	int score = sc.nextInt();
        	prev.get(item2).add(new Pair(item1, score));

        }

        int[] dp = new int[0x1 << n];
        for (int i = 0; i < 0x1 << n; i++) {
        	for (int j = 0; j < n; j++) {
        		if ((i & 0x1 << j) != 0) {
        			continue;
        		}

        		int sum = 0;
        		for (Pair e : prev.get(j)) {
        			if ((i & 0x1 << e.a) != 0) {
        				sum += e.b;
        			}
        		}

        		dp[i | 0x1 << j] = Math.max(dp[i | 0x1 << j], dp[i] + sum);
        	}
        }

        System.out.println(dp[(0x1 << n) - 1]);

        sc.close();
    }

    private static class Pair {
    	int a;
    	int b;

    	Pair(int a, int b) {
    		this.a = a;
    		this.b = b;
    	}
    }
}
0