結果

問題 No.1 道のショートカット
ユーザー yuki2006
提出日時 2014-10-11 03:17:28
言語 Java
(openjdk 23)
結果
AC  
実行時間 247 ms / 5,000 ms
コード長 2,797 bytes
コンパイル時間 5,432 ms
コンパイル使用メモリ 86,852 KB
実行使用メモリ 48,468 KB
最終ジャッジ日時 2024-07-20 16:06:22
合計ジャッジ時間 12,757 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Yuki001 {

    public static class Tuple implements Comparable<Tuple> {
        int cost;
        int time;

        Tuple(int cost, int time) {
            this.cost = cost;
            this.time = time;
        }

        @Override
        public int compareTo(Tuple o) {
            return -Integer.compare(cost, o.cost);
        }
    }

    public static void main(String[] args) {


        ArrayList<Integer> si = new ArrayList<Integer>();
        ArrayList<Integer> ti = new ArrayList<Integer>();

        Scanner scanner = new Scanner(System.in);

        int N = scanner.nextInt();
        int C = scanner.nextInt();

        HashMap<Integer, ArrayList<Tuple>> map = new HashMap<>();


        int V = scanner.nextInt();
        int[] cost = new int[V + 1];


        for (int i = 0; i < V; i++) {
            si.add(scanner.nextInt());
        }
        for (int i = 0; i < V; i++) {
            ti.add(scanner.nextInt());
        }
        for (int i = 0; i < V; i++) {
            cost[i] = scanner.nextInt();
        }
        for (int i = 0; i < V; i++) {

            final int key = ti.get(i) * (N + 1) + si.get(i);
            ArrayList<Tuple> tuples = map.get(key);
            if (tuples == null) {
                tuples = new ArrayList<>();
                map.put(key, tuples);
            }
            tuples.add(new Tuple(cost[i], scanner.nextInt()));
        }


        int MAX = Integer.MAX_VALUE / 2;

        int[][] dp = new int[N + 1][C + 1];

        for (int i = 0; i < dp.length; i++) {
            Arrays.fill(dp[i], MAX);
        }
        dp[1][C] = 0;
        for (int i = 2; i < N + 1; i++) {
            for (int k = 1; k < i; k++) {
                final ArrayList<Tuple> tuples = map.get(i * (N + 1) + k);
                if (tuples == null) {
                    continue;
                }
                for (int t = 0; t <= C; t++) {
                    if (dp[k][t] >= MAX) {
                        continue;
                    }
                    for (Tuple tuple : tuples) {

                        int c = t - tuple.cost;
                        if (c < 0) {
                            continue;
                        }
                        dp[i][c] = Math.min(dp[i][c], dp[k][t] + tuple.time);
                    }
                }
            }
        }

        int min = Integer.MAX_VALUE;
        for (int i = C; i >= 0; i--) {
            if (dp[N][i] != MAX) {
                min = Math.min(min, dp[N][i]);
            }
        }

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

        }

    }

}
0