結果

問題 No.1 道のショートカット
ユーザー tentententen
提出日時 2021-11-10 08:30:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 213 ms / 5,000 ms
コード長 2,874 bytes
コンパイル時間 2,042 ms
コンパイル使用メモリ 79,560 KB
実行使用メモリ 57,492 KB
最終ジャッジ日時 2024-04-30 18:55:23
合計ジャッジ時間 7,365 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
50,264 KB
testcase_01 AC 49 ms
50,596 KB
testcase_02 AC 49 ms
50,452 KB
testcase_03 AC 50 ms
50,104 KB
testcase_04 AC 50 ms
50,508 KB
testcase_05 AC 49 ms
50,264 KB
testcase_06 AC 49 ms
50,532 KB
testcase_07 AC 49 ms
50,560 KB
testcase_08 AC 131 ms
53,192 KB
testcase_09 AC 88 ms
51,800 KB
testcase_10 AC 111 ms
52,308 KB
testcase_11 AC 150 ms
55,396 KB
testcase_12 AC 201 ms
56,896 KB
testcase_13 AC 201 ms
57,380 KB
testcase_14 AC 53 ms
50,588 KB
testcase_15 AC 50 ms
50,564 KB
testcase_16 AC 59 ms
50,956 KB
testcase_17 AC 53 ms
50,436 KB
testcase_18 AC 55 ms
50,652 KB
testcase_19 AC 56 ms
50,624 KB
testcase_20 AC 89 ms
51,964 KB
testcase_21 AC 57 ms
50,652 KB
testcase_22 AC 54 ms
50,528 KB
testcase_23 AC 189 ms
57,204 KB
testcase_24 AC 208 ms
57,220 KB
testcase_25 AC 103 ms
51,916 KB
testcase_26 AC 86 ms
51,728 KB
testcase_27 AC 185 ms
56,324 KB
testcase_28 AC 52 ms
50,144 KB
testcase_29 AC 126 ms
52,792 KB
testcase_30 AC 79 ms
52,204 KB
testcase_31 AC 118 ms
51,772 KB
testcase_32 AC 125 ms
52,464 KB
testcase_33 AC 101 ms
52,060 KB
testcase_34 AC 213 ms
57,492 KB
testcase_35 AC 77 ms
51,912 KB
testcase_36 AC 111 ms
52,184 KB
testcase_37 AC 84 ms
51,680 KB
testcase_38 AC 51 ms
50,548 KB
testcase_39 AC 53 ms
50,540 KB
testcase_40 AC 75 ms
51,140 KB
testcase_41 AC 55 ms
50,636 KB
testcase_42 AC 53 ms
50,556 KB
testcase_43 AC 54 ms
50,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int c = sc.nextInt();
        int v = sc.nextInt();
        ArrayList<ArrayList<Route>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        int[] ss = new int[v];
        for (int i = 0; i < v; i++) {
            ss[i] = sc.nextInt() - 1;
        }
        int[] ts = new int[v];
        for (int i = 0; i < v; i++) {
            ts[i] = sc.nextInt() - 1;
        }
        int[] ys = new int[v];
        for (int i = 0; i < v; i++) {
            ys[i] = sc.nextInt();
        }
        for (int i = 0; i < v; i++) {
            graph.get(ss[i]).add(new Route(ts[i], ys[i], sc.nextInt()));
        }
        PriorityQueue<Route> queue = new PriorityQueue<>();
        queue.add(new Route(0, 0, 0));
        int[][] costs = new int[n][c + 1];
        for (int[] arr : costs) {
            Arrays.fill(arr, Integer.MAX_VALUE);
        }
        while (queue.size() > 0) {
            Route r = queue.poll();
            if (r.yen > c || costs[r.idx][r.yen] <= r.time) {
                continue;
            }
            costs[r.idx][r.yen] = r.time;
            for (Route x : graph.get(r.idx)) {
                queue.add(new Route(x.idx, r.yen + x.yen, r.time + x.time));
            }
        }
        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 Route implements Comparable<Route> {
        int idx;
        int yen;
        int time;
        
        public Route(int idx, int yen, int time) {
            this.idx = idx;
            this.yen = yen;
            this.time = time;
        }
        
        public int compareTo(Route r) {
            return time - r.time;
        }
    }
}
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 nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0