結果
問題 |
No.357 品物の並び替え (Middle)
|
ユーザー |
![]() |
提出日時 | 2021-11-11 14:04:41 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 140 ms / 5,000 ms |
コード長 | 1,970 bytes |
コンパイル時間 | 2,533 ms |
コンパイル使用メモリ | 78,756 KB |
実行使用メモリ | 43,580 KB |
最終ジャッジ日時 | 2024-11-22 19:32:13 |
合計ジャッジ時間 | 4,926 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 |
ソースコード
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); ArrayList<HashMap<Integer, Integer>> list = new ArrayList<>(); int m = sc.nextInt(); for (int i = 0; i < n; i++) { list.add(new HashMap<>()); } for (int i = 0; i < m; i++) { int left = sc.nextInt(); int right = sc.nextInt(); int score = sc.nextInt(); list.get(right).put(left, score); } int[] dp = new int[1 << n]; for (int i = 1; i < (1 << n); i++) { for (int j = 0; j < n; j++) { if ((i & (1 <<j)) == 0) { continue; } int prev = (i ^ (1 << j)); int tmp = 0; for (int x : list.get(j).keySet()) { if ((prev & (1 << x)) != 0) { tmp += list.get(j).get(x); } } dp[i] = Math.max(dp[i], dp[prev] + tmp); } } System.out.println(dp[(1 << n) - 1]); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String nextLine() throws Exception { return br.readLine(); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }