PAST1J
J - Groundwork
https://gyazo.com/35e2c2626e0ea16ae8009726cef684e2
Thoughts.
Since there is a transit point, find the distance from the Y intersection to the three points and choose the one with the smallest sum.
The cost is on the vertices, not on the edges, but we can just search from the least expensive one in the Dijkstra method sense.
O(V^2logV), so we can make it in time.
Official Explanation
Steiner tree
AC
Construct a graph and use Dijkstra method to find one_to_all distances from three endpoints
This counts three intersections, so subtract two.
Answer the minimum cost of the Y-junction obtained
code:python
def solve(H, W, AS):
from collections import defaultdict
edges = defaultdict(dict)
for x in range(W):
for y in range(H):
pos = y * W + x
if x < W - 1:
edgespos + 1pos = ASyx
if x > 0:
edgespos - 1pos = ASyx
if y < H - 1:
edgespos + Wpos = ASyx
if y > 0:
edgespos - Wpos = ASyx
d1 = one_to_all(W - 1, H * W, edges)
d2 = one_to_all(W * (H - 1), H * W, edges)
d3 = one_to_all(W * H - 1, H * W, edges)
INF = 9223372036854775807
ret = INF
for x in range(W):
for y in range(H):
pos = y * W + x
v = d1pos + d2pos + d3pos - 2 * ASyx
if v < ret:
ret = v
return ret
shortest path problem
Shortest path not a straight road
First Algorithm Practical Skills Test
---
This page is auto-translated from /nishio/PAST1J using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.