I tried these two problems in yesterday’s contest Bit by Bit. First one gave WA and second one TLE. The TLE problem is my first shortest path program. Is there any way to optimize it or is there another approach for the problem? Also, why am I getting WA in first one where approach is same as the ones used by other contestants?

Problem and

WA solution

Problem and Solution TLE