Can any body tell me why my code is giving wrong answer ?
Algorithm :
unsigned long max -> will contain the final max waiting time in optimal case unsigned long t -> T unsigned long m -> M unsigned long n -> N
int main(void) { unsigned long busses[m+1][4] -> Contains bus information busses[][0] -> U -> starting station busses[][1] -> V -> Ending station busses[][2] -> S -> Start time busses[][3] -> E -> End time
busses[m+1][4] -> Initialized with 0
unsigned long stations[n+1][m] -> Tells available busses at a particular station like -> busses 2 and 3 start from station 3 then stations[3][0] -> 2 stations[3][1] -> 3 stations[3][2] to stations[3][m] -> 0
step 1 -> read input values in busses matrix
step 2 -> accordingly fill matrix stations
step 3 -> timestamp -> 0 -> current time temp -> 0 -> current max waiting time
step 4 -> call function function which will give the final answer max function(1,&temp,timestamp,stations,busses)
1 -> tells the **first station** since we have to start from staion 1
step 5 -> print final value
}
void function(station , temp , timestamp, stations, busses) { step 1 -> if (station == n) { if (temp < max) { max = temp } } step -> 2 else { step -> 3 while(there is a bus from station station) { if(waiting time > temp) { *temp = waiting time; }
update timestamp -> timetamp = end time of buss
call function **function ** with ending time of bus
function(ending station of current bus , &temp , timestamp , stations , busses)
}
}
}
here is my code :
include<stdio.h>
include<stdlib.h>
include<limits.h>
include<memory.h>
unsigned long max,t,m,n;
void function(unsigned long station , unsigned long temp , unsigned long timestamp,unsigned long stations[n+1][m], unsigned long busses[m+1][4]) { if(station == n) { if(temp < max) { max = *temp; } } else { unsigned long counter = 0;
while(stations[station][counter] != 0)
{
if((busses[stations[station][counter]][2] - timestamp ) > *temp)
{
*temp = busses[stations[station][counter]][2] - timestamp;
}
timestamp = busses[stations[station][counter]][3];
function(busses[stations[station][counter]][1],temp,timestamp,stations,busses);
counter++;
}
}
}
int main(void) { max = UINT_MAX;
scanf("%lu",&n);
scanf("%lu",&t);
scanf("%lu",&m);
unsigned long busses[m+1][4];
memset(busses,0,sizeof(busses[0][0])*(m+1)*4);
unsigned long stations[n+1][m] ;
memset(stations,0,sizeof(stations[0][0])*(n+1)*m);
unsigned long i;
unsigned long counter;
for(i=1;i<=m;i++)
{
scanf("%lu",&busses[i][0]);
counter=0;
while(stations[busses[i][0]][counter] != 0)
{
counter++;
}
stations[busses[i][0]][counter] = i;
scanf("%lu",&busses[i][1]);
scanf("%lu",&busses[i][2]);
scanf("%lu",&busses[i][3]);
}
unsigned long timestamp =0;
unsigned long temp=0;
function(1,&temp,timestamp,stations,busses);
if(max != UINT_MAX)
{
printf("%lu\n",max);
}
else
{
printf("-1\n");
}
return 0;
}