結果
| 問題 |
No.614 壊れたキャンパス
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-01-11 15:07:27 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 1,129 ms / 2,000 ms |
| コード長 | 1,741 bytes |
| コンパイル時間 | 983 ms |
| コンパイル使用メモリ | 142,592 KB |
| 実行使用メモリ | 174,136 KB |
| 最終ジャッジ日時 | 2024-06-12 23:29:19 |
| 合計ジャッジ時間 | 14,456 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
ソースコード
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;
immutable long INF = 1L << 60;
alias Edge = Tuple!(int, "bld", int, "floor");
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto K = s[2];
auto S = s[3];
auto T = s[4];
auto G = new Edge[][int][](N);
auto floors = new int[][](N);
foreach (_; 0..M) {
s = readln.split.map!(to!int);
G[s[0]-1][s[1]] ~= Edge(s[0], s[2]);
floors[s[0]-1] ~= s[1];
floors[s[0]] ~= s[2];
}
floors[0] ~= S;
floors[N-1] ~= T;
foreach (i; 0..N) {
floors[i].sort();
foreach (j; 0..floors[i].length.to!int-1) {
G[i][floors[i][j]] ~= Edge(i, floors[i][j+1]);
G[i][floors[i][j+1]] ~= Edge(i, floors[i][j]);
}
}
auto dist = new long[int][](N);
foreach (i; 0..N)
foreach (j; floors[i])
dist[i][j] = INF;
auto pq = new BinaryHeap!(Array!(Tuple!(int, int, long)), "a[2] > b[2]");
pq.insert(tuple(0, S, 0L));
while (!pq.empty) {
auto tt = pq.front;
pq.removeFront;
auto b = tt[0];
auto f = tt[1];
auto cost = tt[2];
if (dist[b][f] <= cost) continue;
dist[b][f] = cost;
if (!(f in G[b])) continue;
foreach (e; G[b][f]) {
auto nb = e.bld;
auto nf = e.floor;
auto nc = cost + (b == nb ? abs(f-nf) : 0L);
if (nc < dist[nb][nf]) pq.insert(tuple(nb, nf, nc));
}
}
if (dist[N-1][T] >= INF) dist[N-1][T] = -1;
dist[N-1][T].writeln;
}