Flight optimization
Flight optimization
Flight optimization
import heapq
from datetime import datetime
class Flight:
def __init__(self, source, destination, departure, arrival, price):
self.source = source
self.destination = destination
self.departure = parse_time(departure)
self.arrival = parse_time(arrival)
self.price = price
def parse_time(time_str):
hour = int(time_str[:2])
minute = int(time_str[3:5])
meridian = time_str[5:].strip()
def find_cheapest_flight():
num_flights = int(input())
flights = []
flight_map = {}
for _ in range(num_flights):
source, dest, departure, arrival, price = input().split()
price = int(price)
flight = Flight(source, dest, departure, arrival, price)
flights.append(flight)
if source not in flight_map:
flight_map[source] = []
flight_map[source].append(flight)
queue = []
for flight in flight_map.get(start, []):
if flight.departure >= earliest_departure and flight.arrival <=
latest_arrival:
heapq.heappush(queue, (flight.price, flight.arrival,
flight.destination))
min_cost = {}
arrival_time = {}
while queue:
cost, current_arrival, city = heapq.heappop(queue)
if city == end:
print(cost)
return
print("Impossible")
if __name__ == "__main__":
find_cheapest_flight()