結果

問題 No.1 道のショートカット
ユーザー jp_stejp_ste
提出日時 2016-03-03 20:04:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 332 ms / 5,000 ms
コード長 2,836 bytes
コンパイル時間 1,795 ms
コンパイル使用メモリ 74,584 KB
実行使用メモリ 60,328 KB
最終ジャッジ日時 2023-09-27 21:53:14
合計ジャッジ時間 11,201 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
56,168 KB
testcase_01 AC 108 ms
55,928 KB
testcase_02 AC 106 ms
55,828 KB
testcase_03 AC 111 ms
55,596 KB
testcase_04 AC 106 ms
54,012 KB
testcase_05 AC 108 ms
56,192 KB
testcase_06 AC 116 ms
55,640 KB
testcase_07 AC 113 ms
55,888 KB
testcase_08 AC 295 ms
60,024 KB
testcase_09 AC 200 ms
59,380 KB
testcase_10 AC 265 ms
59,920 KB
testcase_11 AC 294 ms
60,328 KB
testcase_12 AC 332 ms
60,152 KB
testcase_13 AC 321 ms
59,972 KB
testcase_14 AC 123 ms
55,748 KB
testcase_15 AC 107 ms
54,028 KB
testcase_16 AC 161 ms
57,336 KB
testcase_17 AC 110 ms
55,732 KB
testcase_18 AC 147 ms
55,800 KB
testcase_19 AC 126 ms
55,876 KB
testcase_20 AC 181 ms
57,216 KB
testcase_21 AC 159 ms
57,068 KB
testcase_22 AC 120 ms
56,160 KB
testcase_23 AC 290 ms
60,112 KB
testcase_24 AC 295 ms
60,076 KB
testcase_25 AC 186 ms
56,984 KB
testcase_26 AC 166 ms
57,128 KB
testcase_27 AC 299 ms
59,912 KB
testcase_28 AC 114 ms
55,880 KB
testcase_29 AC 222 ms
59,648 KB
testcase_30 AC 164 ms
57,388 KB
testcase_31 AC 178 ms
58,884 KB
testcase_32 AC 269 ms
60,072 KB
testcase_33 AC 218 ms
57,964 KB
testcase_34 AC 287 ms
59,656 KB
testcase_35 AC 163 ms
58,668 KB
testcase_36 AC 197 ms
59,936 KB
testcase_37 AC 192 ms
59,940 KB
testcase_38 AC 129 ms
55,952 KB
testcase_39 AC 154 ms
56,676 KB
testcase_40 AC 166 ms
56,620 KB
testcase_41 AC 150 ms
56,496 KB
testcase_42 AC 111 ms
55,768 KB
testcase_43 AC 110 ms
55,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            int N = scan.nextInt();
            int C = scan.nextInt();
            int V = scan.nextInt();
            
            int inFrom[] = new int[V];
            int inTo[] = new int[V];
            int inCost[] = new int[V];
            int inTime[] = new int[V];
            
            for(int i=0; i<V; i++) inFrom[i] = Integer.parseInt(scan.next());
            for(int i=0; i<V; i++) inTo[i] = Integer.parseInt(scan.next());
            for(int i=0; i<V; i++) inCost[i] = Integer.parseInt(scan.next());
            for(int i=0; i<V; i++) inTime[i] = Integer.parseInt(scan.next());
            
            ArrayList<Path> pathList = new ArrayList<>();
            for(int i=0; i<V; i++) {
                Path path = new Path(inFrom[i], inTo[i], inCost[i], inTime[i]);
                pathList.add(path);
            }
                        
            int minTime[][] = new int[N+1][C+1];
            for(int i=1; i<=N; i++) {
                for(int j=0; j<=C; j++) {
                    if(i==1 && j==C) {
                        minTime[i][j] = 0;
                    } else {
                        minTime[i][j] = Integer.MAX_VALUE;
                    }
                }
            }
            
            for(int place = 1; place <= N; place++) { 
                for(int i=C; i>=0; i--) {
                    if(minTime[place][i] != Integer.MAX_VALUE) {
                        int cost = i;
                        int time = minTime[place][i];
                        
                        for(Path path : pathList) {
                            if(path.start == place) {
                                int nextPlace = path.end;
                                int nextCost = cost - path.cost;
                                int nextTime = time + path.time;
                                if(nextCost < 0) continue;
                                minTime[nextPlace][nextCost] = Math.min(minTime[nextPlace][nextCost], nextTime);
                            }
                        }
                    }
                }
            }
            
            int ans = Integer.MAX_VALUE;
            for(int i=0; i<=C; i++) {
                ans = Math.min(minTime[N][i], ans);
            }
            
            if(ans == Integer.MAX_VALUE) {
                System.out.println(-1);
            } else {
                System.out.println(ans);
            }
        }
    }
}

class Path {
    int start;
    int end;
    int cost;
    int time;
    Path(int start, int end, int cost, int time) {
        this.start = start;
        this.end = end;
        this.cost = cost;
        this.time = time;
    }
}
0