Beat by bit

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

Here is your corrected code for first question Problem1.
What you did wrong was did not take care of repeated characters in input string.

Your second answer does not work as it has a complexity of O(n^2), which clearly would not work as n<=10^4.
You should try Dijkstra’s Algorithm as it takes O(n log n) time.