結果
問題 | No.468 役に立つ競技プログラミング実践編 |
ユーザー |
|
提出日時 | 2017-12-19 17:49:10 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 373 ms / 2,000 ms |
コード長 | 1,503 bytes |
コンパイル時間 | 909 ms |
コンパイル使用メモリ | 110,208 KB |
実行使用メモリ | 31,232 KB |
最終ジャッジ日時 | 2024-06-12 23:13:42 |
合計ジャッジ時間 | 7,462 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 31 |
other | AC * 6 |
ソースコード
import std.algorithm, std.conv, std.range, std.stdio, std.string; alias graph = Graph!int; void main() { auto rd = readln.split.to!(int[]), n = rd[0], m = rd[1]; struct Edge { int a, b, c; } auto e = new Edge[](m); foreach (i; 0..m) { auto rd2 = readln.splitter; auto a = rd2.front.to!int; rd2.popFront(); auto b = rd2.front.to!int; rd2.popFront(); auto c = rd2.front.to!int; e[i] = Edge(a, b, c); } auto g = new int[][](n), fg = new Edge[][](n), rg = new Edge[][](n); foreach (ei; e) { g[ei.a] ~= ei.b; fg[ei.a] ~= ei; rg[ei.b] ~= ei; } auto t = graph.topologicalSort(g); auto fct = new int[](n), lct = new int[](n); foreach (ti; t[1..$]) fct[ti] = rg[ti].map!(ej => fct[ej.a] + ej.c).maxElement; lct[n-1] = fct[n-1]; foreach_reverse (ti; t[0..$-1]) lct[ti] = fg[ti].map!(ej => lct[ej.b] - ej.c).minElement; auto y = 0; foreach (i; 0..n) y += fct[i] != lct[i]; writeln(fct[n-1], " ", y, "/", n); } template Graph(Node) { import std.container; Node[] topologicalSort(Node[][] g) { auto n = cast(Node)(g.length), h = new size_t[](n); foreach (u; 0..n) foreach (v; g[u]) ++h[v]; auto st = SList!Node(); foreach (i; 0..n) if (h[i] == 0) st.insertFront(i); Node[] ans; while (!st.empty()) { auto u = st.front; st.removeFront(); ans ~= u; foreach (v; g[u]) { --h[v]; if (h[v] == 0) st.insertFront(v); } } return ans; } }