結果
問題 |
No.17 2つの地点に泊まりたい
|
ユーザー |
|
提出日時 | 2016-08-27 22:08:42 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 351 ms / 5,000 ms |
コード長 | 1,472 bytes |
コンパイル時間 | 797 ms |
コンパイル使用メモリ | 128,672 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-12 03:46:53 |
合計ジャッジ時間 | 3,235 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 |
ソースコード
import std.algorithm, std.array, std.container, std.range; import std.string, std.conv, std.math; import std.stdio, std.typecons; alias Tuple!(int, "v", int, "c") side; void main() { auto n = readln.chomp.to!int; auto sti = iota(n).map!(i => readln.chomp.to!int).array; auto graph = new side[][n]; auto m = readln.chomp.to!int; foreach (i; 0..m) { auto rd = readln.split.map!(to!int); graph[rd[0]] ~= side(rd[1], rd[2]); graph[rd[1]] ~= side(rd[0], rd[2]); } auto min = int.max; foreach (s1; 1..(n - 1)) { foreach (s2; 1..(n - 1)) { if (s1 == s2) continue; auto r1 = dijkstra(n, 0, s1, graph); auto r2 = dijkstra(n, s1, s2, graph); auto r3 = dijkstra(n, s2, n - 1, graph); if (r1 > 0 && r2 > 0 && r3 > 0) { auto r = r1 + r2 + r3 + sti[s1] + sti[s2]; if (r < min) min = r; } } } writeln(min); } int dijkstra(int n, int s, int e, side[][] graph) { auto memo = new int[n]; auto visited = new bool[n]; visited[s] = true; auto pq = Array!side().heapify!("a.c > b.c"); foreach (si; graph[s]) pq.insert(si); while (!pq.empty) { auto si = pq.front; pq.removeFront; if (visited[si.v]) continue; if (si.v == e) return si.c; memo[si.v] = si.c; visited[si.v] = true; foreach (c; graph[si.v]) { if (!visited[c.v]) { pq.insert(side(c.v, si.c + c.c)); } } } return -1; }