結果

問題 No.1995 CHIKA Road
ユーザー tentententen
提出日時 2023-02-28 16:12:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,202 ms / 2,000 ms
コード長 3,462 bytes
コンパイル時間 4,744 ms
コンパイル使用メモリ 94,604 KB
実行使用メモリ 99,656 KB
最終ジャッジ日時 2023-10-13 23:01:07
合計ジャッジ時間 29,604 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,656 KB
testcase_01 AC 45 ms
49,632 KB
testcase_02 AC 45 ms
49,724 KB
testcase_03 AC 47 ms
49,704 KB
testcase_04 AC 49 ms
49,872 KB
testcase_05 AC 54 ms
49,928 KB
testcase_06 AC 232 ms
56,740 KB
testcase_07 AC 422 ms
62,356 KB
testcase_08 AC 60 ms
50,624 KB
testcase_09 AC 58 ms
49,884 KB
testcase_10 AC 457 ms
59,016 KB
testcase_11 AC 1,202 ms
99,656 KB
testcase_12 AC 856 ms
82,748 KB
testcase_13 AC 580 ms
67,560 KB
testcase_14 AC 589 ms
68,136 KB
testcase_15 AC 1,140 ms
90,732 KB
testcase_16 AC 296 ms
61,376 KB
testcase_17 AC 686 ms
68,768 KB
testcase_18 AC 1,011 ms
79,360 KB
testcase_19 AC 980 ms
79,236 KB
testcase_20 AC 629 ms
67,804 KB
testcase_21 AC 606 ms
66,512 KB
testcase_22 AC 955 ms
78,580 KB
testcase_23 AC 413 ms
63,336 KB
testcase_24 AC 872 ms
77,484 KB
testcase_25 AC 325 ms
61,824 KB
testcase_26 AC 451 ms
63,432 KB
testcase_27 AC 848 ms
77,232 KB
testcase_28 AC 584 ms
65,828 KB
testcase_29 AC 1,027 ms
78,516 KB
testcase_30 AC 307 ms
60,900 KB
testcase_31 AC 224 ms
56,588 KB
testcase_32 AC 365 ms
61,620 KB
testcase_33 AC 909 ms
77,868 KB
testcase_34 AC 233 ms
56,620 KB
testcase_35 AC 434 ms
63,584 KB
testcase_36 AC 491 ms
65,352 KB
testcase_37 AC 902 ms
76,636 KB
testcase_38 AC 724 ms
74,444 KB
testcase_39 AC 210 ms
56,020 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