結果

問題 No.357 品物の並び替え (Middle)
ユーザー buno15buno15
提出日時 2021-11-06 11:17:41
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,235 bytes
コンパイル時間 4,440 ms
コンパイル使用メモリ 79,980 KB
実行使用メモリ 42,932 KB
最終ジャッジ日時 2024-04-24 21:53:38
合計ジャッジ時間 4,759 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 56 ms
40,668 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {

	static final int INF = 1000000000;
	static final long MOD = 1000000007;
	static final double EPS = 1e-10;

	public static void main(String args[]) throws IOException {
		IO io = new IO();

		int arr[] = io.getIntArrPrim();

		int n = arr[0];
		int m = arr[1];

		ArrayList<ArrayList<Pair>> list = new ArrayList<>();

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

		for (int i = 0; i < m; i++) {
			arr = io.getIntArrPrim();

			int a = arr[0];
			int b = arr[1];
			int c = arr[2];

			list.get(b).add(new Pair(a, c));
		}

		int dp[] = new int[1 << 20];
		dp[0] = 0;

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

				int sum = 0;

				for (int j = 0; j < list.get(i).size(); j++) {
					Pair p = list.get(i).get(j);

					int to = p.to;
					int cost = p.cost;

					if ((bit & (1 << to)) != 0)
						continue;

					sum += cost;
				}

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

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

class Pair {
	int to;
	int cost;

	public Pair(int to, int cost) {
		this.to = to;
		this.cost = cost;
	}
}

class IO {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	public IO() {

	}

	public void println(String str) {
		System.out.println(str);
	}

	public void printArr(Object o[]) {
		for (int i = 0; i < o.length; i++) {
			System.out.print(o + " ");
		}
		System.out.println();
	}

	public int getInt() throws IOException {
		return Integer.parseInt(br.readLine());
	}

	public long getLong() throws IOException {
		return Long.parseLong(br.readLine());
	}

	public double getDouble() throws IOException {
		return Double.parseDouble(br.readLine());
	}

	public String getLine() throws IOException {
		return br.readLine();
	}

	public int[] getIntArrPrim() throws IOException {
		String str[] = br.readLine().split(" ");
		int a[] = new int[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		return a;
	}

	public Integer[] getIntArr() throws IOException {
		String str[] = br.readLine().split(" ");
		Integer a[] = new Integer[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		return a;
	}

	public Long[] getLongArr() throws IOException {
		String str[] = br.readLine().split(" ");
		Long a[] = new Long[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Long.parseLong(str[i]);
		}

		return a;
	}

	public long[] getLongArrPrim() throws IOException {
		String str[] = br.readLine().split(" ");
		long a[] = new long[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Long.parseLong(str[i]);
		}

		return a;
	}

	public String[] getStrArr(String split) throws IOException {
		return br.readLine().split(split);
	}

	public char[] getCharArr() throws IOException {
		return br.readLine().toCharArray();
	}

	public int[][] getIntMap(int w, int h, String split) throws IOException {
		int a[][] = new int[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = Integer.parseInt(str[j]);
			}
		}

		return a;
	}

	public long[][] getLongMap(int w, int h, String split) throws IOException {
		long a[][] = new long[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = Long.parseLong(str[j]);
			}
		}

		return a;
	}

	public double[][] getDoubleMap(int w, int h, String split) throws IOException {
		double a[][] = new double[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = Double.parseDouble(str[j]);
			}
		}

		return a;
	}

	public char[][] getCharMap(int w, int h, String split) throws IOException {
		char a[][] = new char[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = str[j].charAt(0);
			}
		}

		return a;
	}
}
0