- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
![基于旅行计划者的问题][1]什么方法最适合解决这个问题?任何形式的帮助都将不胜感激
输入是各个城市之间的航类集合。它作为一个文件给出。文件的每一行都包含“city1 city2 departure-time arrival-time flight-no. price” 这意味着从 city1 到 city2 有一个名为“flight-no”(XY012 形式的字符串)的航类离开city1 在时间“出发时间”,在时间“到达时间”到达 city2。此外,该航类的价格是“价格”,它是一个积极的整数。所有时间均以 24 小时格式的 4 位数字字符串形式给出,例如1135、0245、2210。假设所有城市名称都是介于 1 和 N 之间的整数(其中 N 是城市的总数)。
请注意,两个城市之间可能有多个航类(在不同时间)。
您必须回答的查询是:给定两个城市“A”和“B”,时间为“t1”、“t2”,其中 t1 < t2,找到时间后离开城市“A”的最便宜的旅行” t1”,并在时间“t2”之前到达城市“B”。行程是一系列航类,在时间 t1 之后从 A 开始,在时间 t2 之前在 B 结束。此外,从任何中转(中间)城市 C 出发的时间至少在到达 C 后 30 分钟
最佳答案
你可以用图搜索算法解决这个问题,比如Dijkstra's Algorithm .
图的顶点是位置和(到达)时间的元组。边缘是停留(至少 30 分钟)和离港航类的组合。唯一的困难是标记我们已经访问过的顶点(“关闭”列表),因为在给定时间到达机场不应阻止考虑较早到达该机场的航类。我的建议是标记我们已经考虑过的出发航类,而不是标记机场。
这是 Python 中的快速实现。我假设您已经将航类数据解析为字典,该字典从出发机场名称映射到包含航类信息的 5 元组列表 ((flight_number, cost, destination_airport, departure_time, arrival_time)
):
from heapq import heappush, heappop
from datetime import timedelta
def find_cheapest_route(flight_dict, start, start_time, target, target_time):
queue = [] # a min-heap based priority queue
taken_flights = set() # flights that have already been considered
heappush(queue, (0, start, start_time - timedelta(minutes=30), [])) # start state
while queue: # search loop
cost_so_far, location, time, route = heappop(queue) # pop the cheapest route
if location == target and time <= target_time: # see if we've found a solution
return route, cost
earliest_departure = time + timedelta(minutes=30) # minimum layover
for (flight_number, flight_cost, flight_dest, # loop on outgoing flights
flight_departure_time, flight_arrival_time) in flight_dict[location]:
if (flight_departure_time >= earliest_departure and # check flight
flight_arrival_time <= target_time and
flight_number not in taken_flights):
queue.heappush(queue, (cost_so_far + flight_cost, # add it to heap
flight_dest, flight_arrival_time,
route + [flight_number]))
taken_flights.add(flight_number) # and to the set of seen flights
# if we got here, there's no timely route to the destination
return None, None # or raise an exception
关于algorithm - 基本的飞行旅行计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29831154/
我的程序有问题。 我有一个比较两个字符串的条件: (if (eq? (exp1) (exp2))) 当 exp1 给我一个字符串,exp2 给我一个字符串。可以肯定的是,当我更改“eq?”时到“=”,
我们有多种主要使用 GWT 开发的产品,目前由我们的最终客户使用。 想知道 GWT 的路线图。我得到了一些非官方的更新,谷歌正在将 GWT 中开发的产品转移到其他一些新技术。这是真的吗? GWT 的长
我希望每 15 分钟定期构建一次。我在网上看过,我正在使用这个时间表:*/15 * * * * Jenkins 告诉我使用 H/15 * * * * 来平均分配负载而不是 */15 * * * * 有
所以我正试图在 Scheme 中找出整个 call/cc 的东西。下面是我正在使用的代码: (+ 1 (call/cc (lambda (k) (if (number? k)
所以我正试图在 Scheme 中找出整个 call/cc 的东西。下面是我正在使用的代码: (+ 1 (call/cc (lambda (k) (if (number? k)
我们有一个 Azure WebJob,计划在 UTC 每天上午 8:00 运行(CRON - 0 00 08 * * *)。大多数时候它都会正确触发,但有时会触发两次(第二次运行)第一次运行后约 10
我是 Terraform 的新手。我正在尝试通过 azure 管道创建一个简单的存储帐户,但是当我运行我的管道时,我收到错误“太多命令行参数”。我很震惊,我不知道自己做错了什么。有人可以帮忙吗。 这是
我想在某些逻辑中间停止芭蕾舞 Actor 程序。如何使用代码停止 ballerina 中正在运行的程序?我正在寻找相当于 java 中的 System.exit(0) 的东西。 最佳答案 我相信您正在
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 8年前关闭。 Improve this qu
我们有一个 Azure WebJob,计划在 UTC 每天上午 8:00 运行(CRON - 0 00 08 * * *)。大多数时候它都会正确触发,但有时会触发两次(第二次运行)第一次运行后约 10
我是 Terraform 的新手。我正在尝试通过 azure 管道创建一个简单的存储帐户,但是当我运行我的管道时,我收到错误“太多命令行参数”。我很震惊,我不知道自己做错了什么。有人可以帮忙吗。 这是
我正在浏览 htdp 并在一开始的某个地方发现了这个:- Explain why the following sentences are illegal definitions: 1. (define
我正在使用 Laravel 开发成员(member)门户。 成员(member)资格有不同的类别,例如1) 单人2) 成人3) 家庭以及不同价格的所有类型。 我有一个 plans 表和 plans_s
我使用 DreamHost 作为我的网站的服务器,并且我尝试每天、每周和每月执行某个 MySQL 查询来更改我的网站的数据库。我开始在本地主机上使用事件调度程序,然后我发现我无法在 DreamHost
这周我的 crontab 作业发生了一个问题。 设置如下,每两周正常运行一次,直到现在。 10 06 * * 1 test $(($(date +\%W)\%2)) -eq 0 && echo 'te
编写了一个简单的脚本,它将在日志文件中写入日期时间戳,并且每次运行该脚本时,它都会附加到该日志文件中。 #!/bin/sh echo $(date) >> log.txt 当我尝试每 1 分钟安排一次
我对 PIPE 的了解是它用于单向通信,它有助于在两个相关进程之间进行通信。我从一本书中得到了下面的 PIPE 编程代码示例。我正在尝试使用 printf 理解代码并在代码的每一行之后打印出所有点。但
代码如下: (define make-simple-sv-num (lambda (delare) (let ((tal (random-from-to 100000 1000000)))
我目前正在使用“How To Design Programs”——使用 Scheme/Racket;我在 Scheme 的 R5RS 版本中遇到了一个非常奇特的功能。 在进行简单的减法时,尽管使用的是
我想确定时间表的详细信息。例如: 我有一个事件的时间表:event.schedule "Every 3 months on the 10th day of the month" 由哈希表示: {
我是一名优秀的程序员,十分优秀!