結果

問題 No.1995 CHIKA Road
ユーザー tentententen
提出日時 2024-04-30 10:53:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,094 ms / 2,000 ms
コード長 3,011 bytes
コンパイル時間 2,262 ms
コンパイル使用メモリ 80,456 KB
実行使用メモリ 110,728 KB
最終ジャッジ日時 2024-04-30 10:54:24
合計ジャッジ時間 22,989 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
50,020 KB
testcase_01 AC 46 ms
50,132 KB
testcase_02 AC 47 ms
50,344 KB
testcase_03 AC 47 ms
50,164 KB
testcase_04 AC 48 ms
50,552 KB
testcase_05 AC 54 ms
50,504 KB
testcase_06 AC 230 ms
56,192 KB
testcase_07 AC 365 ms
65,316 KB
testcase_08 AC 70 ms
50,544 KB
testcase_09 AC 54 ms
50,252 KB
testcase_10 AC 414 ms
63,240 KB
testcase_11 AC 1,094 ms
110,728 KB
testcase_12 AC 802 ms
82,476 KB
testcase_13 AC 535 ms
76,120 KB
testcase_14 AC 555 ms
76,124 KB
testcase_15 AC 1,051 ms
104,172 KB
testcase_16 AC 263 ms
62,032 KB
testcase_17 AC 609 ms
77,468 KB
testcase_18 AC 933 ms
96,776 KB
testcase_19 AC 916 ms
96,912 KB
testcase_20 AC 573 ms
76,028 KB
testcase_21 AC 523 ms
76,232 KB
testcase_22 AC 892 ms
86,604 KB
testcase_23 AC 384 ms
65,336 KB
testcase_24 AC 799 ms
84,980 KB
testcase_25 AC 297 ms
61,868 KB
testcase_26 AC 421 ms
66,752 KB
testcase_27 AC 798 ms
81,588 KB
testcase_28 AC 547 ms
72,892 KB
testcase_29 AC 884 ms
89,352 KB
testcase_30 AC 281 ms
62,328 KB
testcase_31 AC 208 ms
56,056 KB
testcase_32 AC 324 ms
63,772 KB
testcase_33 AC 765 ms
80,620 KB
testcase_34 AC 214 ms
56,840 KB
testcase_35 AC 423 ms
66,180 KB
testcase_36 AC 458 ms
66,804 KB
testcase_37 AC 881 ms
83,944 KB
testcase_38 AC 629 ms
81,868 KB
testcase_39 AC 201 ms
56,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
        TreeMap<Integer, Integer> compress = new TreeMap<>();
        int[] lefts = new int[m];
        int[] rights = new int[m];
        compress.put(1, null);
        compress.put(n, null);
        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);
        }
        List<List<Path>> graph = new ArrayList<>();
        for (int i = 0; i < compress.size(); i++) {
            graph.add(new ArrayList<>());
        }
        int prev = -1;
        int idx = 0;
        for (int x : compress.keySet()) {
            compress.put(x, idx);
            if (prev >= 1) {
                graph.get(idx - 1).add(new Path(idx, (x - prev) * 2));
            }
            idx++;
            prev = x;
        }
        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));
        }
        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;
    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();
        }
    }
    
}
0