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

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

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

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

Problem: https://cses.fi/problemset/task/1195/

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

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

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().

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

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

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

      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.

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

    I am using priority_queue only in dick()

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

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

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/

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

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

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

      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]});

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

    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?

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

      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.net/blog/entry/77863