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 <iostream>
#include <vector>
#include <queue>
int main (int argc, char const* argv[])
{
int n, m;
std::cin >> n >> m;
std::vector<std::vector<int> >table(n,std::vector<int>(n));
while(m--){
int a ,b;
std::cin >> a >> b;
table[a-1][b-1] = 1;
table[b-1][a-1] = 1;
}
int start , end;
std::cin >> start >> end;
start--;end--;
std::vector<bool>visited(n);
std::queue<int>queue_;
visited[start] = true;
queue_.push(start);
std::vector<int>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<n;i++){
if(!visited[i] && table[s][i] == 1){
visited[i] = true;
minDist[i] = std::min(minDist[i],minDist[s]+1);
queue_.push(i);
}
}
}
std::cout <<minDist[end]<< 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?