rewrite dijkstra in python