Calculating shortest possible route between two given nodes I recently came across this problem:


  Heroes in Indian movies are capable of superhuman feats. For example,
  they can jump between buildings, jump onto and from running trains,
  catch bullets with their hands and teeth and so on. A perceptive
  follower of such movies would have noticed that there are limits to
  what even the superheroes can do. For example, if the hero could
  directly jump to his ultimate destination, that would reduce the
  action sequence to nothing and thus make the movie quite boring. So he
  typically labours through a series of superhuman steps to reach his
  ultimate destination.
  
  In this problem, our hero has to save his wife/mother/child/dog/...
  held captive by the nasty villain on the top floor of a tall building
  in the centre of Bombay/Bangkok/Kuala Lumpur/.... Our hero is on top
  of a (different) building. In order to make the action "interesting"
  the director has decided that the hero can only jump between buildings
  that are "close" to each other. The director decides which pairs of
  buildings are close enough and which are not.
  
  Given the list of buildings, the identity of the building where the
  hero begins his search, the identity of the building where the captive
  (wife/mother/child/dog...) is held, and the set of pairs of buildings
  that the hero can jump across, your aim is determine whether it is
  possible for the hero to reach the captive. And, if he can reach the
  captive he would like to do so with minimum number of jumps.
  
  Here is an example. There are 5 buildings, numbered 1,2,...,5, the
  hero stands on building 1 and the captive is on building 4. The
  director has decided that buildings 1 and 3, 2 and 3, 1 and 2, 3 and 5
  and 4 and 5 are close enough for the hero to jump across. The hero can
  save the captive by jumping from 1 to 3 and then from 3 to 5 and
  finally from 5 to 4. (Note that if i and j are close then the hero can
  jump from i to j as well as from j to i.). In this example, the hero
  could have also reached 4 by jumping from 1 to 2, 2 to 3, 3 to 5 and
  finally from 5 to 4. The first route uses 3 jumps while the second one
  uses 4 jumps. You can verify that 3 jumps is the best possible.
  
  If the director decides that the only pairs of buildings that are
  close enough are 1 and 3, 1 and 2 and 4 and 5, then the hero would not
  be able to reach building 4 to save the captive.


That was breadth first search problem, which is easy to figure out.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;queue&gt;

int main (int argc, char const* argv[])
{
    int n, m;
    std::cin &gt;&gt; n &gt;&gt; m;
    std::vector&lt;std::vector&lt;int&gt; &gt;table(n,std::vector&lt;int&gt;(n));
    while(m--){
        int a ,b;
        std::cin &gt;&gt; a &gt;&gt; b;
        table[a-1][b-1] = 1;
        table[b-1][a-1] = 1;
    }
    int start , end;
    std::cin &gt;&gt; start &gt;&gt; end;
    start--;end--;
    std::vector&lt;bool&gt;visited(n);
    std::queue&lt;int&gt;queue_;
    visited[start] = true;
    queue_.push(start);
    std::vector&lt;int&gt;minDist(n);
    std::fill(minDist.begin(),minDist.end(),31000);
    minDist[start] = 0;
    while(!queue_.empty()){
        int s = queue_.front();
        queue_.pop();
        for(int i=0;i&lt;n;i++){
            if(!visited[i] &amp;&amp; table[s][i] == 1){
                visited[i] = true;
                minDist[i] = std::min(minDist[i],minDist[s]+1);
                queue_.push(i);
            }
        }
    }
    std::cout &lt;&lt;minDist[end]&lt;&lt; std::endl;
    return 0;
}


The code passed 9 test cases out of the 10 and got stuck at the 10th test case, which was really big. It didn't even run on my own machine.

Here is the test case of whooping 5 MBs: 10th test case in the archive

Can someone lend me some tips for optimizing the code for such a big input?
I think you choice of a data table is really bad. You should consider creating nodes similar to an trie.
struct node {
    unsigned houseID;
    unsigned firstVisited = 0;
    std::vector&lt;node*&gt; houseLinks;
    explicit node(unsigned id) : houseID(id) {}
};

Then your initial code would look like this:
unsigned numHouses, numLinks;
std::cin &gt;&gt; numHouses &gt;&gt; numLinks;

std::vector&lt;std::unique_ptr&lt;node&gt;&gt; houses;
for (unsigned house = 0; house &lt; numHouses; ++house) {
    houses.push_back(std::make_unique&lt;node&gt;(house));
}

