The code works for some but not all inputs

The code works for some but not all input, kindly look into it.

for example, it doesn’t work for this-
3 3
0 1 100
1 2 50
2 0 40