Can someone help me in finding why my code TLE's for some cases.

Code: https://gist.github.com/medhruv7/98e802016a9f9c34cbe3e7dc178c0dd4

# | User | Rating |
---|---|---|

1 | tourist | 3707 |

2 | Benq | 3672 |

3 | Radewoosh | 3655 |

4 | ksun48 | 3547 |

5 | jiangly | 3492 |

6 | Miracle03 | 3480 |

7 | ecnerwala | 3400 |

8 | maroonrk | 3385 |

9 | peehs_moorhsum | 3384 |

10 | sunset | 3338 |

# | User | Contrib. |
---|---|---|

1 | 1-gon | 216 |

2 | YouKn0wWho | 190 |

2 | Um_nik | 190 |

4 | sus | 183 |

5 | awoo | 182 |

6 | Errichto | 179 |

7 | tourist | 177 |

8 | -is-this-fft- | 173 |

9 | Radewoosh | 170 |

10 | maroonrk | 169 |

Can someone help me in finding why my code TLE's for some cases.

Code: https://gist.github.com/medhruv7/98e802016a9f9c34cbe3e7dc178c0dd4

↑

↓

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Sep/27/2021 13:39:18 (f1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

I would suggest dijkstra.

To take care for the coupon we need to maintain two best values per city, one without having used the coupon, one with having used it.

We would use a priority_queue else TLE since it is to much state changes. It took me some tries to find a configuration where it is AC. Not sure if this is hackable.

I assume it would work if you use a priority_queue in function dick().

CodeCan u please tell why priority queue has a better time complexity than a set? Even for question in SPOJ i saw people cursing set.

The time complexity of set and priority_queue are same, they both should work. Problems arise by using a simple queue without sorting.

In fact, if memory size matters, we can use an additional set to make sure every vertex is at most once added to the priority_queue. This limits the size of the queue to max the number of vertex.

Then we can always use a set for that matter?

priority_queue and set have the same time complexity, but still set is somewhat slower.

I am using priority_queue only in dick()

Ok, I didn't recognize that.

English description please

I've solved the same problem recently with coincidentally the same idea as your's, here's my AC submission. https://paste.ubuntu.com/p/cKqC9Gfv6S/

Yea i get the idea , but what is wrong with my implementation. Why does it TLE. It's really frustrating

You put the vertex to pair.first, and dist to pair.second, so the queue sorts the entries by vertex number instead of distance.

`pq.push({to,dist[to]});`

Oh fuck you are right. Thanks bro

Accepted , thanks a lot dude

nice code can you tell me what is the significance of prev in your code you don't seem to use it anywhere also what i guess is that you were trying to track the path using prev and making the maximum of flight cost in your path to halve right? can you tell me why it won't work?

So I was trying to make a greedy before. That take smallest cost path and then discount max element in that path. But that failed. So then I did dijkstra

prev has no use in that question, I was doing CSES problems in a sequence and one question before that wanted you to find shortest path nodes so for that I used prev. I've made all my CSES submissions public to github if you want to checkout that question refer: https://codeforces.com/blog/entry/77863