← 返回 google 的题目列表Reachability with Flight Schedules (Time-Feasible Path)
类型:online_judge
Problem: Flight Reachability with Schedule Constraints
You are given a list of flights flights. Each flight contains:
origin: departure airport (string)
destination: arrival airport (string)
departure_time: departure time (integer or comparable timestamp)
arrival_time: arrival time (integer or comparable timestamp)
You are also given a start airport start and an end airport end.
Determine whether there exists an itinerary from start to end (using one or multiple flights) such that:
The first flight departs from start and the last flight arrives at end.
For a connection, the next flight’s departure time must be not earlier than the previous flight’s arrival time (i.e., the itinerary must be time-feasible).
Return true if such an itinerary exists; otherwise return false.
I/O Format (assumption for implementation)
Input:
Line 1: integer n (number of flights)
Next n lines: origin destination departure_time arrival_time
Last line: start end
Output: one line true or false
Constraints
1 <= n <= 2 * 10^5
Airport codes are non-empty strings
Times are non-negative integers and comparable
Examples
Input:
3
SFO LAX 1 2
LAX JFK 3 5
SFO JFK 10 12
SFO JFK
Output:
true
Input:
2
SFO LAX 5 7
LAX JFK 6 9
SFO JFK
Output:
false
Input:
4
A B 1 3
B C 3 4
C D 4 6
A D
Output:
true
Input:
1
A B 1 2
A C
Output:
false
Input:
3
A B 1 2
B A 2 3
A C 3 4
A C
Output:
true
Example
Input
3
SFO LAX 1 2
LAX JFK 3 5
SFO JFK 10 12
SFO JFK
Output
true