Editorial for Just One More Tunnel
Submitting an official solution before solving the problem yourself is a bannable offence.
Call the original edges ordinary edges. They all have positive weight. The extra edges between pairs of special vertices are teleport edges, each with weight
.
First, compute the shortest ordinary-only path from vertex to vertex
. This path is one possible answer, and it is also useful in the cases below.
Case 1: 
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 , this does not make the path worse.
So the path has this form:
\[ 1 \leadsto v \quad \text{using ordinary edges, then one teleport edge, then} \quad u \leadsto n \]
where and
are special vertices.
Run Dijkstra on the ordinary graph from vertex , and run it again from vertex
. Let these distance arrays be
and
.
The best path using a teleport has cost:
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
to
;
- the best path that uses one teleport edge.
If neither exists, the answer is Impossible.
Case 2: 
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 special vertices, the teleport block then uses exactly
teleport edges, contributing:
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 \leadsto v \quad \text{using ordinary edges, then all special vertices by teleports, then} \quad u \leadsto n \]
where and
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
to a special vertex;
- one path from vertex
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:
- Split every vertex
into
and
, and add an edge
with capacity
and cost
. This enforces vertex-disjointness.
- For every ordinary undirected edge
of weight
, add the directed edge
if
is not special, and add the directed edge
if
is not special. Each added edge has capacity
and cost
. This lets a path enter a special vertex, but once it reaches one, it stops there.
- Add a source
with edges to
and
, each with capacity
and cost
.
- Add an edge from every special vertex
to the sink
, with capacity
and cost
.
Now find the minimum-cost flow of value . 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:
Again, compare this value with the ordinary-only shortest path.
Complexity
For , two Dijkstra runs are enough:
For , the min-cost flow sends only
units of flow. Using shortest augmenting paths with Bellman-Ford or SPFA on the residual graph gives:
Finally, if no candidate path exists, print Impossible; otherwise print the minimum candidate value.
Comments