[英]How to find the cheapest combo of metro subscription
Lets say we have the following fares available:假设我们有以下可用票价:
... with a price for every one of them. ...每个人都有一个价格。
The problem is: **How to determine what set of subscriptions to chose given a date of arrival and a date of departure?问题是:**如何确定在给定到达日期和离开日期的情况下选择哪组订阅? ** **
Lets say we want the solution for n between 1 and 8, n being the number of time we take the metro daily (so we assume we take the metro the same number of time every day)假设我们想要 n 在 1 到 8 之间的解决方案,n 是我们每天乘坐地铁的次数(因此我们假设我们每天乘坐地铁的次数相同)
For example it would say something like:例如它会说这样的话:
n = 1 Arriving on Friday 19th and leaving Thursday 23th, the best is taking the 1 trip, then the weekend, then the 2 trips (didnt calculate but you see the point) n = 1 19号周五到达,23号周四离开,最好是走1趟,然后是周末,然后是2趟(没有计算,但你明白了)
n = 2... n = 2 ...
I have found examples with only 1 day, 2 days, 7 days fares with dynamic programming, but it looks a lot harder when you considerate the days of the week.我找到了只有 1 天、2 天、7 天票价的动态规划示例,但当你考虑一周中的几天时,它看起来要困难得多。
Thanks:)谢谢:)
I like to view this kind of dynamic program as finding a shortest path in a directed acyclic graph.我喜欢将这种动态程序视为在有向无环图中寻找最短路径。
Each node of the graph encodes图的每个节点编码
Each arc represents either每条弧线代表
The time-limited passes advance the day to the first day they no longer work.限时通行证提前一天到他们不再工作的第一天。 The trip-limited passes increase the number of remaining trips.行程限制通行证增加剩余行程的次数。 The zero-cost arcs advance the day by one while decreasing the number of remaining trips by n.零成本弧将一天提前一天,同时将剩余行程数减少 n。
Given the shortest path, it is easy to decode it to a plan for purchasing passes.给定最短路径,很容易将其解码为购买通行证的计划。
(PS I don't know what the rules are on, eg, purchasing a week pass on a Tuesday for the rest of the week. Even if this is not allowed, you're going to want to put arcs for the time-limited passes that could have been purchased on a previous day during the travel period.) (PS我不知道是怎么规定的,比如每周rest的星期二买周票。就算不允许,你也会想在限时放弧线可以在旅行期间的前一天购买的通行证。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.