File size: 551 Bytes
93237a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
edges = [("A","B",2),("A","C",3),("B","C",1),("B","D",4),
         ("C","D",2),("C","E",3),("D","E",1)]
paths = []
def visit(path, cost):
    if path[-1] == "E":
        paths.append((path, cost))
        return
    for u, v, weight in edges:
        if u == path[-1] and v not in path:
            visit(path + [v], cost + weight)
visit(["A"], 0)
best = min(cost for _, cost in paths)
result = {"distance": best, "shortest_path_count": sum(cost == best for _, cost in paths)}
import json
print(json.dumps(result, ensure_ascii=False, sort_keys=True))