Systems of difference constraints

A system of difference constraints is a set of inequations of the kind
x − y ≤ k, where x and y are integer variables, and k is an integer
constant. Given a system of difference constraints, a solution is an
assignment of values to variables in such a way that all inequations
hold.

For instance, the system of difference constraints
{x₁ − x₂ ≤ 4, x₂ − x₃ ≤ −1, x₃ − x₁ ≤ −2} has, among other solutions,
x₁ = 4, x₂ = 0 and x₃ = 2.

Write a program that, given a system of difference constraints with n
variables x₁, …, x_(n) and m inequations among them, tells if there is
some solution or not.

Input

Input consists of several cases. Every case begins with n and m,
followed m triplets i, j, k, with i ≠ j, for the inequation
x_(i) − x_(j) ≤ k. Assume 1 ≤ n ≤ 10³, 0 ≤ m ≤ 5n, −10⁵ ≤ k ≤ 10⁵, and
that every pair of i and j appears at most once. All given numbers are
integers.

Output

For every case, print “yes” if the system has some solution, and print
“no” otherwise.

Problem information

Author: Enric Rodríguez

Generation: 2026-01-25T10:17:26.254Z

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