Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def dist_min():
- """
- Gives the nearest node to the origin that is in the set
- """
- min = 10000000
- elem = -1
- for i in range(n):
- if i in set and distTo[i] <= min:
- min = distTo[i]
- elem = i
- return elem
- def neighbors(node):
- """
- Return every neighbor of a node, with the weight of the edge that leads to it
- """
- ret = []
- for (f, t, edge) in paths:
- if f-1 == node:
- weight = edge + tasks[t-1]
- ret.append((t-1, weight))
- return ret
- n = len(tasks)
- distTo = [10000000 for x in range(n)]
- distTo[0] = tasks[0] # distance to first task is its duration
- set = deque([0]) # set of nodes to explore. contains first task
- while set: # while set is not empty
- min = dist_min() # take nearest node to explore
- set.remove(min) # it has been explored
- for neigh, weight in neighbors(min): # for all its neighbors
- if distTo[neigh] > distTo[min] + weight: # if a destination can be reached more quickly:
- distTo[neigh] = distTo[min] + weight # update that distance
- if not set.__contains__(neigh):
- set.append(neigh)
- return distTo[n-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement