问题:
根据所有航班的时间表,找出飞机运营商所需的最小飞机数量。
因此,考虑到所有航班的时间表(来源,目的地,出发时间,旅程的持续时间),我们需要找到运营商所需的最少数量的飞机。
飞机完成旅行时。开始另一次旅行需要至少50分钟。
编辑:我无法提出解决方案..
我尝试将每次旅行的图形作为节点。如果第一个节点的目的地与第二个节点的源相同,并且第二个节点的开始时间为50个,则第一个节点与第二个节点之间存在有向边缘。完成第一个节点的旅程。
任何帮助将不胜感激如何继续..
注意:我在微软的一次采访中被问到这个问题。
答案 0 :(得分:1)
如果我对你不好,那就是开始城市,完成城市,我们需要找到从最初的城市到达目的地城市的最小航班的方式。那可以吗?
我如何通过动态编程看到解决方案,dp[i][j]
允许我们在i
仅使用j
个广告来到达城市dp
的最佳时间。
开始时,infinity
的所有元素都设置为 dp[0][0] = 0;
priority_queue< pair<int,int> > q;
q.Add( make_pair(0,0) );
/*in q we store pair, where first is time of arrival in city,
and the second is THAT city.*/
while there are element is queue {
x = the first one element ( where time is the smallest )
remove first element from queue
if x.city == destination city
break cycle;
then
for all j
if dp[x.city][j] < x.time + 50
for all flights(from, to) where from==x.city we try to update
if( dp[to][j+1] < dp[x.city][j] + 50 + jurney_duration ) {
dp[to][j+1] = dp[x.city][j] + 50 + jurney_duration ;
q.add( make_pair(dp[x.city][j] + 50 + jurney_duration, to) );
}
}
。我们将尝试在每一步更新它。
因此,算法将如下所示:
x
所以,为了找到答案,我们只需找到dp[final_dest][x] != infinity
所在的最小x
,这个O(n*n*m)
就是答案。
效率将为while-cycle
,因为n
的正文仅运行n - number of cities
次(其中n
),并且周期有两个周期{{1} }和m
。
我们只会首次for-cycle
运行n次,因为该路径将使用少于N
个航班 - 没有理由回到以前的城市。
修改强>
实际上,如果我们将存储Adjacency list等航班的信息,我们可以更好地获得效率:O(n * m),因为,例如,如果城市号码i
与m 相邻i ,我们将得到N * m 0 + N * m 1 + ... + N * m N = N *(m 0 + m 1 + ... + m n )= N * M,因为m i的总和< / sub> th == M.(M代表航班总数)。
More details about priority queue
答案 1 :(得分:0)
正如问题所述,实际上非常简单。
Order the flight list by the departure time.
Start with 0 planes.
For every flight,
look if there's a plane available at the source at the time of departure
If yes, select it, else add a new plane and select it.
Mark the plane as available at the destination at time of arrival + preparation
Return # of planes used