結果

問題 No.1995 CHIKA Road
ユーザー tentententen
提出日時 2023-02-28 16:12:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,179 ms / 2,000 ms
コード長 3,462 bytes
コンパイル時間 4,418 ms
コンパイル使用メモリ 86,248 KB
実行使用メモリ 85,272 KB
最終ジャッジ日時 2024-09-15 18:50:34
合計ジャッジ時間 26,715 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
36,840 KB
testcase_01 AC 53 ms
37,000 KB
testcase_02 AC 52 ms
36,744 KB
testcase_03 AC 53 ms
37,152 KB
testcase_04 AC 53 ms
36,700 KB
testcase_05 AC 58 ms
36,968 KB
testcase_06 AC 243 ms
45,340 KB
testcase_07 AC 424 ms
52,248 KB
testcase_08 AC 65 ms
37,440 KB
testcase_09 AC 62 ms
36,948 KB
testcase_10 AC 468 ms
49,128 KB
testcase_11 AC 1,179 ms
85,272 KB
testcase_12 AC 828 ms
69,472 KB
testcase_13 AC 561 ms
56,628 KB
testcase_14 AC 575 ms
58,056 KB
testcase_15 AC 1,114 ms
77,652 KB
testcase_16 AC 324 ms
49,672 KB
testcase_17 AC 686 ms
58,724 KB
testcase_18 AC 1,013 ms
67,888 KB
testcase_19 AC 1,022 ms
68,088 KB
testcase_20 AC 632 ms
57,336 KB
testcase_21 AC 577 ms
56,284 KB
testcase_22 AC 976 ms
67,640 KB
testcase_23 AC 430 ms
52,764 KB
testcase_24 AC 876 ms
65,712 KB
testcase_25 AC 333 ms
50,044 KB
testcase_26 AC 470 ms
53,108 KB
testcase_27 AC 799 ms
65,132 KB
testcase_28 AC 586 ms
56,016 KB
testcase_29 AC 1,041 ms
68,124 KB
testcase_30 AC 331 ms
50,248 KB
testcase_31 AC 233 ms
45,772 KB
testcase_32 AC 376 ms
51,004 KB
testcase_33 AC 832 ms
63,828 KB
testcase_34 AC 250 ms
45,996 KB
testcase_35 AC 456 ms
52,708 KB
testcase_36 AC 489 ms
53,824 KB
testcase_37 AC 917 ms
66,424 KB
testcase_38 AC 735 ms
62,088 KB
testcase_39 AC 230 ms
44,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        TreeMap<Integer, Integer> compress = new TreeMap<>();
        compress.put(1, null);
        compress.put(n, null);
        int m = sc.nextInt();
        int[] lefts = new int[m];
        int[] rights = new int[m];
        for (int i = 0; i < m; i++) {
            lefts[i] = sc.nextInt();
            compress.put(lefts[i], null);
            rights[i] = sc.nextInt();
            compress.put(rights[i], null);
        }
        int[] values = new int[compress.size()];
        int idx = 0;
        for (int x : compress.keySet()) {
            compress.put(x, idx);
            values[idx] = x;
            idx++;
        }
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < idx; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            graph.get(compress.get(lefts[i])).add(compress.get(rights[i]));
        }
        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 (int x : graph.get(p.idx)) {
                int add = (values[x] - values[p.idx]) * 2 - 1;
                queue.add(new Path(x, p.value + add));
            }
            if (p.idx < idx - 1) {
                int add = (values[p.idx + 1] - values[p.idx]) * 2;
                queue.add(new Path(p.idx + 1, p.value + add));
            }
        }
        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 Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0