RaFiN_

1051F - The Shortest Statement

Jun 29th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. //1051F - The Shortest Statement
  2. Firstly let's find any spanning tree and root it at any vertex. For each vertex we calculate the distance to the root (let it be hv for vertex v). There are no more than 21 edges that don't belong to the tree. For each of these edges, let's run Dijkstra's algorithm from some vertex incident to this edge.
  3.  
  4. Suppose we are answering a query u v. If the shortest path between these vertices passes only along the edges of the tree, then it can be calculated by the formula hv+hu−2hlca(u,v), where lca(u,v) is the lowest common ancestor of vertices u and v. You may use any fast enough algorithm you know to calculate lca(u,v).
  5.  
  6. Otherwise there exists at least one vertex such that we ran Dijkstra's algorithm from it, and it belongs to the shortest path. Just iterate on every vertex for which we ran Dijkstra and update the answer with the value of du+dv, where dx is the shortest path to the vertex x from the fixed vertex.
Add Comment
Please, Sign In to add comment