結果

問題 No.1 道のショートカット
ユーザー 37zigen37zigen
提出日時 2016-03-28 17:24:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 278 ms / 5,000 ms
コード長 1,498 bytes
コンパイル時間 2,094 ms
コンパイル使用メモリ 77,816 KB
実行使用メモリ 60,620 KB
最終ジャッジ日時 2023-09-27 21:54:42
合計ジャッジ時間 12,213 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,880 KB
testcase_01 AC 128 ms
55,952 KB
testcase_02 AC 130 ms
55,804 KB
testcase_03 AC 133 ms
55,788 KB
testcase_04 AC 127 ms
55,592 KB
testcase_05 AC 128 ms
55,988 KB
testcase_06 AC 134 ms
55,864 KB
testcase_07 AC 142 ms
56,156 KB
testcase_08 AC 251 ms
60,608 KB
testcase_09 AC 229 ms
59,548 KB
testcase_10 AC 220 ms
59,644 KB
testcase_11 AC 241 ms
59,608 KB
testcase_12 AC 264 ms
60,408 KB
testcase_13 AC 278 ms
60,452 KB
testcase_14 AC 173 ms
56,088 KB
testcase_15 AC 132 ms
55,944 KB
testcase_16 AC 211 ms
59,180 KB
testcase_17 AC 133 ms
55,592 KB
testcase_18 AC 176 ms
56,372 KB
testcase_19 AC 177 ms
55,964 KB
testcase_20 AC 203 ms
58,280 KB
testcase_21 AC 201 ms
56,408 KB
testcase_22 AC 161 ms
56,164 KB
testcase_23 AC 245 ms
59,596 KB
testcase_24 AC 244 ms
59,928 KB
testcase_25 AC 212 ms
59,228 KB
testcase_26 AC 203 ms
56,948 KB
testcase_27 AC 240 ms
59,728 KB
testcase_28 AC 137 ms
55,852 KB
testcase_29 AC 231 ms
59,308 KB
testcase_30 AC 198 ms
58,260 KB
testcase_31 AC 210 ms
59,308 KB
testcase_32 AC 237 ms
59,568 KB
testcase_33 AC 224 ms
59,112 KB
testcase_34 AC 252 ms
60,620 KB
testcase_35 AC 210 ms
59,128 KB
testcase_36 AC 214 ms
59,148 KB
testcase_37 AC 208 ms
56,308 KB
testcase_38 AC 160 ms
56,152 KB
testcase_39 AC 207 ms
57,176 KB
testcase_40 AC 201 ms
58,272 KB
testcase_41 AC 189 ms
56,312 KB
testcase_42 AC 129 ms
55,936 KB
testcase_43 AC 132 ms
55,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
 public static void main(String[] args){
  Scanner sc=new Scanner(System.in);
  int n=sc.nextInt();//the num of cities
  int c=sc.nextInt();//money
  int v=sc.nextInt();//the num of roads
  int[] s=new int[v];//s->t
  int[] t=new int[v];
  int[] y=new int[v];//cost
  int[] m=new int[v];//time
  int INF=Integer.MAX_VALUE/4;
  for(int i=0;i<v;i++){
   s[i]=sc.nextInt();
  }
  for(int i=0;i<v;i++){
   t[i]=sc.nextInt();
  }
  for(int i=0;i<v;i++){
   y[i]=sc.nextInt();
  }
  for(int i=0;i<v;i++){
   m[i]=sc.nextInt();
  }
  
  List<List<Triple>> edges=new ArrayList<List<Triple>>();
  for(int i=0;i<n;i++){
   edges.add(new ArrayList<Triple>());
  }
  for(int i=0;i<v;i++){
   edges.get(s[i]-1).add(new Triple(t[i]-1,y[i],m[i]));
  }
  int[][] dp=new int[n][c+1];
  for(int i=0;i<n;i++){
   Arrays.fill(dp[i],INF);
  }
  dp[0][0]=0;
  
  for(int i=0;i<n;i++){
   for(int j=0;j<=c;j++){
    for(Triple e:edges.get(i)){
     if(j+e.b<=c)//a:to,b:money,c:time
      dp[e.a][j+e.b]=Math.min(dp[e.a][j+e.b], dp[i][j]+e.c);
//     dp[道の終端][費用]=時間
    }
   }
  }
  int min=INF;
  for(int i=0;i<=c;i++){
   min=Math.min(dp[n-1][i],min);
  }
  if(min==INF){
   System.out.println(-1);
  }else{
   System.out.println(min);
  }
  sc.close();
 }
 private static class Triple{
  int a,b,c;
  Triple(int a,int b,int c){
   this.a=a;
   this.b=b;
   this.c=c;
  }
 }
}

0