結果

問題 No.1 道のショートカット
ユーザー takeya_okinotakeya_okino
提出日時 2017-06-16 22:43:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 258 ms / 5,000 ms
コード長 1,947 bytes
コンパイル時間 3,912 ms
コンパイル使用メモリ 74,700 KB
実行使用メモリ 61,204 KB
最終ジャッジ日時 2023-09-27 22:08:05
合計ジャッジ時間 12,478 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,744 KB
testcase_01 AC 126 ms
55,820 KB
testcase_02 AC 129 ms
53,800 KB
testcase_03 AC 134 ms
56,052 KB
testcase_04 AC 129 ms
56,152 KB
testcase_05 AC 127 ms
55,696 KB
testcase_06 AC 133 ms
55,884 KB
testcase_07 AC 141 ms
55,788 KB
testcase_08 AC 235 ms
59,868 KB
testcase_09 AC 222 ms
57,496 KB
testcase_10 AC 227 ms
60,000 KB
testcase_11 AC 228 ms
60,004 KB
testcase_12 AC 258 ms
61,204 KB
testcase_13 AC 247 ms
59,876 KB
testcase_14 AC 157 ms
53,960 KB
testcase_15 AC 127 ms
55,692 KB
testcase_16 AC 205 ms
56,720 KB
testcase_17 AC 129 ms
56,140 KB
testcase_18 AC 159 ms
56,020 KB
testcase_19 AC 166 ms
56,248 KB
testcase_20 AC 199 ms
56,284 KB
testcase_21 AC 194 ms
56,520 KB
testcase_22 AC 158 ms
57,908 KB
testcase_23 AC 242 ms
60,016 KB
testcase_24 AC 233 ms
59,916 KB
testcase_25 AC 208 ms
57,040 KB
testcase_26 AC 196 ms
56,824 KB
testcase_27 AC 235 ms
59,252 KB
testcase_28 AC 134 ms
55,852 KB
testcase_29 AC 227 ms
59,836 KB
testcase_30 AC 195 ms
58,820 KB
testcase_31 AC 214 ms
58,532 KB
testcase_32 AC 225 ms
59,676 KB
testcase_33 AC 217 ms
58,656 KB
testcase_34 AC 240 ms
59,680 KB
testcase_35 AC 211 ms
58,852 KB
testcase_36 AC 215 ms
58,920 KB
testcase_37 AC 205 ms
58,876 KB
testcase_38 AC 149 ms
55,748 KB
testcase_39 AC 188 ms
56,852 KB
testcase_40 AC 181 ms
58,180 KB
testcase_41 AC 175 ms
55,904 KB
testcase_42 AC 131 ms
55,524 KB
testcase_43 AC 132 ms
55,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int C = sc.nextInt();
    int V = sc.nextInt();
    int[] s = new int[V];
    int[] t = new int[V];
    int[] y = new int[V];
    int[] m = new int[V];
    for(int i = 0; i < V; i++) {
      s[i] = sc.nextInt() - 1;
    }
    for(int i = 0; i < V; i++) {
      t[i] = sc.nextInt() - 1;
    }
    for(int i = 0; i < V; i++) {
      y[i] = sc.nextInt();
    }
    for(int i = 0; i < V; i++) {
      m[i] = sc.nextInt();
    }
    ArrayList<Edge>[] edge = new ArrayList[N];
    for(int i = 0; i < N; i++) {
      edge[i] = new ArrayList<Edge>();
    }
    for(int i = 0; i < V; i++) {
      Edge e = new Edge(s[i], t[i], y[i], m[i]);
      edge[t[i]].add(e);
    }
    // dp[i][j]は頂点iで金額j円持っている状態に行く最小時間を表す(不可能なら-1とする)
    int[][] dp = new int[N][C + 1];
    for(int j = 0; j < C; j++) {
      dp[0][j] = -1;
    }
    for(int i = 1; i < N; i++) {
      for(int j = 0; j < C + 1; j++) {
        int min = -1;
        for(int k = 0; k < edge[i].size(); k++) {
          Edge e = edge[i].get(k);
          if(j + e.cost < C + 1) {
            if(dp[e.s][j + e.cost] != -1) {
              if(min == -1) {
                min = dp[e.s][j + e.cost] + e.time;
              } else {
                min = Math.min(min, dp[e.s][j + e.cost] + e.time);
              }
            }
          }
        }
        dp[i][j] = min;
      }
    }
    int ans = Integer.MAX_VALUE;
    for(int j = 0; j < C + 1; j++) {
      if(dp[N - 1][j] > 0) ans = Math.min(ans, dp[N - 1][j]);
    }
    if(ans == Integer.MAX_VALUE) ans = -1;
    System.out.println(ans);
  }
}

class Edge {
  int s;
  int t;
  int cost;
  int time;
  Edge(int s, int t, int cost, int time) {
    this.s = s;
    this.t = t;
    this.cost = cost;
    this.time = time;
  }
}
0