結果

問題 No.1 道のショートカット
ユーザー tenten
提出日時 2020-10-22 16:46:21
言語 Java
(openjdk 23)
結果
AC  
実行時間 364 ms / 5,000 ms
コード長 2,488 bytes
コンパイル時間 2,503 ms
コンパイル使用メモリ 79,452 KB
実行使用メモリ 49,040 KB
最終ジャッジ日時 2024-07-21 09:19:53
合計ジャッジ時間 11,759 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int c = sc.nextInt();
        int v = sc.nextInt();
        int[] starts = new int[v];
        for (int i = 0; i < v; i++) {
            starts[i] = sc.nextInt() - 1;
        }
        int[] terminals = new int[v];
        for (int i = 0; i < v; i++) {
            terminals[i] = sc.nextInt() - 1;
        }
        int[] yen = new int[v];
        for (int i = 0; i < v; i++) {
            yen[i] = sc.nextInt();
        }
        int[] mins = new int[v];
        for (int i = 0; i < v; i++) {
            mins[i] = sc.nextInt();
        }
        ArrayList<ArrayList<Route>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < v; i++) {
            graph.get(starts[i]).add(new Route(terminals[i], yen[i], mins[i]));
        }
        int[][] costs = new int[n][c + 1];
        for (int[] arr : costs) {
            Arrays.fill(arr, Integer.MAX_VALUE);
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0, c));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (p.yen < 0 || costs[p.idx][p.yen] <= p.value) {
                continue;
            }
            costs[p.idx][p.yen] = p.value;
            for (Route r : graph.get(p.idx)) {
                queue.add(new Path(r.idx, p.value + r.value, p.yen - r.yen));
            }
        }
        int ans = Integer.MAX_VALUE;
        for (int x : costs[n - 1]) {
            ans = Math.min(ans, x);
        }
        if (ans == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(ans);
        }
    }
    
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        int yen;
        
        public Path(int idx, int value, int yen) {
            this.idx = idx;
            this.value = value;
            this.yen = yen;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
    
    static class Route {
        int idx;
        int yen;
        int value;
        
        public Route(int idx, int yen, int value) {
            this.idx = idx;
            this.yen = yen;
            this.value = value;
        }
    }
}
0