Hello everybody
I've got the problem that I always receive a SIGSEGV error when I submit my C code. On my machine the code is running fine. I tried to find out on ideone.com what could be the problem and I come up that it must be some analyse problem. However, I'm stick with the problem that I don't know where neither why the code fails... the big question is, how to get around it? Are there some tricks?
here the code that fails before compiling
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <string.h>
typedef struct {
unsigned int cost;
unsigned int index;
} vertec;
typedef struct {
char name[11];
unsigned int countVertec;
vertec* vertecs;
unsigned int cost;
} city;
#define swap_integer(a, b) a += b; \
b = a - b; \
a -= b;
/*heap[child].cost += heap[parent].cost;
heap[parent].cost = heap[child].cost - heap[parent].cost;
heap[child].cost -= heap[parent].cost;*/
vertec pop_heap(vertec* heap, unsigned int* length) {
vertec ret = heap[0];
(*length)--;
//heapify the node down
heap[0]=heap[*length];
unsigned int parent = 0, child;
while(parent < *length) {
child = parent*2+1;
//is there a left one?
if(child >= *length)
break;
//is there a right one?
if(child+1<*length) {
//which is better the right or left one?
if(heap[child+1].cost < heap[child].cost)
child = child+1;
}
//is the child smaller?
if(heap[child].cost >= heap[parent].cost)
break;
//swap them
swap_integer(heap[child].cost, heap[parent].cost)
swap_integer(heap[child].index, heap[parent].index)
parent = child;
}
return ret;
}
void push_heap(vertec* heap, unsigned int* length, unsigned int index, unsigned int cost) {
heap[*length].index = index;
heap[*length].cost = cost;
unsigned int child = *length, parent;
(*length)++;
//heapify up
while(child>0) {
parent=floor((child-1)/2);
//need to swap with parent?
if(heap[child].cost >= heap[parent].cost)
break;
swap_integer(heap[child].cost, heap[parent].cost)
swap_integer(heap[child].index, heap[parent].index)
//reloop
child=parent;
}
}
int main() {
unsigned int countTest;
unsigned int i, a;
vertec* heap = (vertec*)malloc(sizeof(vertec)*10000*10000);
unsigned int heap_len;
scanf("%u", &countTest);
while(countTest--) {
unsigned int countCity;
scanf("%u", &countCity);
city* cities = (city*)malloc(sizeof(city)*countCity);
for(i=0;i<countCity;i++) {
city* c = &(cities[i]);
scanf("%s", c->name);
scanf("%u", &(c->countVertec));
c->vertecs = (vertec*)malloc(sizeof(vertec)*c->countVertec);
for(a=0;a<c->countVertec;a++) {
scanf("%u %u", &(c->vertecs[a].index), &(c->vertecs[a].cost));
c->vertecs[a].index--;
}
}
unsigned int countPath;
scanf("%u", &countPath);
while(countPath--) {
char src[11], dst[11];
scanf("%s %s", src, dst);
unsigned int index;
//reset the stuff
for(i=0;i<countCity;i++) {
cities[i].cost=UINT_MAX;
if(strcmp(cities[i].name, src) == 0) {
index = i;
}
}
//start algorithm
heap_len=0;
push_heap(heap, &heap_len, index, 0);
while(heap_len) {
vertec vec = pop_heap(heap, &heap_len);
city* c = &(cities[vec.index]);
if(strcmp(c->name, dst)==0) {
printf("%u\n", vec.cost);
break;
}
for(a=0;a < c->countVertec;a++) {
vertec v = c->vertecs[a];
unsigned int newCost = v.cost + vec.cost;
if(cities[v.index].cost > newCost) {
cities[v.index].cost = newCost;
push_heap(heap, &heap_len, v.index, newCost);
}
}
}
//end algorithm
}
//give memory free
for(i=0;i<countCity;i++)
free(cities[i].vertecs);
free(cities);
scanf("\n");
}
free(heap);
return 0;
}
PS: Does spoj.pl really works with ideone in the background?