Datasets:
Download code/math_code_006_solution.py from efe-T/bilingual-math-code-verification-v1: direct link, hf CLI and curl.
- Browser
- Download file 738 Bytes
-
https://huggingface.co/datasets/efe-T/bilingual-math-code-verification-v1/resolve/main/code/math_code_006_solution.py
- Command line
-
hf download hf://datasets/efe-T/bilingual-math-code-verification-v1/code/math_code_006_solution.py
-
curl -L -o math_code_006_solution.py https://huggingface.co/datasets/efe-T/bilingual-math-code-verification-v1/resolve/main/code/math_code_006_solution.py
738 Bytes
| import heapq | |
| graph = {"A": [("B", 2), ("C", 3)], "B": [("C", 1), ("D", 4)], | |
| "C": [("D", 2), ("E", 3)], "D": [("E", 1)], "E": []} | |
| dist = {v: float("inf") for v in graph} | |
| ways = {v: 0 for v in graph} | |
| dist["A"], ways["A"] = 0, 1 | |
| queue = [(0, "A")] | |
| while queue: | |
| d, u = heapq.heappop(queue) | |
| if d != dist[u]: | |
| continue | |
| for v, weight in graph[u]: | |
| candidate = d + weight | |
| if candidate < dist[v]: | |
| dist[v], ways[v] = candidate, ways[u] | |
| heapq.heappush(queue, (candidate, v)) | |
| elif candidate == dist[v]: | |
| ways[v] += ways[u] | |
| result = {"distance": dist["E"], "shortest_path_count": ways["E"]} | |
| import json | |
| print(json.dumps(result, ensure_ascii=False, sort_keys=True)) | |