假设我们有一个无向加权图。我们的任务是查找所有路径,这些路径的起点和终点相同,且总成本等于N。我认为可以使用修改后的Dijkstra算法结合BFS或DFS来完成,但我不知道如何实现这样的事情。感谢任何帮助。
void DFS(Vertex current, Vertex goal, List<Vertex> path, int money_left) {
// oops
if (money_left < 0)
return;
// avoid cycles
if (contains(path, current)
return;
// got it!
if (current == goal)) {
if (money_left == 0)
print(path);
return;
}
// keep looking
children = successors(current); // optionally sorted from low to high cost
for(child: children)
DFS(child, add_path(path, child), money_left - cost(child));
}
DFS(start, goal, List<Vertex>(empty), N)
cost <= N
的路径。 - TemplateRex
(n-2)!
个解决方案(完整图,所有边长度相等,需要汉密尔顿路径),因此枚举速度会非常慢。 - John DvorakN
的路径,对吗?可能会有_很多_这样的路径。 - John Dvorak