https://codeforces.net/problemset/problem/1063/B this question can be done with djikstra as well. but dont know y its giving TLE. https://ideone.com/Az9ZAL (its properly commented -running and no templates are used so wont be tough to read) .using djikstra i am assigning 1 unit weight to all the left edges. if anyone can suggest any optimization i would be very thankful. UPD-error found i was putting less than -equal sign for checking djikstra
First, if you run a Dijkstra you are using a priority queue, that adds a "log" to your time complexity and it isn't necessary because no shortest paths are needed, you just have to count how many cells you can reach, so you could do it with a normal BFS.
Second, the problem have constraints on the left and right moves.
Edit:
My comment is wrong, since you want to get to each cell with the least amount of work. You do need to use a shortest path algorithm!
if the lft moves are optimized so are the right ones because they are linear to each other for any cell
also its still 10^6log wich should run
Auto comment: topic has been updated by kumarpratyush4 (previous revision, new revision, compare).