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

Автор vineetjai, 4 года назад, По-английски

I tried to solve the Hypertubes problem on Spoj but I am getting TLE.

I tried to store the graph in the adjacency list(using set) and then iterated over 0 to n and for every entry in its adjacency element, updated its distance. It gives WA on the 25th test case which I understand why it is giving WA. Also, I think its complexity is m*k*k*log(min(n,k*k)). log factor is coming from insertion in the set.

NOTE: Graph storing part is not giving TLE.

For reference here is my WA code

I had to store the graph as same as above(it will not give me TLE). I tried to use Disjoint set union for answering -1 if (n-1) index is not reachable from 0. If it is reachable then I tried to use priority_queue(Dijkstra) for finding the minimum distance from 0 to n-1 but it is giving me TLE on the 25th test case. I think that this approach has to be worked fine but it is throwing TLE. I also tried only Dijkstra without Disjoint set union but it still gives me TLE.

Here is my code which is getting TLE

TLE Code

My Questions are:

  1. Can I get rid of this TLE?

  2. Also, can someone mention what is expected complexity of this problem?

  3. Is there any other methods to solve this question?

Edit 1: Here is proof that WA coming on 25th.

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

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

I think you are getting TLE bcz you are using O(M.K^2.log(something)) for the storing the graph part as if M is 1000 and K is 1000 then definitely it is TLE. And for your WA code it is getting WA before the Test Case 25 so it is not running for further cases and giving the verdict WA not the TLE

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

    It is giving WA after saying running 25th. So I think it means WA on 25th.

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

      I have also submitted your code and it is showing running on 24th and then giving WA :(

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

        you have to keep refreshing the page frequently only then you can see WA on 25th because 24 and 25 are very close.

        Edit: Here is proof: URL

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

          Ohh.. Yes it is giving WA on 25th. But why using priority_queue or even simple queue it is giving TLE?, the question remains the same <..>

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

COCI 2012/2013, task HIPERCIJEVI, the excerpt from "Round 5 solutions".

The graph described in the problem statement is a hypergraph. When solving the shortest path problem on a hypergraph, we can convert it to a “normal” undirected graph by simply connecting all pairs of nodes (stations) on the same hyperedge (hypertube) with undirected edges. This results in a graph with a total of $$$N$$$ nodes and $$$MK(K-1)/2$$$ edges. A simple breadthfirst search applied to this graph is then a solution, ignoring time and memory constraints. However, the constraints prevent such a solution from obtaining all points.

The most elegant way to speed up the solution is adding a new node for each hypertube, connecting it with undirected edges to all stations connected to the corresponding hypertube. Such a graph has $$$N+M$$$ nodes and $$$MK$$$ edges. A breadth-first search applied to this graph yields the solution $$$2X-1$$$, where $$$X$$$ is the number of stations on the shortest path from station $$$1$$$ to station $$$N$$$.

Necessary skills: breadth-first search