1

I'm a newbie at coding and i was unsure why this code is getting this error: runtime error(SIGKILL). Thanks. The code is the naive Dijkstra's algorithm. this code is run on batches of test cases. hence the most outer loop. Sample Input: 3 3 2 1 2 5 2 3 7 1 3 3 3 1 2 4 1 3 7 2 3 1 1 3 3 1 1 2 4 1 3

Output- 12 5 NO

    #include <cstdio>
int weight [10003][10003];
bool seen [10003];
int  dist[10003];
#define oo 100000000
int main(){
//    freopen("dij.out","w",stdout);
   int rounds;
  scanf("%d",&rounds);
    for(int i=0;i<rounds;i++){
        for(int me=0;me<10003;me++){ //initialising for next batch
            seen[me]=false;
        }
        for(int you=0;you<10003;you++){ // initialising for next batch
            for(int u=0;u<10003;u++){
                weight[you][u]=0;
            }
        }
        int v,k;
        scanf("%d %d",&v,&k);
        for(int j=0;j<k;j++){
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            weight[a][b]=c;
            weight[b][a]=c;
        }
        for(int z=0;z<=v;z++){
            dist[z]=oo;
        }
        int start,end;
        scanf("%d %d",&start,&end);
        dist[start]=0;
        while(true){ //dijkstra's
            int closest=-1;
            for(int bla=1;bla<=v;bla++){
                if(!seen[bla]&&(closest==-1||dist[bla]<dist[closest])){
                    closest=bla;
                }
            }
            if(closest==-1){
                break;
            }
            seen[closest]=true;
            for(int adj=1;adj<=v;adj++){
                if(weight[closest][adj]>0){
                    if(dist[closest]+weight[closest][adj]<dist[adj]){
                        dist[adj]=dist[closest]+weight[closest][adj];
                    }
                }
            }
        }
        if(dist[end]==oo){
            printf("NO\n");
        }else{
            printf("%d\n",dist[end]);
        }
    }
}
Sanchit
  • 21
  • 1
  • 4
  • Compile it with debug symbols (`-g`), then run in gdb and give us the detailed error message. I tried running your code to help you, but it requires manual entry of your own format of an edge list. You can't really expect people to (1) read your code and understand your input conventions and then (2) type in a coherent edge list. Alternatively, provide some sample input that we can read into your program via `stdin`. – us2012 Jan 12 '13 at 04:07
  • Okay, same output for me, but no sigkill. Where do you get that sigkill with your input? – us2012 Jan 12 '13 at 04:12
  • It was a contest and I was getting errors when it was being judged, asked the holder and he said some ridiculously large test case was the cause memory limit. Thanks anyway – Sanchit Jan 12 '13 at 04:19
  • In that case, your algorithm took too long for the large test case. In contests, the evaulation systems terminate a contestant's solution if they take too long, they use a `SIGKILL` for that. Your implementation would probably benefit from using adjacency lists instead of a matrix. – us2012 Jan 12 '13 at 04:32
  • Ok Thank you very much I'll try doing it again using an adjacency list – Sanchit Jan 12 '13 at 04:33

2 Answers2

0

It may happen that you didn't give "custom input" while executing your code. That is what happened to me.

Amit Goel
  • 43
  • 6
0

My code ran an infinite loop where it didn't have an output to provide, that is why it threw SIGKILL error. I put a print statement with a random string in the loop and it output that string infinitely when I ran the code again. Fixed it then.

whatever
  • 11
  • 2