fixing the backtracking issues in dijstra