結果
| 問題 |
No.848 なかよし旅行
|
| コンテスト | |
| ユーザー |
htensai
|
| 提出日時 | 2020-05-08 17:38:51 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,198 bytes |
| コンパイル時間 | 2,644 ms |
| コンパイル使用メモリ | 80,072 KB |
| 実行使用メモリ | 89,452 KB |
| 最終ジャッジ日時 | 2024-07-03 09:56:56 |
| 合計ジャッジ時間 | 23,197 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 7 WA * 19 |
ソースコード
import java.util.*;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int p = sc.nextInt() - 1;
int q = sc.nextInt() - 1;
long t = sc.nextInt();
ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new HashMap<>());
}
for (int i = 0; i < m; i++) {
int a = sc.nextInt() - 1;
int b = sc.nextInt() - 1;
int c = sc.nextInt();
graph.get(a).put(b, c);
graph.get(b).put(a, c);
}
PriorityQueue<Path> queue = new PriorityQueue<>();
long[] fromS = new long[n];
queue.add(new Path(0, 0));
search(queue, fromS, graph);
long[] fromP = new long[n];
queue.add(new Path(p, 0));
search(queue, fromP, graph);
long[] fromQ = new long[n];
queue.add(new Path(q, 0));
search(queue, fromQ, graph);
if (fromS[p] + fromS[q] + fromP[q] <= t) {
System.out.println(t);
return;
}
long max = -1;
for (int i = 0; i < n; i++) {
if ((fromS[i] + Math.max(fromP[i], fromQ[i])) * 2 <= t) {
max = Math.max(max, t - Math.max(fromP[i], fromQ[i]) * 2);
}
}
System.out.println(max);
}
static class Path implements Comparable<Path> {
int idx;
long value;
public Path(int idx, long value) {
this.idx = idx;
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 void search(PriorityQueue<Path> queue, long[] costs, ArrayList<HashMap<Integer, Integer>> graph) {
Arrays.fill(costs, Long.MAX_VALUE / 10);
while (queue.size() > 0) {
Path p = queue.poll();
if (costs[p.idx] <= p.value) {
continue;
}
costs[p.idx] = p.value;
for (Map.Entry<Integer, Integer> entry : graph.get(p.idx).entrySet()) {
if (costs[entry.getKey()] == Long.MAX_VALUE / 10) {
queue.add(new Path(entry.getKey(), p.value + entry.getValue()));
}
}
}
}
}
htensai