結果

問題 No.614 壊れたキャンパス
ユーザー tentententen
提出日時 2021-07-21 11:46:41
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,288 bytes
コンパイル時間 4,378 ms
コンパイル使用メモリ 77,100 KB
実行使用メモリ 198,568 KB
最終ジャッジ日時 2023-09-24 13:02:20
合計ジャッジ時間 13,001 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
50,008 KB
testcase_01 AC 45 ms
49,764 KB
testcase_02 AC 45 ms
49,884 KB
testcase_03 AC 45 ms
49,824 KB
testcase_04 AC 46 ms
49,692 KB
testcase_05 AC 45 ms
49,756 KB
testcase_06 AC 47 ms
50,116 KB
testcase_07 AC 45 ms
49,808 KB
testcase_08 TLE -
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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 k = sc.nextInt();
        int s = sc.nextInt();
        int t = sc.nextInt();
        ArrayList<TreeSet<Integer>> floors = new ArrayList<>();
        ArrayList<HashMap<Integer, Long>> dp = new ArrayList<>();
        ArrayList<HashMap<Integer, ArrayList<Integer>>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            floors.add(new TreeSet<>());
            dp.add(new HashMap<>());
            graph.add(new HashMap<>());
        }
        floors.get(0).add(s);
        floors.get(n - 1).add(t);
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt();
            int c = sc.nextInt();
            floors.get(a).add(b);
            floors.get(a + 1).add(c);
            if (!graph.get(a).containsKey(b)) {
                graph.get(a).put(b, new ArrayList<>());
            }
            graph.get(a).get(b).add(c);
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, s, 0));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (dp.get(p.idx).containsKey(p.floor)) {
                continue;
            }
            dp.get(p.idx).put(p.floor, p.value);
            if (floors.get(p.idx).first() != p.floor) {
                int next = floors.get(p.idx).lower(p.floor);
                queue.add(new Path(p.idx, next, p.value + p.floor - next));
            }
            if (floors.get(p.idx).last() != p.floor) {
                int next = floors.get(p.idx).higher(p.floor);
                queue.add(new Path(p.idx, next, p.value + next - p.floor));
            }
            if (graph.get(p.idx).containsKey(p.floor)) {
                for (int x : graph.get(p.idx).get(p.floor)) {
                    queue.add(new Path(p.idx + 1, x, p.value));
                }
            }
        }
        System.out.println(dp.get(n - 1).getOrDefault(t, -1L));
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int floor;
        long value;
        
        public Path(int idx, int floor, long value) {
            this.idx = idx;
            this.floor = floor;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            if (value == another.value) {
                return 0;
            } else if (value < another.value) {
                return -1;
            } else {
                return 1;
            }
        }
    }
}
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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0