I have to say, that my compiler hat home complains about make_unique. In any case this should work too houses.push_back(std::unique_ptr&lt;node&gt;(new node (house)));

Now you have to add the links between the houses
for (unsigned link = 0; link &lt; numLinks; ++link) {
    unsigned house1, house2;
    std::cin &gt;&gt; house1 &gt;&gt; house2;            
    houses[house1-1]-&gt;houseLinks.push_back(houses[house2-1].get());
    houses[house2-1]-&gt;houseLinks.push_back(houses[house1-1].get());
}


Your checking part seems right, although i would suggest to group everything as it belongs:
unsigned start, end;
std::cin &gt;&gt; start &gt;&gt; end;
start--;end--;

std::vector&lt;bool&gt; visited(numHouses, false);
visited[start] = true;


EDIT: this is no longer relevant but i dont know how to cross it out. However, i would suggest, that you keep track of your jump length via the queue. So rather than haveing a queue&lt;node*&gt; have a queue&lt;std::pair&lt;node*, size_t&gt;&gt;
New: I would now suggest to store the first visited field in the struct itself, which simplifies the code a lot
std::queue&lt;node*&gt; jumpQueue;
jumpQueue.push(houses[start].get());

Now while you are adding something to the queue you just increment the jump index.

Now to your breadth first search. Obviously you can ignore any occurrence of a house that was reached before, as that would lead to an at least equally large jumping series.
size_t result = 0;
while (!jumpQueue.empty()) {
    auto oldHouse = jumpQueue.front();
    jumpQueue.pop();
    for (auto newHouse : oldHouse-&gt;houseLinks) {
        if (!visited[newHouse-&gt;houseID]) {
            visited[newHouse-&gt;houseID] = true;
            newHouse-&gt;firstVisited = oldHouse-&gt;firstVisited+1;
            jumpQueue.push(newHouse);
        }
        if (newHouse-&gt;houseID == end) {
            result = oldHouse-&gt;firstVisited+1;
            std::queue&lt;node*&gt; swapQueue;
            std::swap(swapQueue, jumpQueue);
        }
    }
}

Note, that one can directly terminate the traversal of the trie by swapping the current queue with an empty one.

Finally now that you have the result, or 0 if you never found the villan then print result. All together you now have:


Solution
#include &lt;iostream&gt;
#include &lt;memory&gt;
#include &lt;queue&gt;
#include &lt;vector&gt;

struct node {
    unsigned houseID;
    unsigned firstVisited = 0;
    std::vector&lt;node*&gt; houseLinks;
    explicit node(unsigned id) : houseID(id) {}
};

int main()
{
    unsigned numHouses, numLinks;
    std::cin &gt;&gt; numHouses &gt;&gt; numLinks;

    std::vector&lt;std::unique_ptr&lt;node&gt;&gt; houses;
    for (unsigned house = 0; house &lt; numHouses; ++house) {
        houses.push_back(std::unique_ptr&lt;node&gt;(new node (house)));
    }

    for (unsigned link = 0; link &lt; numLinks; ++link) {
        unsigned house1, house2;
        std::cin &gt;&gt; house1 &gt;&gt; house2;
        houses[house1-1]-&gt;houseLinks.push_back(houses[house2-1].get());
        houses[house2-1]-&gt;houseLinks.push_back(houses[house1-1].get());
    }

    unsigned start, end;
    std::cin &gt;&gt; start &gt;&gt; end;
    start--;end--;

    std::vector&lt;bool&gt; visited(numHouses, false);
    visited[start] = true;

    std::queue&lt;node*&gt; jumpQueue;
    jumpQueue.push(houses[start].get());

    size_t result = 0;
    while (!jumpQueue.empty()) {
        auto oldHouse = jumpQueue.front();
        jumpQueue.pop();
        for (auto newHouse : oldHouse-&gt;houseLinks) {
            if (!visited[newHouse-&gt;houseID]) {
                visited[newHouse-&gt;houseID] = true;
                newHouse-&gt;firstVisited = oldHouse-&gt;firstVisited+1; 
                jumpQueue.push(newHouse);
            }
            if (newHouse-&gt;houseID == end) {
                result = oldHouse-&gt;firstVisited+1;
                std::queue&lt;node*&gt; swapQueue;
                std::swap(swapQueue, jumpQueue);
            }
        }
    }

    std::cout &lt;&lt; result &lt;&lt; &quot;\n&quot;;
}