typing_singh's blog

By typing_singh, history, 4 years ago, In English

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!!****

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

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

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