結果

問題 No.614 壊れたキャンパス
ユーザー tentententen
提出日時 2021-01-26 14:40:07
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,795 bytes
コンパイル時間 2,189 ms
コンパイル使用メモリ 76,504 KB
実行使用メモリ 170,176 KB
最終ジャッジ日時 2023-09-05 18:50:45
合計ジャッジ時間 11,414 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
56,592 KB
testcase_01 AC 44 ms
49,888 KB
testcase_02 AC 46 ms
49,584 KB
testcase_03 AC 46 ms
49,620 KB
testcase_04 AC 46 ms
49,732 KB
testcase_05 AC 47 ms
49,716 KB
testcase_06 AC 47 ms
49,540 KB
testcase_07 AC 46 ms
49,648 KB
testcase_08 AC 1,983 ms
88,496 KB
testcase_09 AC 1,259 ms
101,772 KB
testcase_10 AC 666 ms
81,940 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 5);
        int n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        int k = Integer.parseInt(first[2]);
        int s = Integer.parseInt(first[3]);
        int t = Integer.parseInt(first[4]);
        ArrayList<ArrayList<Route>> graph = new ArrayList<>();
        for (int i = 0; i < n - 1; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            String[] line = br.readLine().split(" ", 3);
            int a = Integer.parseInt(line[0]) - 1;
            int b = Integer.parseInt(line[1]);
            int c = Integer.parseInt(line[2]);
            graph.get(a).add(new Route(b, c));
        }
        ArrayList<HashMap<Integer, Long>> costs = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            costs.add(new HashMap<>());
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, s, 0));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs.get(p.idx).containsKey(p.floor)) {
                continue;
            }
            costs.get(p.idx).put(p.floor, p.value);
            if (p.idx == n - 1) {
                continue;
            }
            for (Route x : graph.get(p.idx)) {
                queue.add(new Path(p.idx + 1, x.end, p.value + Math.abs(p.floor - x.start)));
            }
        }
        long min = Long.MAX_VALUE;
        for (Map.Entry<Integer, Long> entry : costs.get(n - 1).entrySet()) {
            int key = entry.getKey();
            long value = entry.getValue();
            min = Math.min(min, Math.abs(key - t) + value);
        }
        if (min == Long.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(min);
        }
    }
    
    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;
            }
        }
    }
    
    static class Route {
        int start;
        int end;
        
        public Route(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }
}
0