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

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

Link to the problem I use bottom up DP, where $$$dp[\text{mask of visited vertices}][\text{ending vertex}]$$$. I'm pretty sure my time complexity is $$$O(N \cdot 2^N + 2^N \cdot N^2)$$$ however it gives TLE. Are there any way to optimize my code, and what's the reason for TLE (probably big constant factor?)

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

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

Your dp idea could work, if you eliminate the N^2 factor. Try using a top-bottom approach, so start with the full mask and ending, and work to the bottom (recursion should be an easy implementation). don't forget to use memoization in that case, since you don't want to compute the same value more than once.

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

    Is it because bottom-up approach checked to many unimportant states?

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

      Scratch that, looking at my submission I think I'm using N^2*2^N as well, the problem is probably you sorting the masks.

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

        Also quick tip for speeding up your code by about x5 is only doing dp on the middle nodes (2,3,...,n-1) because you know for sure when you'll reach 1 and n.

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

        Sorting is only $$$O(N \cdot 2^N)$$$ which is way smaller than $$$O(2^N \cdot N^2)$$$. Also, only do dp on the middle nodes like this $$$dp[\text{first_node}][\text{visited_nodes}][\text{ending_node}]$$$ ? However it requires $$$O(2^N \cdot N^2)$$$ memory which is too much.

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

          no no, do the same dp, but only with middle nodes, don't do it for 1 and n

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

            Not sure how. If we only do the middle ones, what will be the final answer? Some of the $$$\text{full_mask}$$$ could be starting from some nodes that isn't directly connected to node $$$1$$$.

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

              the only difference is the starting values, which will be 1 for each direct neighbor of node 1 (for the 1 bit mask).

              The calculation at the end will be the sum of full masks over all nodes that connect to n