"Shortest" Pair of Paths
Time limit1sMemory limit128 MB
Find two vertex-disjoint (except endpoints) and edge-disjoint directed paths from node 0 to node N-1 minimizing total cost, or report impossibility.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Minimum spanning tree
- Solved
- No attempts yet
Problem
A chemical company has an unusual shortest path problem.
There are depots (vertices) where chemicals can be stored, and individual shipping methods (edges) connecting pairs of depots. Each individual shipping method has a cost. In the usual problem, the company would need only to route a single shipment from the first depot () to the last (); that is easy. The problem they have is harder: they have to ship two chemicals from the first depot () to the last (). The chemicals are dangerous and cannot safely be placed together. The regulations say the company cannot use the same shipping method for both chemicals. Further, the company cannot place the two chemicals in the same depot (for any length of time) without special storage handling, which is available only at the first and last depots.
To begin, they need to know whether it is possible to ship both chemicals under these constraints. Next, they need to find the least cost of shipping both chemicals from the first depot to the last depot. In brief, they need two completely separate paths (from the first depot to the last) where the overall cost of both is minimal.
Your program must simply determine the minimum cost or, if it is not possible, conclusively state that the shipment cannot be made.
Input
The input consists of multiple cases. The first line of each case contains and , where is the number of depots and is the number of individual shipping methods. You may assume that is less than and that is less than . The next lines each contain three values, , , and . Each line corresponds to a single, unique shipping method. The values and are the indices of two depots, and is the (non-negative integer) cost of getting from to . Note that these shipping methods are directed: if something can be shipped from to with cost , that says nothing about shipping from to . Also, there may be more than one way to ship between any pair of depots, and that may be important here. A line containing two zeroes signals the end of the data and should not be processed.
Output
For each case, print one line in the form Instance #k: C, where is the case number (starting from ) and is the minimum total cost of shipping both chemicals. If the shipment cannot be made, print Instance #k: Not possible instead. Note that there are two spaces after the colon.