Codeforces Round 1133 (Div. 1, Div. 2) is coming soon! Register here. ×

chari407's blog

By chari407, history, 9 years ago, In English

Can some one help me with this code? I do not know why it is giving me weird values. Code

  • Vote: I like it
  • -1
  • Vote: I do not like it

»
9 years ago, hide # |
← Rev. 3  
Vote: I like it 0 Vote: I do not like it

INT_MAX + 1 = INT_MIN, you can imagine it as a cycle. So when your program check for the 5th edge, i.e "5 3 20", both dist[5] and dist[3] are equal to INT_MAX, so INT_MAX + edge.weight will return to negative value.

You can fix it easily. Instead of assigning every element equal to INT_MAX, you should assign them equal to a big enough number, for example 10^9 + 7 is good.