結果

問題 No.1 道のショートカット
ユーザー tentententen
提出日時 2023-01-25 17:45:19
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,529 bytes
コンパイル時間 3,896 ms
コンパイル使用メモリ 85,352 KB
実行使用メモリ 52,912 KB
最終ジャッジ日時 2023-09-09 04:01:14
合計ジャッジ時間 8,423 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,480 KB
testcase_01 AC 43 ms
50,220 KB
testcase_02 AC 43 ms
49,572 KB
testcase_03 AC 44 ms
49,640 KB
testcase_04 AC 43 ms
49,712 KB
testcase_05 AC 44 ms
49,988 KB
testcase_06 AC 45 ms
50,164 KB
testcase_07 AC 45 ms
49,716 KB
testcase_08 AC 102 ms
52,496 KB
testcase_09 AC 86 ms
52,300 KB
testcase_10 AC 103 ms
52,352 KB
testcase_11 AC 113 ms
52,504 KB
testcase_12 AC 121 ms
52,596 KB
testcase_13 AC 114 ms
52,308 KB
testcase_14 AC 48 ms
49,700 KB
testcase_15 AC 43 ms
49,764 KB
testcase_16 WA -
testcase_17 AC 43 ms
49,588 KB
testcase_18 AC 50 ms
49,804 KB
testcase_19 AC 49 ms
48,256 KB
testcase_20 AC 61 ms
51,012 KB
testcase_21 WA -
testcase_22 AC 50 ms
49,804 KB
testcase_23 AC 115 ms
52,228 KB
testcase_24 AC 109 ms
52,716 KB
testcase_25 AC 82 ms
52,088 KB
testcase_26 AC 59 ms
50,652 KB
testcase_27 AC 120 ms
52,420 KB
testcase_28 AC 45 ms
49,760 KB
testcase_29 AC 114 ms
50,384 KB
testcase_30 AC 84 ms
52,344 KB
testcase_31 AC 79 ms
52,360 KB
testcase_32 AC 99 ms
52,412 KB
testcase_33 AC 79 ms
52,572 KB
testcase_34 AC 114 ms
52,156 KB
testcase_35 AC 77 ms
50,956 KB
testcase_36 AC 101 ms
52,912 KB
testcase_37 WA -
testcase_38 AC 47 ms
49,652 KB
testcase_39 WA -
testcase_40 AC 67 ms
50,864 KB
testcase_41 AC 48 ms
50,140 KB
testcase_42 AC 42 ms
49,828 KB
testcase_43 AC 43 ms
50,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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();
        int[] starts = new int[v];
        for (int i = 0; i < v; i++) {
            starts[i] = sc.nextInt() - 1;
        }
        int[] goals = new int[v];
        for (int i = 0; i < v; i++) {
            goals[i] = sc.nextInt() - 1;
        }
        int[] costs = new int[v];
        for (int i = 0; i < v; i++) {
            costs[i] = sc.nextInt();
        }
        ArrayList<ArrayList<Path>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < v; i++) {
            int x = sc.nextInt();
            graph.get(starts[i]).add(new Path(goals[i], costs[i], x));
            graph.get(goals[i]).add(new Path(starts[i], costs[i], x));
        }
        PriorityQueue<Path> queue =  new PriorityQueue<>();
        queue.add(new Path(0, c, 0));
        int[][] values = new int[n][c + 1];
        for (int[] arr : values) {
            Arrays.fill(arr, Integer.MAX_VALUE);
        }
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (values[p.idx][p.cost] <= p.time) {
                continue;
            }
            for (int i = p.cost; i >= 0 && values[p.idx][i] > p.time; i--) {
                values[p.idx][i] = p.time;
            }
            for (Path x : graph.get(p.idx)) {
                if (x.cost <= p.cost) {
                    queue.add(new Path(x.idx, p.cost - x.cost, p.time + x.time));
                }
            }
        }
        if (values[n - 1][0] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(values[n - 1][0]);
        }
    }

    static class Path implements Comparable<Path> {
        int idx;
        int cost;
        int time;
        
        public Path(int idx, int cost, int time) {
            this.idx = idx;
            this.cost = cost;
            this.time = time;
        }
        
        public int compareTo(Path another) {
            return time - another.time;
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0