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 x
and y, computes the minimum cost to go from x to y.

Input

Input consists of several cases. Every case begins with the number of
vertices n and the number of arcs m. Follow m triples u, v, c,
indicating that there is an arc u → v of cost c, where u ≠ v,
−1000 ≤ c ≤ 1000 and c ≠ 0. Finally, we have x and y. Assume
1 ≤ n ≤ 10⁴, 0 ≤ m ≤ 5n, and that for every pair of vertices u and v
there is at most one arc of the kind u → v. All numbers are integers.
Vertices are numbered from 0 to n − 1. The directed graph has no
negative cycles.

Output

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

Problem information

Author: Jordi Petit

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

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