Coding problem

Develop a search algorithm that lists available trains from city a to city b.
sort in ascending order drom cheapest to costliest.
if multiple trains with same cost then print train with lesser connection first and even if the number of connections are same then print alphabetical order.
input
Bangalore Hyderabad
Bangalore Hyderabad 10000
Bangalore Chennai 4000
Chennai Hyderabad 4000

output
Bangalore Chennai Hyderabad 8000
Bangalore Hyderabad 10000