【问题标题】:What can be a correct approach for SPOJ COURIERSPOJ COURIER 的正确方法是什么
【发布时间】:2015-06-20 17:20:45
【问题描述】:

我正在尝试解决spoj 上的COURIER 问题。我能够理解我必须使用动态编程方法来解决 TSP,但我无法完全理解我在同一对城市之间处理多个包裹的方法是否正确。我的伪代码如下:

1) Use floyd warshall to find all pair shortest path in O(n^3). Some pair of cities are connected by more than one roads, I can just keep the shortest one for my undirected graph.
2) Add the shortest cost for each request start to end.
3) Create a new directed graph for each of 12 requests and homecity. The node of this new graph will be a merge of each request's source and destination. The edge weight between a->b can be calculated by shortest path between 'a' request's destination to 'b' request's source.I am thinking of duplicating the pairs if I have multiple request between them.
4) Use a TSP DP to solve this new undirected 13 city TSP problem. O(n^2 2^n) would come around 1384448. Not sure whether this will time out for multiple test cases.

您能否提供您的意见,因为我创建这个新的有向图的方法会使问题复杂化?我没有使用只有 5 个这样不同的请求的信息。我知道我可以解决这个问题并且知道,但我想先获得一些解决方案的建议。

【问题讨论】:

    标签: algorithm language-agnostic dynamic-programming


    【解决方案1】:

    很好的问题。

    做完第1)点后,你可以忽略所有不是来源或地址的城市。

    因此,您有旅行者当前所在的 10 个城市和 2^12 种可能的任务组合尚未完成。

    您可以只使用两个参数进行 DP:当前城市和要完成的交付,您可以使用位掩码进行存储。

    编辑:

    如前所述,您有两个参数:p 跟踪当前位置和 mask 跟踪您已经完成的访问。

    掩码用作位掩码:http://en.wikipedia.org/wiki/Mask_%28computing%29

    您从掩码 0 开始,二进制为 000000000000。例如,当您执行第 5 次请求旅行时,您将掩码更改为:000000010000 等。

    您首先调用 f(p=0, mask=0)。

    当您求解 f(p, mask) 时,您有两个选择。您可以移动到任何其他城市 p2。如果这是您尚未完成的旅行之一,您可以进行旅行 p -> p2。在所有这些选项中,您必须选择最好的一个。

    这个问题非常棘手,我建议首先使用位掩码解决更简单的问题。你可以在这里找到一些:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=778

    【讨论】:

    • 非常感谢您的回答。我仍然无法理解您如何使 TSP 多次访问城市以进行多次交付?您能否添加一个小伪代码或多一点描述。这将是很大的帮助。
    • 无需感谢我 :) 如果您喜欢我的回答,请点赞并接受 :) 将对如何处理它进行简短编辑,让您休息
    【解决方案2】:

    我不知道你现在是否需要答案,但这是我所做的:

    最初你的方法是正确的,你必须申请 floyd-warshall 以获得所有对的最短距离。现在是经典的dp+bitmask 问题。只有12个操作,你要安排这12个操作 这样你就得到了最低限度。

    这可以通过使用位掩码来完成:000000000000
    你有这 12 个状态 => 如果你选了一个,你就不能再选了。而且因为 (2^12=4096) 这样的数字存储起来并不难。

    我的 dp 状态非常直接:“掩码号”和“父级”
    PARENT->你上次做的操作

    dp[掩码][par]
    希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 2012-10-10
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多