結果

問題 No.357 品物の並び替え (Middle)
ユーザー buno15
提出日時 2020-10-22 20:39:39
言語 Java
(openjdk 23)
結果
AC  
実行時間 95 ms / 5,000 ms
コード長 1,053 bytes
コンパイル時間 2,595 ms
コンパイル使用メモリ 76,572 KB
実行使用メモリ 38,348 KB
最終ジャッジ日時 2024-07-21 09:33:07
合計ジャッジ時間 4,220 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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