Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

Блог пользователя Reference

Автор Reference, история, 8 лет назад, По-английски

in bellman ford algorithm when i relax an edge(u,v) but the distance in u and v is infinite dist[u] = dist[v] = oo and the weight of edge is big negative so what will happen is: dist[u]+w < dist[v] so dist[v] will modified i want to asked if there is necessity to write a condition before the relaxing to avoid that or not and why if not ? because i try some graphs with an arbitary order to edges it gives wrong answer without the condition. thank you all.

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
8 лет назад, # |
Rev. 2   Проголосовать: нравится +13 Проголосовать: не нравится

At least use commas...

You will need to check for negative cycles, rather than edges with negative weights.
To do that, iterate through the E edges again (After Bellman-Ford) and see if any vertex is still "relaxable". If so, there exists at least 1 negative cycle on the graph which means there are no shortest paths for certain vertices.