We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In the absence of the editorial, I'll provide a brief explanation of my solution for those who may be struggling (This could be seen as a self-note rather than a complete explanation) : Use any shortest-path algorithm to find the shortest distance from Source and Sink to other nodes. Construct a graph so-called shortest-path graph G in which an edge u->v presents if and only if d[u] + w(u,v) == d[v] where d[x] is the shortest distance from Source to x and w(u,v) is the weight of that edge. Consider the induced graph G' of vertices that can reach Sink from G, if the deleted edge is not a bridge of G', then the shortest distance from Source to Sink won't change. Otherwise, that bridge separates G into two disjoint components, the new shortest distance is d_source[u] + w(u,v) + d_sink[v] assuming that u belongs to the source's and v belongs to the sinkscomponent. By applying sweepline algorithm + a min heap / multiset, we can precompute the answer for all such bridges in O(N + ElogN) times.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Going to the Office
You are viewing a single comment's thread. Return to all comments →
In the absence of the editorial, I'll provide a brief explanation of my solution for those who may be struggling (This could be seen as a self-note rather than a complete explanation) : Use any shortest-path algorithm to find the shortest distance from
Source
andSink
to other nodes. Construct a graph so-called shortest-path graphG
in which an edge u->v presents if and only ifd[u] + w(u,v) == d[v]
whered[x]
is the shortest distance fromSource
tox
andw(u,v)
is the weight of that edge. Consider the induced graphG'
of vertices that can reachSink
fromG
, if the deleted edge is not a bridge ofG'
, then the shortest distance fromSource
toSink
won't change. Otherwise, that bridge separatesG
into two disjoint components, the new shortest distance isd_source[u] + w(u,v) + d_sink[v]
assuming that u belongs to the source's and v belongs to the sinkscomponent. By applying sweepline algorithm + a min heap / multiset, we can precompute the answer for all such bridges in O(N + ElogN) times.