MSE06I - "Shortest" pair of paths

A chemical company has an unusual shortest path problem. There are N depots (vertices) where chemicals can be stored. There are M individual shipping methods (edges) connecting pairs of depots. Each individual shipping method has a cost. In the usual problem, the company would need to find a way to route a single shipment from the first depot (0) to the last (N - 1). That's easy. The problem they have seems harder. They have to ship two chemicals from the first depot (0) to the last (N - 1). 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 same depot (for any length of time) without special storage handling --- available only at the first and last depots.

To begin, they need to know if it's possible to ship both chemicals under these constraints. Next, they need to find the least cost of shipping both chemicals from 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's not possible, conclusively state that the shipment cannot be made.

The input will consist of multiple cases. The first line of each input will contain N and M where N is the number of depots and M is the number of individual shipping methods. You may assume that N is less than 20 and that M is less than 200. The next M lines will contain three values, i, j, and v. Each line corresponds a single, unique shipping method. The values i and j are the indices of two depots, and v is the cost of getting from i to j. Note that these shipping methods are directed. If something can be shipped from i to j with cost 10, that says nothing about shipping from j to i. 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 data and should not be processed.


Input :
2 1 
0 1 20 
2 3 
0 1 20 
0 1 20 
1 0 10 
4 6 
0 1 22 
1 3 11 
0 2 14 
2 3 26 
0 3 43 
0 3 58 
0 0
Instance #1:  Not possible 
Instance #2:  40 
Instance #3:  73 

hide comments
joqsan_77: 2018-08-04 18:20:23

Maybe the tag "minimum-cost-flow" should be added.

kejriwal: 2017-01-24 18:36:15

Weak test cases... my earlier code failed on the following case:
7 8
0 1 1
0 2 1
1 3 1
2 3 1
3 4 1
3 5 1
4 6 1
5 6 1
0 0

Jacob Plachta: 2013-10-30 01:47:23

The input contains paths with i=j (a path from a node to itself).

~!(*(@*!@^&: 2009-06-26 19:15:48

Updated in statement. Original text and data set came from official website.

Anton Lunyov: 2009-06-26 19:14:13

In fact, in author tests n<=15 and m<=170 instead of n<=64 and m<=10000.
It may help. :-)

Last edit: 2009-05-06 19:31:53

Added by:~!(*(@*!@^&
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Southeastern European 2006