@Andrea: What I do is start by sorting the train departures. Then I go one after one. I keep an array with the available trains at each station and the hour of availability (counting the turnaround time). I start with two empty array. So, with every departure, I see if there is a train available, if there is one, i remove it from the availables at that station and put it in the destination station with the new hour of availability. Else, if there is no train, then I increase the need of trains for that station and put it in the array of available trains at the destination station. Not so difficult.