F - Virus 2 Editorial by evima

別解

部屋 \(i\) が最初に感染した日を \(d_i\) 日目とし、\(d_i - 1\) 日目までに感染していた部屋から部屋 \(i\) までの最短距離を \(e_i\) として、組 \((d_i, e_i)\) をダイクストラ法の要領で計算することを考えます。(下の実装例を見てください。)問題は伝播が翌日以降に繰り越されるときですが、\(d\)\(w\) が与えられたときに \(d+1\) 日目以降で \(X_j\)\(w\) 以上であるような最も早い日を高速で求められるように前準備をしておけば間に合います。

主要部分の実装例(Python。コード全体は https://atcoder.jp/contests/abc307/submissions/42933837

c = [(D + 1, 0) for _ in range(N)]
for a in A:
    c[a] = 0, 0
q = [(0, 0, a) for a in A]
while len(q):
    d, e, u = heapq.heappop(q)
    if (d, e) > c[u]:
        continue
    for v, w in g[u]:
        if e + w <= X[d]:
            nd, ne = d, e + w
        else:
            nd, ne = find(d + 1, w), w
        if (nd, ne) < c[v]:
            c[v] = nd, ne
            heapq.heappush(q, (nd, ne, v))
for d, e in c:
    print(d if d <= D else -1)

posted:
last update: