扭曲的最短路径

编程入门 行业动态 更新时间:2024-10-24 16:22:51
本文介绍了扭曲的最短路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有 n 顶点和 m 它们之间的无向加权边(权重代表分钟)。每个顶点包含在该顶点上喝咖啡所需的分钟数。

I have n vertices and m undirected weighted edges between them (weights are representing minutes). Each vertex contains a number of minutes required to drink a coffee on that vertex.

我想确定从顶点 v 到顶点 w 但是附加限制条件是我必须在从 v 到 w )。

I want to determine the shortest amount of time (minutes) neccessary to get from vertex v to vertex w but with the additional constraint that I have to drink my coffee on exactly one of the vertices on my way from v to w).

示例:

(顶点的数字是喝咖啡所需的分钟数,边缘的重量代表了这个边缘所需的分钟数)

(number in the vertex is the amount of minutes required to drink a coffee, the weights on the edges represent the amount of minutes neccessary to travel this edge)

从 v 到 w 并在途中喝一杯咖啡,输出最短的必要时间(输出应为30 )。

Get from v to w and drink a coffe on your way, output the minimal neccessary time (output should be 30).

我目前的做法是找到Dijkstra的最短路径(总结该路径上所有边的权重)然后将该路径上咖啡时间最短的顶点值添加到我的结果中,以获得从 v 到所需的总时间> w 。

My current approach is to find the shortest path with Dijkstra (sum up the weights of all edges on that path) and then add the value of the vertex with the lowest coffee time on that path to my result in order to get the total amount of time neccessary to get from v to w.

我的方法不起作用,这是我的方法失败的一个例子(我的方法的结果是12分钟,实际结果应为6分钟):

My approach doesn't work, here is an example where my approach fails (the result of my approach is 12 minutes, the actual result should be 6 minutes) :

如何确定从顶点 v 到 w 的最短时间,以及我需要喝咖啡的约束在我的路上?

How to determine the shortest amount of time from vertex v to w with the constraint that I need to drink a coffe on my path?

推荐答案

解决这个问题的标准方法是:

The standard way to solve this problem is:

  • 制作2个图表副本 - need_coffee 版本和 had_coffee版本。

    连接每个 need_coffee 节点使用相应的 had_coffee 节点,边缘成本等于在该节点喝咖啡的成本。

    Connect each need_coffee node with the corresponding had_coffee node, with an edge cost equal to the cost of drinking coffee at that node.

    使用Dijkstra算法查找从 V_need_coffee 到 W_had_coffee

    Use Dijkstra's algorithm to find the shortest path from V_need_coffee to W_had_coffee

  • 更多推荐

    扭曲的最短路径

    本文发布于:2023-11-29 23:23:04,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1647944.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:最短   路径

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!