結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-02-06 13:54:11 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,563 bytes |
| コンパイル時間 | 1,125 ms |
| コンパイル使用メモリ | 127,968 KB |
| 実行使用メモリ | 10,012 KB |
| 最終ジャッジ日時 | 2024-06-12 06:53:53 |
| 合計ジャッジ時間 | 7,763 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 3 TLE * 1 -- * 22 |
ソースコード
import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap
void main()
{
auto rd1 = readln.split.to!(size_t[]), n = rd1[0], m = rd1[1], s = rd1[2], g = rd1[3];
auto aij = new int[][](n, n);
foreach (_; m.iota) {
auto rd2 = readln.split, a = rd2[0].to!size_t, b = rd2[1].to!size_t, c = rd2[2].to!int;
aij[a][b] = aij[b][a] = c;
}
auto mi = dijkstra(aij, s)[g];
size_t[] calc(int mi) {
struct qitem {
size_t[] route;
int rest;
}
size_t[][] r;
auto qi = SList!qitem(qitem([s], mi));
while (!qi.empty) {
auto q = qi.front; qi.removeFront;
foreach (np; n.iota) {
if (aij[q.route.back][np] == 0 || q.route.canFind(np)) continue;
auto nr = q.rest - aij[q.route.back][np];
if (np == g && nr == 0) r ~= q.route ~ np;
if (nr <= 0) continue;
qi.insertFront(qitem(q.route ~ np, nr));
}
}
return r.fold!min;
}
writeln(calc(mi).to!(string[]).join(" "));
}
struct Edge(T) {
size_t v;
T w;
}
T[] dijkstra(T)(T[][] aij, size_t s, T inf = 0) {
auto n = aij.length;
auto di = new T[](n);
di[] = inf;
auto qi = heapify!("a.w > b.w")(Array!(Edge!T)());
void addNext(Edge!T e) {
auto v = e.v, w = e.w;
di[v] = w;
foreach (i; n.iota)
if (aij[v][i] != inf && di[i] == inf)
qi.insert(Edge!T(i, w + aij[v][i]));
}
addNext(Edge!T(s, 0));
while (!qi.empty) {
auto e = qi.front; qi.removeFront;
if (di[e.v] == inf) addNext(e);
}
return di;
}