Weighted shortest path (5)

Write a program that, given a directed graph with postive and/or negative costs at the arcs (but no negative cycles), 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, 1000c1000-1000 \le c \le 1000 and c0c\neq 0. 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 directed graph has no negative cycles.

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.

Problem information

Author: Jordi Petit

Generation: 2026-01-25T11:37:36.868Z

© Jutge.org, 2006–2026.
https://jutge.org