Systems of difference constraints P23267


Statement
 

pdf   zip

A system of difference constraints is a set of inequations of the kind xykx - y \le k, where xx and yy are integer variables, and kk 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 {x1x24,x2x31,x3x12}\{ x_1 - x_2 \le 4, x_2 - x_3 \le -1, x_3 - x_1 \le -2 \} has, among other solutions, x1=4x_1 = 4, x2=0x_2 = 0 and x3=2x_3 = 2.

Write a program that, given a system of difference constraints with nn variables x1,,xnx_1, \dots, x_n and mm inequations among them, tells if there is some solution or not.

Input

Input consists of several cases. Every case begins with nn and mm, followed mm triplets ii, jj, kk, with iji \ne j, for the inequation xixjkx_i - x_j \le k. Assume 1n1031 \le n \le 10^3, 0m5n0 \le m \le 5n, 105k105-10^5 \le k \le 10^5, and that every pair of ii and jj 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.

Public test cases
  • Input

    3 3
    1 2 4
    2 3 -1
    3 1 -2
    
    3 3
    1 2 3
    2 3 -2
    3 1 -2
    
    4 6
    2 4 -2
    4 2  2
    1 2  1
    1 4  3
    4 3  2
    3 1 -1
    

    Output

    yes
    no
    yes
    
  • Information
    Author
    Enric Rodríguez
    Language
    English
    Official solutions
    C++
    User solutions
    C++