結果

問題 No.1 道のショートカット
ユーザー t8m8⛄️
提出日時 2015-04-03 06:55:38
言語 Java
(openjdk 23)
結果
AC  
実行時間 249 ms / 5,000 ms
コード長 2,476 bytes
コンパイル時間 3,807 ms
コンパイル使用メモリ 78,932 KB
実行使用メモリ 46,280 KB
最終ジャッジ日時 2024-07-20 16:12:14
合計ジャッジ時間 13,434 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

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