Getting SIGABRT ???

Jun 11, 2012 at 4:30pm
Here's the question :-
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.
Input format
The first line of the input contains two integers N and M. N is the number of buildings: we assume that our buildings are numbered 1,2,...,N. M is the number of pairs of buildings that the director lists as being close enough to jump from one to the other. Each of the next M lines, lines 2,...,M+1, contains a pair of integers representing a pair of buildings that are close. Line i+1 contains integers Ai and Bi, 1 ≤ Ai ≤ N and 1 ≤ Bi ≤ N, indicating that buildings Ai and Bi are close enough. The last line, line M+2 contains a pair of integers S and T, where S is the building from which the Hero starts his search and T is the building where the captive is held.
Output format
If the hero cannot reach the captive print 0. If the hero can reach the captive print out a single integer indicating the number of jumps in the shortest route (in terms of the number of jumps) to reach the captive.
Test Data:
You may assume that 1 ≤ N ≤ 3500 and 1 ≤ M ≤ 1000000. Further, in at least 50% of the inputs 1 ≤ N ≤ 1000 and 1 ≤ M ≤ 200000.
Example:
Here are the inputs and outputs corresponding to the two examples discussed above.
Sample Input 1:
5 5
1 3
2 3
1 2
3 5
4 5
1 4
Sample Output 1:
3
Sample Input 2:
5 3
1 3
1 2
4 5
1 4
Sample Output 2:
0


Here's my solution :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<cstdio>
#include <queue>
#include <vector>
using namespace std;
int main() {
    //variable declarations
    int i,j,steps=1,n,m,s,t,tmp,tmp1,visited[1000001];
vector<vector<int> > buildings ( 3501, vector<int> ( 3501 ) );
    queue<int> tovisit;
    //take inputs
    scanf("%d %d",&n,&m);
    for (i=1;i<=m;i++) {
        scanf("%d %d",&tmp,&tmp1);
        buildings[tmp][buildings[tmp][0]+1]=tmp1;
        buildings[tmp][0]++;
        buildings[tmp1][buildings[tmp1][0]+1]=tmp;
        buildings[tmp1][0]++;
    }
    scanf("%d %d",&s,&t);
    if (s==t) {printf("0");goto gym;}
    //intialize queue tovisit
    visited[s]=1;
    for (i=1;i<=buildings[s][0];i++) {tovisit.push(buildings[s][i]);visited[buildings[s][i]]=1;}
    tovisit.push(-1);
    while (!tovisit.empty()) {
          if (tovisit.front()==t) {printf("%d",steps);break;}
          if (tovisit.front()==-1) {steps++;tovisit.pop();if (tovisit.empty()) {printf("0");break;}tovisit.push(-1);} else {
             for (i=1;i<=buildings[tovisit.front()][0];i++) {
if (visited[buildings[tovisit.front()][i]]!=1) {
for (j=1;j<=buildings[tovisit.front()][0];j++) if (visited[buildings[tovisit.front()][i]]!=1) tovisit.push(buildings[tovisit.front()][j]);
}
}
             visited[tovisit.front()]=1;
             tovisit.pop();
          }
    }
    gym:;
}


I am getting a SIGABRT error , what is it , why and where it is occuring here and how to correct it ???
Last edited on Jun 11, 2012 at 4:31pm
Jun 11, 2012 at 5:28pm
There are a number of things to point out here, such as finding a way to write your code without the use of goto, but most immediate problem that catches my eye is the array allocation of 1000001 on line 7.
Last edited on Jun 11, 2012 at 5:33pm
Jun 11, 2012 at 5:29pm
SIGABRT is a signal sent by abort() when there is a pretty bad internal error in one of the library functions.

I'm not sure exactly what is triggering it here but you shouldn't use 'goto'. Use 'break' or 'return' (since it looks like u want the program to terminate). Speaking of which, int main() is supposed to return something...
Jun 11, 2012 at 5:49pm
From what I've been able to tell, the stack doesn't seem to like allocating a block of data that large, I couldn't exactly say why though and I'm struggling to find any articles about this currently. Allocating that array onto the heap should be fine, however.

Edit: Alright, I found an explanation from Helios on these forums: http://www.cplusplus.com/forum/general/12086/#msg57628 it seems to go along with what I was suspecting but wasn't sure of. The stack just shouldn't be taking an allocation that large, the heap is definitely the way to go.
Last edited on Jun 11, 2012 at 5:58pm
Topic archived. No new replies allowed.