简体   繁体   English

如何找到最便宜的地铁订阅组合

[英]How to find the cheapest combo of metro subscription

Lets say we have the following fares available:假设我们有以下可用票价:

  • 1 trip 1 趟
  • 2 trips 2 趟
  • 10 trips 10次
  • unlimited weekend (saturday to sunday, not 2 days)无限周末(周六至周日,不是 2 天)
  • 1 day 1天
  • 3 days 3天
  • unlimited week (monday to sunday, not 7 days)无限周(周一到周日,不是7天)
  • unlimited month (1st to last day of month)无限制的月份(每月的第一天到最后一天)

... 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图的每个节点编码

  • what the current day is (either during the travel period or the day after), and今天是几号(在旅行期间或之后的一天),以及
  • how many trips remain on trip-limited passes (at most n + 9).有多少次旅行限制在旅行限制通行证上(最多 n + 9)。

Each arc represents either每条弧线代表

  • purchasing a specific pass at a specific time (the length of the arc is the cost of this pass), or在特定时间购买特定通行证(弧长是该通行证的费用),或
  • using trip-limited passes to cover the day's trips (the length of the arc is zero).使用行程限制通行证来涵盖当天的行程(弧长为零)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM