結果
| 問題 |
No.1 道のショートカット
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-11-10 08:30:57 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 255 ms / 5,000 ms |
| コード長 | 2,874 bytes |
| コンパイル時間 | 2,336 ms |
| コンパイル使用メモリ | 79,468 KB |
| 実行使用メモリ | 57,316 KB |
| 最終ジャッジ日時 | 2024-11-20 16:26:08 |
| 合計ジャッジ時間 | 8,359 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
ソースコード
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int c = sc.nextInt();
int v = sc.nextInt();
ArrayList<ArrayList<Route>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
int[] ss = new int[v];
for (int i = 0; i < v; i++) {
ss[i] = sc.nextInt() - 1;
}
int[] ts = new int[v];
for (int i = 0; i < v; i++) {
ts[i] = sc.nextInt() - 1;
}
int[] ys = new int[v];
for (int i = 0; i < v; i++) {
ys[i] = sc.nextInt();
}
for (int i = 0; i < v; i++) {
graph.get(ss[i]).add(new Route(ts[i], ys[i], sc.nextInt()));
}
PriorityQueue<Route> queue = new PriorityQueue<>();
queue.add(new Route(0, 0, 0));
int[][] costs = new int[n][c + 1];
for (int[] arr : costs) {
Arrays.fill(arr, Integer.MAX_VALUE);
}
while (queue.size() > 0) {
Route r = queue.poll();
if (r.yen > c || costs[r.idx][r.yen] <= r.time) {
continue;
}
costs[r.idx][r.yen] = r.time;
for (Route x : graph.get(r.idx)) {
queue.add(new Route(x.idx, r.yen + x.yen, r.time + x.time));
}
}
int ans = Integer.MAX_VALUE;
for (int x : costs[n - 1]) {
ans = Math.min(ans, x);
}
if (ans == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(ans);
}
}
static class Route implements Comparable<Route> {
int idx;
int yen;
int time;
public Route(int idx, int yen, int time) {
this.idx = idx;
this.yen = yen;
this.time = time;
}
public int compareTo(Route r) {
return time - r.time;
}
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public String nextLine() throws Exception {
return br.readLine();
}
public String next() throws Exception {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten