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

Автор typing_singh, история, 4 года назад, По-английски

https://cses.fi/problemset/task/1671/

I m getting tle in only 2 test cases of this problem. here's my code link--

https://ideone.com/SEcRXD

need help!!****

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

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Your use of vis[] is not correct. if (!vis[v]&&dist[v] > dist[u] + weight)

Just remove it. It is enough to check if dist[v] can be relaxed. This is the case if a longer path is cheaper because sum of weights are less than a earlier, shorter path.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    okay..but still giving tle. thanks for replying spookywooky

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Sorry, I was wrong ;)

      You need a vis[] array here. But not inside the loop over the children. You should check vis[] array just after pop() from the queue.

      Since it is a priority_queue, the first time a node is popped from the queue the path is shortest, so all pops after the first one can be ignored.

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

Just write this line "if(dist[u]<w)continue;" before the for loop in shortestpath function, here "w=pq.top().first".