Editorial for Just One More Tunnel


Call the original mm edges ordinary edges. They all have positive weight. The extra edges between pairs of special vertices are teleport edges, each with weight tt.

First, compute the shortest ordinary-only path from vertex 11 to vertex nn. This path is one possible answer, and it is also useful in the cases below.

Case 1: t≥0t \geq 0

In an optimal path, the teleport edges can be assumed to form one consecutive block.

To see why, suppose the path uses a teleport edge, then some ordinary edges, and later another teleport edge. The ordinary edges between those two teleport edges go from one special vertex to another special vertex. Since every pair of special vertices has a teleport edge, we can replace that whole middle part with one teleport edge. Because ordinary edges have positive weight and t≥0t \geq 0, this does not make the path worse.

So the path has this form:

1⇝vusing ordinary edges, then one teleport edge, thenu⇝n1 \leadsto v \quad \text{using ordinary edges, then one teleport edge, then} \quad u \leadsto n

where vv and uu are special vertices.

Run Dijkstra on the ordinary graph from vertex 11, and run it again from vertex nn. Let these distance arrays be d1d_1 and dnd_n.

The best path using a teleport has cost:

min⁡v≠u, v,u speciald1[v]+t+dn[u].\min_{v \neq u,\ v,u\text{ special}} d_1[v] + t + d_n[u].

This minimum can be found by keeping the best and second-best reachable special vertices from each side, or simply by trying all pairs under the given constraints.

The answer for this case is the minimum of:

  • the ordinary-only shortest path from 11 to nn;
  • the best path that uses one teleport edge.

If neither exists, the answer is Impossible.

Case 2: t<0t < 0

The teleport edges still form one consecutive block. Now, since every teleport edge has negative weight, if the path uses any teleport edge, it is always optimal to pass through all special vertices inside that block.

If there are kk special vertices, the teleport block then uses exactly k−1k-1 teleport edges, contributing:

(k−1)t(k-1)t

to the total cost. This value is fixed, so we only need to minimize the total weight of the ordinary parts of the path.

The path has this form:

1⇝vusing ordinary edges, then all special vertices by teleports, thenu⇝n1 \leadsto v \quad \text{using ordinary edges, then all special vertices by teleports, then} \quad u \leadsto n

where vv and uu are two special vertices. The two ordinary parts must be vertex-disjoint; otherwise their union would not form a simple path.

Because the ordinary graph is undirected, this is equivalent to finding two vertex-disjoint ordinary paths:

  • one path from vertex 11 to a special vertex;
  • one path from vertex nn to a different special vertex.

with minimum total cost.

This can be solved with min-cost flow.

Build a directed flow network from the ordinary graph:

  1. Split every vertex xx into xinx_{in} and xoutx_{out}, and add an edge xin→xoutx_{in} \to x_{out} with capacity 11 and cost 00. This enforces vertex-disjointness.
  2. For every ordinary undirected edge (u,v)(u,v) of weight ww, add the directed edge uout→vinu_{out} \to v_{in} if uu is not special, and add the directed edge vout→uinv_{out} \to u_{in} if vv is not special. Each added edge has capacity 11 and cost ww. This lets a path enter a special vertex, but once it reaches one, it stops there.
  3. Add a source ss with edges to 1in1_{in} and ninn_{in}, each with capacity 11 and cost 00.
  4. Add an edge from every special vertex xoutx_{out} to the sink qq, with capacity 11 and cost 00.

Now find the minimum-cost flow of value 22. If such a flow exists, its cost is exactly the minimum total ordinary-edge cost of the two required vertex-disjoint paths. The corresponding full path has cost:

flow cost+(k−1)t.\text{flow cost} + (k-1)t.

Again, compare this value with the ordinary-only shortest path.

Complexity

For t≥0t \geq 0, two Dijkstra runs are enough:

O((n+m)log⁡n).O((n+m)\log n).

For t<0t < 0, the min-cost flow sends only 22 units of flow. Using shortest augmenting paths with Bellman-Ford or SPFA on the residual graph gives:

O(nm).O(nm).

Finally, if no candidate path exists, print Impossible; otherwise print the minimum candidate value.

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.