結果

問題 No.1 道のショートカット
ユーザー mastersatoshimastersatoshi
提出日時 2015-07-24 02:18:00
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,041 bytes
コンパイル時間 2,142 ms
コンパイル使用メモリ 75,308 KB
実行使用メモリ 53,504 KB
最終ジャッジ日時 2023-09-22 12:24:55
合計ジャッジ時間 6,293 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,368 KB
testcase_01 AC 43 ms
49,344 KB
testcase_02 AC 44 ms
49,472 KB
testcase_03 AC 44 ms
49,404 KB
testcase_04 WA -
testcase_05 AC 44 ms
49,452 KB
testcase_06 AC 44 ms
49,660 KB
testcase_07 AC 45 ms
49,396 KB
testcase_08 AC 98 ms
52,976 KB
testcase_09 AC 62 ms
50,696 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 43 ms
49,364 KB
testcase_16 WA -
testcase_17 AC 43 ms
49,584 KB
testcase_18 AC 47 ms
49,360 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 47 ms
49,488 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 53 ms
49,508 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 43 ms
49,672 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 60 ms
50,796 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 45 ms
49,452 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 43 ms
49,472 KB
testcase_43 AC 43 ms
49,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());
        int C = Integer.parseInt(br.readLine());
        int V = Integer.parseInt(br.readLine());

        int[] S = convert(br.readLine().split(" "));
        int[] T = convert(br.readLine().split(" "));
        int[] Y = convert(br.readLine().split(" "));
        int[] M = convert(br.readLine().split(" "));

        int[][] cost = new int[N + 1][N + 1];
        int[][] time = new int[N + 1][N + 1];

        //経路形成(双方向)
        for (int i = 0; i < V; i++) {
            cost[S[i]][T[i]] = Y[i];

            time[S[i]][T[i]] = M[i];
        }

        ArrayList<int[]> next = new ArrayList();
        for (int i = 2; i <= N; i++) {
            if (cost[1][i] != 0) {
                int[] tmp = new int[4];

                tmp[0] = 1;
                tmp[1] = i;
                tmp[2] = cost[1][i];
                tmp[3] = time[1][i];

                next.add(tmp);
            }
        }

        next.sort(new CustomComparator());

        int[] minCost = new int[N + 1];
        int[] minTime = new int[N + 1];

        Arrays.fill(minCost, Integer.MAX_VALUE);
        Arrays.fill(minTime, Integer.MAX_VALUE);

        minCost[1] = 0;
        minTime[1] = 0;

        while (!next.isEmpty()) {
            int[] tmp = next.get(0);
            next.remove(0);

            if (minCost[tmp[0]] + tmp[2] <= C
                    && minTime[tmp[1]] > minTime[tmp[0]] + tmp[3]) {
                minCost[tmp[1]] = minCost[tmp[0]] + tmp[2];
                minTime[tmp[1]] = minTime[tmp[0]] + tmp[3];

                for (int i = 1; i <= N; i++) {

                    if (cost[tmp[1]][i] != 0) {
                        int[] tmp2 = new int[4];

                        tmp2[0] = tmp[1];
                        tmp2[1] = i;
                        tmp2[2] = cost[tmp[1]][i];
                        tmp2[3] = time[tmp[1]][i];

                        next.add(tmp2);
                    }
                }

                next.sort(new CustomComparator());
            }
        }

        if (minCost[N] <= C) {
            System.out.println(minTime[N]);
        } else {
            System.out.println(-1);
        }
    }

    private static int[] convert(String[] source) {
        int[] dest = new int[source.length];

        for (int i = 0; i < source.length; i++) {
            dest[i] = Integer.parseInt(source[i]);
        }

        return dest;
    }

    static class CustomComparator implements Comparator<int[]> {

        @Override
        public int compare(int[] a, int[] b) {
            int no1 = a[2];
            int no2 = b[2];

            if (no1 > no2) {
                return 1;

            } else if (no1 == no2) {
                return 0;

            } else {
                return -1;

            }
        }

    }
}
0