【发布时间】:2022-01-20 05:51:21
【问题描述】:
假设我有一个图G,其中有N 顶点和M 边。每条边都有它的长度和时间(比如说以分钟为单位),它需要遍历该边。我需要在图中找到顶点1 和N 之间的最短路径,这是在T 分钟时间内执行的。
由于时间是更有价值的资源,我们关心的是及时遍历图,并且只有在最短的时间内,我决定使用 Dijkstra 算法,我将每条边的时间视为其权重。我添加了一个向量来存储持续时间。因此,该算法返回最少的时间,而不是最少的长度。一位朋友建议在我的代码中添加此内容:
int answer(int T) {
int l = 1;
int r = M; // a very big number
int answer = M;
while (l <= r) {
int mid = (l + r) / 2;
int time = dijkstra(mid); // the parameter mid serves as an upper bound for dijkstra and I relax the edge only if its length(not time) is less than mid
if (time <= T) {
answer = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
if (best == M) {
return -1; // what we return in case there is no path in the graph, which takes less than T minutes
}
return answer;
}
这是dijkstra 方法(Graph 和std::unordered_map<int, std::vector<Node>> adjacencyList member 类的一部分):
int dijkstra(int maxLength) {
std::priority_queue<Node, std::vector<Node>, NodeComparator> heap;//NodeComparator sorts by time of edge
std::vector<int> durations(this->numberOfVertices + 1, M);
std::set<int> visited;
// duration 1->1 is 0
durations[1] = 0;
heap.emplace(1, 0, 0);
while (!heap.empty()) {
int vertex = heap.top().endVertex;
heap.pop();
// to avoid repetition
if (visited.find(vertex) != visited.end()) {
continue;
}
for (Node node: adjacencyList[vertex]) {
// relaxation
if (node.length <= maxLength && durations[node.endVertex] > durations[vertex] + node.time) {
durations[node.endVertex] = durations[vertex] + node.time;
heap.emplace(node.endVertex, durations[node.endVertex], 0);
}
}
// mark as visited to avoid going through the same vertex again
visited.insert(vertex);
}
// return path time between 1 and N bounded by maxKilograms
return durations.back();
}
这似乎可行,但对我来说似乎效率低下。老实说,我并不完全理解他的想法。在我看来,这就像随机尝试找到最佳答案(因为没有人说边缘的时间与其长度成正比)。我尝试搜索shortest path in graph with time limit,但我找到了找到最快路径的算法,而不是最短有限制的路径。甚至存在这样的算法吗?如何改进我的解决方案?
【问题讨论】:
-
dijkstra 的功能是什么?您希望您的函数答案返回什么? “最佳”变量来自哪里?该算法似乎使用二进制搜索,但很难掌握这么少的信息。请编辑您关于代码的问题。您的问题的一个快速解决方案是修改 dijkstra 算法,以便任何路径花费的时间超过允许的“无限”权重。一次通过就可以了,但请务必检查答案是否有效。
-
我编辑了我的问题。我希望现在信息足够了。感谢您调查我的问题。
标签: c++ algorithm graph-theory shortest-path