結果
問題 | No.1676 Coin Trade (Single) |
ユーザー | tenten |
提出日時 | 2022-07-27 10:25:58 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 609 ms / 2,000 ms |
コード長 | 2,678 bytes |
コンパイル時間 | 2,941 ms |
コンパイル使用メモリ | 83,272 KB |
実行使用メモリ | 65,388 KB |
最終ジャッジ日時 | 2024-07-16 17:16:39 |
合計ジャッジ時間 | 13,397 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 55 ms
37,036 KB |
testcase_01 | AC | 57 ms
36,532 KB |
testcase_02 | AC | 56 ms
36,976 KB |
testcase_03 | AC | 487 ms
54,012 KB |
testcase_04 | AC | 453 ms
52,540 KB |
testcase_05 | AC | 439 ms
52,524 KB |
testcase_06 | AC | 447 ms
52,364 KB |
testcase_07 | AC | 416 ms
51,460 KB |
testcase_08 | AC | 354 ms
49,528 KB |
testcase_09 | AC | 399 ms
49,972 KB |
testcase_10 | AC | 398 ms
51,372 KB |
testcase_11 | AC | 500 ms
53,352 KB |
testcase_12 | AC | 390 ms
50,268 KB |
testcase_13 | AC | 56 ms
36,992 KB |
testcase_14 | AC | 56 ms
37,148 KB |
testcase_15 | AC | 57 ms
37,180 KB |
testcase_16 | AC | 56 ms
37,056 KB |
testcase_17 | AC | 56 ms
37,072 KB |
testcase_18 | AC | 56 ms
37,052 KB |
testcase_19 | AC | 55 ms
36,980 KB |
testcase_20 | AC | 56 ms
36,972 KB |
testcase_21 | AC | 55 ms
37,148 KB |
testcase_22 | AC | 56 ms
37,072 KB |
testcase_23 | AC | 55 ms
37,168 KB |
testcase_24 | AC | 56 ms
36,836 KB |
testcase_25 | AC | 54 ms
37,072 KB |
testcase_26 | AC | 56 ms
36,880 KB |
testcase_27 | AC | 56 ms
37,208 KB |
testcase_28 | AC | 55 ms
37,088 KB |
testcase_29 | AC | 55 ms
36,880 KB |
testcase_30 | AC | 55 ms
36,944 KB |
testcase_31 | AC | 55 ms
37,176 KB |
testcase_32 | AC | 54 ms
37,200 KB |
testcase_33 | AC | 549 ms
56,152 KB |
testcase_34 | AC | 539 ms
55,352 KB |
testcase_35 | AC | 609 ms
55,408 KB |
testcase_36 | AC | 543 ms
65,388 KB |
testcase_37 | AC | 533 ms
65,232 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<ArrayList<Path>> graph = new ArrayList<>(); int[] values = new int[n]; for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); values[i] = sc.nextInt(); int m = sc.nextInt(); for (int j = 0; j < m; j++) { int idx = sc.nextInt() - 1; if (values[idx] < values[i]) { graph.get(idx).add(new Path(i, values[i] - values[idx])); } } } PriorityQueue<Path> queue = new PriorityQueue<>(); long[] gains = new long[n]; Arrays.fill(gains, -1); queue.add(new Path(0, 0)); while (queue.size() > 0) { Path p = queue.poll(); if (gains[p.idx] >= p.value) { continue; } gains[p.idx] = p.value; for (Path x : graph.get(p.idx)) { queue.add(new Path(x.idx, x.value + p.value)); } if (p.idx < n - 1) { queue.add(new Path(p.idx + 1, p.value)); } } System.out.println(gains[n - 1]); } static class Path implements Comparable<Path> { int idx; long value; public Path(int idx, long value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { if (idx == another.idx) { if (value == another.value) { return 0; } else if (value < another.value) { return 1; } else { return -1; } } else { return idx - another.idx; } } } } 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 next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }