結果

問題 No.357 品物の並び替え (Middle)
ユーザー buno15buno15
提出日時 2020-10-22 20:39:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 78 ms / 5,000 ms
コード長 1,053 bytes
コンパイル時間 3,339 ms
コンパイル使用メモリ 73,964 KB
実行使用メモリ 52,024 KB
最終ジャッジ日時 2023-09-28 14:51:44
合計ジャッジ時間 4,286 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
49,152 KB
testcase_01 AC 46 ms
49,152 KB
testcase_02 AC 46 ms
49,396 KB
testcase_03 AC 46 ms
49,188 KB
testcase_04 AC 46 ms
49,300 KB
testcase_05 AC 46 ms
49,472 KB
testcase_06 AC 45 ms
49,276 KB
testcase_07 AC 44 ms
49,564 KB
testcase_08 AC 48 ms
49,108 KB
testcase_09 AC 65 ms
51,292 KB
testcase_10 AC 51 ms
49,140 KB
testcase_11 AC 52 ms
50,188 KB
testcase_12 AC 69 ms
51,348 KB
testcase_13 AC 65 ms
50,956 KB
testcase_14 AC 78 ms
52,024 KB
testcase_15 AC 52 ms
50,120 KB
testcase_16 AC 62 ms
50,784 KB
testcase_17 AC 52 ms
49,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		int n = Integer.parseInt(str[0]);
		int m = Integer.parseInt(str[1]);

		int score[][] = new int[n][n];

		for (int i = 0; i < m; i++) {
			str = br.readLine().split(" ");

			int a = Integer.parseInt(str[0]);
			int b = Integer.parseInt(str[1]);
			int s = Integer.parseInt(str[2]);

			score[a][b] = s;
		}

		int dp[] = new int[(1 << n)];

		dp[0] = 0;

		for (int bit = 0; bit < (1 << n); bit++) {
			for (int i = 0; i < n; i++) {
				if ((bit & (1 << i)) != 0)
					continue;

				int cost = 0;
				for (int j = 0; j < n; j++) {
					if ((bit & (1 << j)) != 0)
						continue;

					cost += score[j][i];
				}

				int now = ((bit | (1 << i)));
				dp[now] = Math.max(dp[now], dp[bit] + cost);
			}
		}

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