結果

問題 No.1 道のショートカット
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-04-03 06:55:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 234 ms / 5,000 ms
コード長 2,476 bytes
コンパイル時間 5,061 ms
コンパイル使用メモリ 76,080 KB
実行使用メモリ 61,180 KB
最終ジャッジ日時 2023-09-27 21:42:05
合計ジャッジ時間 14,861 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,608 KB
testcase_01 AC 129 ms
55,900 KB
testcase_02 AC 129 ms
55,724 KB
testcase_03 AC 134 ms
55,636 KB
testcase_04 AC 128 ms
55,684 KB
testcase_05 AC 129 ms
56,152 KB
testcase_06 AC 137 ms
56,144 KB
testcase_07 AC 142 ms
55,848 KB
testcase_08 AC 217 ms
57,216 KB
testcase_09 AC 207 ms
58,432 KB
testcase_10 AC 207 ms
58,884 KB
testcase_11 AC 225 ms
59,636 KB
testcase_12 AC 234 ms
60,140 KB
testcase_13 AC 233 ms
59,036 KB
testcase_14 AC 165 ms
56,184 KB
testcase_15 AC 128 ms
58,104 KB
testcase_16 AC 192 ms
56,096 KB
testcase_17 AC 128 ms
55,408 KB
testcase_18 AC 171 ms
56,056 KB
testcase_19 AC 157 ms
56,244 KB
testcase_20 AC 194 ms
56,400 KB
testcase_21 AC 182 ms
56,376 KB
testcase_22 AC 154 ms
54,228 KB
testcase_23 AC 231 ms
59,760 KB
testcase_24 AC 232 ms
59,740 KB
testcase_25 AC 194 ms
56,496 KB
testcase_26 AC 190 ms
56,400 KB
testcase_27 AC 225 ms
59,244 KB
testcase_28 AC 134 ms
56,028 KB
testcase_29 AC 228 ms
59,776 KB
testcase_30 AC 194 ms
56,064 KB
testcase_31 AC 204 ms
58,980 KB
testcase_32 AC 214 ms
59,108 KB
testcase_33 AC 200 ms
57,860 KB
testcase_34 AC 228 ms
60,076 KB
testcase_35 AC 208 ms
58,740 KB
testcase_36 AC 211 ms
61,180 KB
testcase_37 AC 205 ms
58,912 KB
testcase_38 AC 156 ms
55,828 KB
testcase_39 AC 177 ms
56,328 KB
testcase_40 AC 194 ms
59,112 KB
testcase_41 AC 182 ms
55,964 KB
testcase_42 AC 130 ms
56,256 KB
testcase_43 AC 128 ms
56,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//No.1 道のショートカット

import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;

public class No1 {
    
    static final Scanner sc = new Scanner(System.in);
    static final PrintWriter out = new PrintWriter(System.out,false);

    static void solve() {
        int n = sc.nextInt();
        int c = sc.nextInt();
        int v = sc.nextInt();
        int[] from = new int[v];
        int[] to = new int[v];
        int[] cost = new int[v];
        int[] time = new int[v];
        for (int i=0; i<v; i++) from[i] = sc.nextInt()-1;
        for (int i=0; i<v; i++) to[i] = sc.nextInt()-1;
        for (int i=0; i<v; i++) cost[i] = sc.nextInt();
        for (int i=0; i<v; i++) time[i] = sc.nextInt();

        int[][][] cg = toAdjacencyList(n,from,to,cost);
        int[][][] tg = toAdjacencyList(n,from,to,time);

        int[][] dp = new int[n][c+1];
        for (int i=0; i<n; i++) fill(dp[i],Integer.MAX_VALUE);
        dp[0][0] = 0;
        for (int i=0; i<n; i++) {
            for (int j=0; j<=c; j++) {
                if (dp[i][j] == Integer.MAX_VALUE) continue;
                for (int k=0; k<cg[i].length; k++) {
                    int next = cg[i][k][0];
                    int ncost = cg[i][k][1];
                    int ntime = tg[i][k][1];
                    if (j + ncost <= c) {
                        dp[next][j+ncost] = min(dp[next][j+ncost], dp[i][j]+ntime);
                    }
                }
            }
        }

        int min = Integer.MAX_VALUE;
        for (int i=0; i<=c; i++) {
            min = min(min,dp[n-1][i]);
        }
        out.println(min==Integer.MAX_VALUE?"-1":min);
    }

    static int[][][] toAdjacencyList(int n,int[] from, int[] to, int[] cost) {
        int[] cnt = new int[n];
        for (int i : from) cnt[i]++;
    
        int[][][] g = new int[n][][];
        for (int i=0; i<n; i++) g[i] = new int[cnt[i]][2];
        for (int i=0; i<from.length; i++) {
            int f = from[i];
            int t = to[i];
            g[f][--cnt[f]][0] = t;
            g[f][cnt[f]][1] = cost[i];
        }
    
        return g;
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        solve();
        out.flush();

        long end = System.currentTimeMillis();
        //trace(end-start + "ms");
        sc.close();
    }

    static void trace(Object... o) { System.out.println(deepToString(o));}
}
0