Lets build DAG using algorithm of finding SCC. Give a number to mark each SCC.
Now reverse all the edges of the DAG.
Take all the edges of the new graph in a stack with topological order.
Make all the nodes distance = 0 in dis array.
Iterate stack until it's become empty. Take the top and iterate all the edges of this nodes from the Reverse DAG. For edge u -> v with cost c,
If ( dis [ v ] < dis [ u ] + c )dis [ v ] = dis [ u ] + c.
Complexity - O( N+M+Q )
Why this works?
Think!

Statistics

94% Solution Ratio
NirjhorEarliest, Oct '19
mbsabbirr127Fastest, 0.0s
Burn_FireblazeLightest, 16 MB
Tareq_AbrarShortest, 1220B
Toph uses cookies. By continuing you agree to our Cookie Policy.