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

Автор mkmeden, история, 21 месяц назад, По-английски

Can some one help me undertand why this solution is giving a TLE for a particular test case.

Task :https://cses.fi/problemset/task/1193/

Solution :

Code

TLE test case : https://cses.fi/file/3d5f25fa10486d5059bfe87f28554e8fa18d9e0f1f1d6f27f3d3f3d324ab45f0/1/1/

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

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

If i understand your code correctly, you go with bfs and for each cell in the labyrinth you store the path from starting cell to this cell, and in the end you just print path of finishing cell. It will probably use too much memory/time. Instead of it, try to store it's "parent", i.e. the cell, from wich you come to your current cell. This way, you will be able to easily construct your path from finish cell to start, and then just reverse.

  • »
    »
    21 месяц назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Thank you for your response .But i cant understand why my approach would take more time. It does take a lot of memory storing the strings but why time actually. And how would your approach save this time and memory as we are storing the parents which would take memory.

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

      Firstly, you could have time and memory limit at the same time, but checker shows only TL because it happens first. Your time complexity in the worst case is O(n^2*log(n)*c) where c is a constant of all your computations, including map <pair <int, int>, char> finding time, string concatenation(i'm not sure about this, maybe it's even O(length)). O(10^8) ~ 1 second, and your time complexity is O(10^7*c), which is luckily too much

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

        I understood the correct method, but still confused why my initial one didnt work.

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

          Oh, i thought you were using priority queue instead of queue, but nevermind, it doesn't change anything else.

          At least, there is ML error.

          Also, I checked your code and it runs in 57 seconds.

          I don't know exactly how it works, probably huge amount of memory needs a lot of time to run.

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

            daymn...guess i ll have to avoid using this method anyway.. Thanks

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

              Just stick with the standard BFS always. Store parents no paths