結果

問題 No.1 道のショートカット
ユーザー Grenache
提出日時 2016-03-04 23:44:43
言語 Java
(openjdk 23)
結果
AC  
実行時間 249 ms / 5,000 ms
コード長 1,933 bytes
コンパイル時間 3,655 ms
コンパイル使用メモリ 78,172 KB
実行使用メモリ 47,564 KB
最終ジャッジ日時 2024-07-20 16:21:13
合計ジャッジ時間 11,658 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_yukicoder1 {

    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();
        }
        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]));
        }

        final int INF = Integer.MAX_VALUE / 2;
        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) {
        				dp[e.a][j + e.b] = Math.min(dp[e.a][j + e.b], dp[i][j] + e.c);
        			}
        		}
        	}
        }

        int min = INF;
        for (int i = 0; i <= c; i++) {
        	min = Math.min(min, dp[n - 1][i]);
        }

        if (min == INF) {
        	System.out.println(-1);
        } else {
        	System.out.println(min);
        }

        sc.close();
    }

    private static class Triple {
    	int a;
    	int b;
    	int c;

    	Triple(int a, int b, int c) {
    		this.a = a;
    		this.b = b;
    		this.c = c;
    	}
    }
}
0