 # Why my code is giving wrong answer for Traveling Plan Problem

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] -> Contains bus information
busses[] -> U -> starting station
busses[] -> V -> Ending station
busses[] -> S -> Start time
busses[] -> E -> End time

busses[m+1] -> 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 -> 2
stations -> 3
stations to stations[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])
{
if(station == n)
{
if(*temp < max)
{
max = *temp;
}
}
else
{
unsigned long counter = 0;

``````	while(stations[station][counter] != 0)
{
if((busses[stations[station][counter]] - timestamp ) > *temp)
{
*temp = busses[stations[station][counter]] - timestamp;
}

timestamp = busses[stations[station][counter]];
function(busses[stations[station][counter]],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];

memset(busses,0,sizeof(busses)*(m+1)*4);

unsigned long  stations[n+1][m] ;

memset(stations,0,sizeof(stations)*(n+1)*m);

unsigned long i;
unsigned long  counter;

for(i=1;i<=m;i++)
{
scanf("%lu",&busses[i]);

counter=0;

while(stations[busses[i]][counter] != 0)
{
counter++;
}

stations[busses[i]][counter] = i;

scanf("%lu",&busses[i]);

scanf("%lu",&busses[i]);

scanf("%lu",&busses[i]);
}

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;
``````

}

//