CT070

Cheapest Flight Itinerary

HardAcceptance: 0.0%

A traveler wants to fly from origin A to destination B with at most k connections. Given a list of flights (origin, destination, price), find the cheapest fare and print the itinerary. Input: first line origin and destination, second line k (max connections), third line n (number of flights), then n lines "origin destination price". Output: cheapest price and itinerary as "price: airport1 airport2 ...", or "null".

Example 1:

Input: JFK LAX 3 8 JFK ATL 150 ATL SFO 400 ORD LAX 200 LAX DFW 80 JFK HKG 800 ATL ORD 90 JFK LAX 500 SFO LAX 100
Output: 440: JFK ATL ORD LAX

Example 2:

Input: A B 0 1 A B 100
Output: 100: A B

Constraints:

1 <= k <= 5 1 <= n <= 100 1 <= price <= 10^6 Airport codes are 3-letter strings

Tags:

graph shortest-path heap bfs
Loading...
Test Cases:No test cases
No test cases available.
Coding Problem Not Found | CodeTikki