简体   繁体   English

具有更新的最短路径算法

[英]Shortest Path Algorithm With Updates

There are N cities and there are M bidirectional roads connected to them , I have to find the shortest path between two fixed cities A and B. 有N个城市,并且有M个双向道路连接到它们,我必须找到两个固定城市A和B之间的最短路径。

But the problem is there are Q queries given such that path between two cities is blocked , i have to find the shortest path in each Q queries. 但问题是有Q查询给出了两个城市之间的路径被阻止,我必须找到每个Q查询中的最短路径。

My time Complexity in my brute force Algorithm is O(QNlogN) which give me Time Limit Exceeded Error, How can i improve my solution please Help 我的时间复杂性在我的蛮力算法是O(QNlogN),它给我时间限制超出错误,我怎样才能改善我的解决方案请帮助

Pseduo Code: Pseduo代码:

for u in Q:
  cin>>a>>b;
graph[a][b] = graph[b][a] = INFINITY VALUE
dijkstra algorithm();
cout<<Distance[D]<<endl; 

Problem LINK 问题链接

MY CODE Which Is giving me Time Limit Exceeded Error 我的代码给了我超出时间限制的错误

Plese Help How can I improve my algorithm ? 请帮助我如何改进算法?

The paper Vickrey Prices and Shortest Paths: What is an edge worth? 论文Vickrey价格和最短路径:什么是优势? by John Hershberger and Subhash Suri shows how to solve this problem in time O(NlogN+M) where N is the number of vertices, and M is the number of edges. 作者:John Hershberger和Subhash Suri展示了如何在时间O(NlogN + M)中解决这个问题,其中N是顶点数,M是边数。

This allows you to precalculate the M answers depending on which road is blocked, so you can answer each query in O(1), for a total complexity of O(NlogN+M+Q). 这允许您根据阻止的道路预先计算M个答案,因此您可以在O(1)中回答每个查询,总复杂度为O(NlogN + M + Q)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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