結果

問題 No.1995 CHIKA Road
ユーザー tentententen
提出日時 2022-07-28 13:26:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,416 ms / 2,000 ms
コード長 2,861 bytes
コンパイル時間 2,575 ms
コンパイル使用メモリ 76,884 KB
実行使用メモリ 111,880 KB
最終ジャッジ日時 2023-09-25 05:05:39
合計ジャッジ時間 28,934 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,548 KB
testcase_01 AC 46 ms
49,740 KB
testcase_02 AC 47 ms
49,940 KB
testcase_03 AC 47 ms
49,540 KB
testcase_04 AC 49 ms
49,580 KB
testcase_05 AC 56 ms
50,116 KB
testcase_06 AC 251 ms
57,660 KB
testcase_07 AC 450 ms
65,856 KB
testcase_08 AC 62 ms
50,612 KB
testcase_09 AC 56 ms
49,936 KB
testcase_10 AC 481 ms
63,220 KB
testcase_11 AC 1,416 ms
111,880 KB
testcase_12 AC 884 ms
88,432 KB
testcase_13 AC 642 ms
76,672 KB
testcase_14 AC 679 ms
76,600 KB
testcase_15 AC 1,190 ms
110,016 KB
testcase_16 AC 296 ms
61,716 KB
testcase_17 AC 733 ms
80,412 KB
testcase_18 AC 1,045 ms
90,148 KB
testcase_19 AC 1,094 ms
89,896 KB
testcase_20 AC 697 ms
76,620 KB
testcase_21 AC 656 ms
76,820 KB
testcase_22 AC 1,100 ms
98,148 KB
testcase_23 AC 452 ms
67,016 KB
testcase_24 AC 911 ms
85,920 KB
testcase_25 AC 333 ms
62,100 KB
testcase_26 AC 502 ms
68,868 KB
testcase_27 AC 948 ms
85,720 KB
testcase_28 AC 658 ms
76,964 KB
testcase_29 AC 1,055 ms
89,892 KB
testcase_30 AC 313 ms
61,940 KB
testcase_31 AC 227 ms
56,676 KB
testcase_32 AC 359 ms
64,100 KB
testcase_33 AC 859 ms
85,864 KB
testcase_34 AC 239 ms
57,176 KB
testcase_35 AC 493 ms
68,244 KB
testcase_36 AC 520 ms
71,104 KB
testcase_37 AC 944 ms
86,896 KB
testcase_38 AC 729 ms
78,792 KB
testcase_39 AC 223 ms
57,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 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();
            compress.put(lefts[i], null);
            rights[i] = sc.nextInt();
            compress.put(rights[i], null);
        }
        compress.put(1, null);
        compress.put(n, null);
        int idx = 0;
        ArrayList<Integer> distance = new ArrayList<>();
        int prev = -1;
        for (int x : compress.keySet()) {
            compress.put(x, idx++);
            distance.add(x - prev);
            prev = x;
        }
        ArrayList<ArrayList<Path>> graph = new ArrayList<>();
        for (int i = 0; i < idx; i++) {
            graph.add(new ArrayList<>());
            if (i > 0) {
                graph.get(i - 1).add(new Path(i, distance.get(i) * 2));
            }
        }
        for (int i = 0; i < m; i++) {
            graph.get(compress.get(lefts[i])).add(new Path(compress.get(rights[i]), 2 * (rights[i] - lefts[i]) - 1));
        }
        int[] costs = new int[idx];
        Arrays.fill(costs, Integer.MAX_VALUE);
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0));
        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, x.value + p.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 = 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();
    }
}
0