chari407's blog

By chari407, history, 6 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

»
6 years ago, # |
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.