結果
| 問題 |
No.848 なかよし旅行
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-07-05 21:58:28 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 246 ms / 2,000 ms |
| コード長 | 1,708 bytes |
| コンパイル時間 | 1,094 ms |
| コンパイル使用メモリ | 132,488 KB |
| 実行使用メモリ | 19,308 KB |
| 最終ジャッジ日時 | 2024-06-22 01:51:25 |
| 合計ジャッジ時間 | 4,222 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long MOD = 10^^9 + 7;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto P = s[2] - 1;
auto Q = s[3] - 1;
auto T = s[4].to!long;
auto G = new Tuple!(int, long)[][](N);
foreach (_; 0..M) {
s = readln.split.map!(to!int);
auto u = s[0] - 1;
auto v = s[1] - 1;
auto c = s[2].to!long;
G[u] ~= tuple(v, c);
G[v] ~= tuple(u, c);
}
auto d0 = dijkstra(N, 0, G);
auto dp = dijkstra(N, P, G);
auto dq = dijkstra(N, Q, G);
if (d0[P] + dp[Q] + dq[0] <= T) {
writeln(T);
return;
}
long ans = -1;
foreach (a; 0..N) {
foreach (b; 0..N) {
long time = d0[a] + max(dp[a] + dp[b], dq[a] + dq[b]) + d0[b];
if (time > T) continue;
ans = max(ans, T - max(dp[a] + dp[b], dq[a] + dq[b]));
}
}
writeln(ans);
}
long[] dijkstra(int N, int s, ref Tuple!(int, long)[][] G) {
auto dist = new long[](N);
dist[] = 1L << 59;
auto pq = new BinaryHeap!(Array!(Tuple!(int, long)), "a[1] > b[1]");
pq.insert(tuple(s, 0L));
while (!pq.empty) {
auto n = pq.front[0];
auto d = pq.front[1];
pq.removeFront;
if (dist[n] <= d) continue;
dist[n] = d;
foreach (to; G[n]) {
auto m = to[0];
auto nd = d + to[1];
if (dist[m] <= nd) continue;
pq.insert(tuple(m, nd));
}
}
return dist;
}