Implement main logic for dijkstra algorithm