結果

問題 No.1 道のショートカット
ユーザー 37zigen
提出日時 2016-03-28 17:24:11
言語 Java
(openjdk 23)
結果
AC  
実行時間 243 ms / 5,000 ms
コード長 1,498 bytes
コンパイル時間 2,149 ms
コンパイル使用メモリ 78,684 KB
実行使用メモリ 47,252 KB
最終ジャッジ日時 2024-07-20 16:22:38
合計ジャッジ時間 10,425 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

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