結果
問題 | No.1995 CHIKA Road |
ユーザー | tenten |
提出日時 | 2024-07-30 10:20:50 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,270 ms / 2,000 ms |
コード長 | 2,921 bytes |
コンパイル時間 | 2,527 ms |
コンパイル使用メモリ | 80,348 KB |
実行使用メモリ | 111,576 KB |
最終ジャッジ日時 | 2024-07-30 10:21:19 |
合計ジャッジ時間 | 27,076 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 54 ms
50,532 KB |
testcase_01 | AC | 52 ms
50,216 KB |
testcase_02 | AC | 51 ms
50,368 KB |
testcase_03 | AC | 53 ms
50,496 KB |
testcase_04 | AC | 54 ms
50,036 KB |
testcase_05 | AC | 59 ms
50,616 KB |
testcase_06 | AC | 246 ms
56,712 KB |
testcase_07 | AC | 457 ms
65,724 KB |
testcase_08 | AC | 81 ms
51,124 KB |
testcase_09 | AC | 64 ms
50,656 KB |
testcase_10 | AC | 473 ms
63,376 KB |
testcase_11 | AC | 1,270 ms
111,576 KB |
testcase_12 | AC | 932 ms
85,040 KB |
testcase_13 | AC | 619 ms
76,456 KB |
testcase_14 | AC | 650 ms
75,684 KB |
testcase_15 | AC | 1,199 ms
102,692 KB |
testcase_16 | AC | 333 ms
61,924 KB |
testcase_17 | AC | 774 ms
77,376 KB |
testcase_18 | AC | 1,082 ms
97,192 KB |
testcase_19 | AC | 1,100 ms
97,116 KB |
testcase_20 | AC | 698 ms
75,988 KB |
testcase_21 | AC | 694 ms
76,408 KB |
testcase_22 | AC | 1,103 ms
88,444 KB |
testcase_23 | AC | 462 ms
66,688 KB |
testcase_24 | AC | 974 ms
85,100 KB |
testcase_25 | AC | 348 ms
62,044 KB |
testcase_26 | AC | 507 ms
66,868 KB |
testcase_27 | AC | 989 ms
84,336 KB |
testcase_28 | AC | 651 ms
76,236 KB |
testcase_29 | AC | 1,036 ms
96,980 KB |
testcase_30 | AC | 338 ms
61,888 KB |
testcase_31 | AC | 256 ms
57,492 KB |
testcase_32 | AC | 393 ms
64,832 KB |
testcase_33 | AC | 862 ms
82,844 KB |
testcase_34 | AC | 257 ms
56,300 KB |
testcase_35 | AC | 489 ms
66,924 KB |
testcase_36 | AC | 543 ms
67,888 KB |
testcase_37 | AC | 1,042 ms
84,048 KB |
testcase_38 | AC | 774 ms
78,368 KB |
testcase_39 | AC | 229 ms
55,668 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); int[] lefts = new int[m]; int[] rights = new int[m]; TreeMap<Integer, Integer> compress = new TreeMap<>(); for (int i = 0; i < m; i++) { lefts[i] = sc.nextInt(); rights[i] = sc.nextInt(); compress.put(lefts[i], null); compress.put(rights[i], null); } compress.put(1, null); compress.put(n, null); int idx = 0; List<List<Path>> graph = new ArrayList<>(); for (int x : compress.keySet()) { graph.add(new ArrayList<>()); if (x != compress.lastKey()) { graph.get(idx).add(new Path(idx + 1, (compress.higherKey(x) - x) * 2)); } compress.put(x, idx++); } for (int i = 0; i < m; i++) { graph.get(compress.get(lefts[i])).add(new Path(compress.get(rights[i]), (rights[i] - lefts[i]) * 2 - 1)); } PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(0, 0)); int[] costs = new int[idx]; Arrays.fill(costs, Integer.MAX_VALUE); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; for (Path x : graph.get(p.idx)) { queue.add(new Path(x.idx, p.value + x.value)); } } System.out.println(costs[idx - 1]); } static class Path implements Comparable<Path> { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { return value - another.value; } } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }