結果
| 問題 | No.614 壊れたキャンパス |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-01-26 14:40:07 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,795 bytes |
| 記録 | |
| コンパイル時間 | 2,660 ms |
| コンパイル使用メモリ | 80,128 KB |
| 実行使用メモリ | 166,372 KB |
| 最終ジャッジ日時 | 2024-06-23 14:17:42 |
| 合計ジャッジ時間 | 12,074 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 11 TLE * 1 -- * 8 |
ソースコード
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] first = br.readLine().split(" ", 5);
int n = Integer.parseInt(first[0]);
int m = Integer.parseInt(first[1]);
int k = Integer.parseInt(first[2]);
int s = Integer.parseInt(first[3]);
int t = Integer.parseInt(first[4]);
ArrayList<ArrayList<Route>> graph = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
String[] line = br.readLine().split(" ", 3);
int a = Integer.parseInt(line[0]) - 1;
int b = Integer.parseInt(line[1]);
int c = Integer.parseInt(line[2]);
graph.get(a).add(new Route(b, c));
}
ArrayList<HashMap<Integer, Long>> costs = new ArrayList<>();
for (int i = 0; i < n; i++) {
costs.add(new HashMap<>());
}
PriorityQueue<Path> queue = new PriorityQueue<>();
queue.add(new Path(0, s, 0));
while (queue.size() > 0) {
Path p = queue.poll();
if (costs.get(p.idx).containsKey(p.floor)) {
continue;
}
costs.get(p.idx).put(p.floor, p.value);
if (p.idx == n - 1) {
continue;
}
for (Route x : graph.get(p.idx)) {
queue.add(new Path(p.idx + 1, x.end, p.value + Math.abs(p.floor - x.start)));
}
}
long min = Long.MAX_VALUE;
for (Map.Entry<Integer, Long> entry : costs.get(n - 1).entrySet()) {
int key = entry.getKey();
long value = entry.getValue();
min = Math.min(min, Math.abs(key - t) + value);
}
if (min == Long.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(min);
}
}
static class Path implements Comparable<Path> {
int idx;
int floor;
long value;
public Path(int idx, int floor, long value) {
this.idx = idx;
this.floor = floor;
this.value = value;
}
public int compareTo(Path another) {
if (value == another.value) {
return 0;
} else if (value < another.value) {
return -1;
} else {
return 1;
}
}
}
static class Route {
int start;
int end;
public Route(int start, int end) {
this.start = start;
this.end = end;
}
}
}
tenten