結果
| 問題 |
No.848 なかよし旅行
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-08-31 18:26:03 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,766 ms / 2,000 ms |
| コード長 | 2,594 bytes |
| コンパイル時間 | 2,240 ms |
| コンパイル使用メモリ | 80,096 KB |
| 実行使用メモリ | 88,900 KB |
| 最終ジャッジ日時 | 2024-11-16 20:25:01 |
| 合計ジャッジ時間 | 24,387 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
import java.util.*;
public class Main {
static final long MAX = Long.MAX_VALUE / 10;
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;
int 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<>();
queue.add(new Path(p, 0));
long[] pCosts = getCost(n, queue, graph);
queue.add(new Path(q, 0));
long[] qCosts = getCost(n, queue, graph);
queue.add(new Path(0, 0));
long[] zCosts = getCost(n, queue, graph);
if (Math.max(pCosts[0], qCosts[0]) * 2 > t) {
System.out.println(-1);
return;
}
if (pCosts[0] + pCosts[q] + qCosts[0] <= t) {
System.out.println(t);
return;
}
long max = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (Math.max(pCosts[i] + pCosts[j], qCosts[i] + qCosts[j]) + zCosts[i] + zCosts[j] <= t) {
max = Math.max(max, t - Math.max(pCosts[i] + pCosts[j], qCosts[i] + qCosts[j]));
}
}
}
System.out.println(max);
}
static long[] getCost(int size, PriorityQueue<Path> queue, ArrayList<HashMap<Integer, Integer>> graph) {
long[] costs = new long[size];
Arrays.fill(costs, MAX);
while (queue.size() > 0) {
Path p = queue.poll();
if (costs[p.idx] <= p.value) {
continue;
}
costs[p.idx] = p.value;
for (int x : graph.get(p.idx).keySet()) {
queue.add(new Path(x, p.value + graph.get(p.idx).get(x)));
}
}
return costs;
}
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;
}
}
}
}
tenten