結果

問題 No.1 道のショートカット
ユーザー htensaihtensai
提出日時 2020-05-11 19:28:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 217 ms / 5,000 ms
コード長 1,926 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 75,932 KB
実行使用メモリ 61,628 KB
最終ジャッジ日時 2023-09-27 22:28:37
合計ジャッジ時間 11,175 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
56,264 KB
testcase_01 AC 113 ms
56,028 KB
testcase_02 AC 115 ms
56,064 KB
testcase_03 AC 122 ms
56,304 KB
testcase_04 AC 116 ms
56,076 KB
testcase_05 AC 115 ms
56,132 KB
testcase_06 AC 123 ms
56,072 KB
testcase_07 AC 128 ms
56,656 KB
testcase_08 AC 186 ms
59,188 KB
testcase_09 AC 188 ms
59,068 KB
testcase_10 AC 203 ms
61,628 KB
testcase_11 AC 201 ms
59,656 KB
testcase_12 AC 213 ms
59,716 KB
testcase_13 AC 217 ms
59,540 KB
testcase_14 AC 163 ms
56,008 KB
testcase_15 AC 114 ms
56,336 KB
testcase_16 AC 169 ms
56,180 KB
testcase_17 AC 114 ms
56,264 KB
testcase_18 AC 158 ms
56,232 KB
testcase_19 AC 158 ms
56,652 KB
testcase_20 AC 173 ms
56,452 KB
testcase_21 AC 170 ms
56,524 KB
testcase_22 AC 137 ms
56,984 KB
testcase_23 AC 217 ms
59,832 KB
testcase_24 AC 210 ms
59,464 KB
testcase_25 AC 178 ms
56,400 KB
testcase_26 AC 174 ms
56,528 KB
testcase_27 AC 212 ms
59,240 KB
testcase_28 AC 124 ms
55,852 KB
testcase_29 AC 214 ms
59,536 KB
testcase_30 AC 181 ms
59,252 KB
testcase_31 AC 190 ms
59,396 KB
testcase_32 AC 189 ms
58,500 KB
testcase_33 AC 186 ms
58,832 KB
testcase_34 AC 211 ms
59,700 KB
testcase_35 AC 200 ms
59,568 KB
testcase_36 AC 195 ms
59,028 KB
testcase_37 AC 197 ms
59,932 KB
testcase_38 AC 137 ms
56,828 KB
testcase_39 AC 164 ms
56,580 KB
testcase_40 AC 182 ms
58,472 KB
testcase_41 AC 169 ms
56,840 KB
testcase_42 AC 118 ms
56,384 KB
testcase_43 AC 114 ms
56,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int c = sc.nextInt();
		int size = sc.nextInt();
		int[] starts = new int[size];
		int[] ends = new int[size];
		int[] costs = new int[size];
		int[] times = new int[size];
		for (int i = 0; i < size; i++) {
		    starts[i] = sc.nextInt() - 1;
		}
		for (int i = 0; i < size; i++) {
		    ends[i] = sc.nextInt() - 1;
		}
		for (int i = 0; i < size; i++) {
		    costs[i] = sc.nextInt();
		}
		for (int i = 0; i < size; i++) {
		    times[i] = sc.nextInt();
		}
		ArrayList<ArrayList<Path>> graph = new ArrayList<>();
		for (int i = 0; i < n; i++) {
		    graph.add(new ArrayList<>());
		}
		for (int i = 0; i < size; i++) {
		    graph.get(starts[i]).add(new Path(ends[i], costs[i], times[i]));
		}
		PriorityQueue<Path> queue = new PriorityQueue<>();
		queue.add(new Path(0, c, 0));
		int[] values = new int[n];
		Arrays.fill(values, Integer.MAX_VALUE);
		while (queue.size() > 0) {
		    Path p = queue.poll();
		    if (values[p.idx] <= p.time) {
		        continue;
		    }
		    values[p.idx] = p.time;
		    for (Path x : graph.get(p.idx)) {
		        if (x.cost > p.cost) {
		            continue;
		        }
		        queue.add(new Path(x.idx, p.cost - x.cost, p.time + x.time));
		    }
		}
		if (values[n - 1] == Integer.MAX_VALUE) {
		    System.out.println(-1);
		} else {
		    System.out.println(values[n - 1]);
		}
	}
	
	static class Path implements Comparable<Path> {
	    int idx;
	    int cost;
	    int time;
	    
	    public Path(int idx, int cost, int time) {
	        this.idx = idx;
	        this.cost = cost;
	        this.time = time;
	    }
	    
	    public int compareTo(Path another) {
	        if (cost == another.cost) {
	            return time - another.time;
	        } else {
	            return another.cost - cost;
	        }
	    }
	}
}
0