Weighted shortest path (1) P43859


Statement
 

pdf   zip

Write a program that, given a directed graph with positive costs at the arcs, and two vertices xx and yy, computes the minimum cost to go from xx to yy.

Input

Input consists of several cases. Every case begins with the number of vertices nn and the number of arcs mm. Follow mm triples u,v,cu, v, c, indicating that there is an arc uvu \to v of cost cc, where uvu \ne v and 1c1041 \le c \le 10^4. Finally, we have xx and yy. Assume 1n1041 \le n \le 10^4, 0m5n0 \le m \le 5n, and that for every pair of vertices uu and vv there is at most one arc of the kind uvu \to v. All numbers are integers. Vertices are numbered from 0 to n1n-1.

The condition for cc was previously c1000c \le 1000. It was updated to create new test cases.

Output

For every case, print the minimum cost to go from xx to yy, if this is possible. If there is no path from xx to yy, state so.

Public test cases
  • Input

    6 10
    1 0 6
    1 5 15
    3 4 3
    3 1 8
    4 0 20
    0 5 5
    0 2 1
    5 1 10
    4 1 2
    2 3 4
    3 5
    
    2 1
    0 1 1000
    1 0
    
    3 3
    0 2 100
    0 1 40
    1 2 60
    0 2
    

    Output

    16
    no path from 1 to 0
    100
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++ Python
    User solutions
    C++ Python