{
  "name": "hard-graph-shortest-path",
  "description": "Implement Dijkstra's algorithm and solve a shortest path problem",
  "dataset": "terminal-bench-local",
  "difficulty": "hard",
  "instruction": "There is a file called graph.txt in the current directory containing a weighted directed graph. Each line has the format: source destination weight. Write a Python script called shortest_path.py that:\n\n1. Reads the graph from graph.txt\n2. Implements Dijkstra's algorithm (do not use networkx or other graph libraries)\n3. Finds the shortest path from node 'A' to node 'H'\n4. Writes to result.txt two lines:\n   - Line 1: the shortest distance (integer)\n   - Line 2: the path as space-separated node names (e.g., 'A C F H')\n\nIf multiple shortest paths exist, output any valid one.",
  "setup_files": {
    "graph.txt": "A B 4\nA C 2\nB D 3\nB E 1\nC B 1\nC F 5\nD H 4\nE D 1\nE G 7\nF E 2\nF H 3\nG H 1\n",
    "test_shortest_path.py": "import sys\n\ntry:\n    with open('result.txt') as f:\n        lines = f.read().strip().split('\\n')\nexcept:\n    print('ERROR: result.txt not found')\n    sys.exit(1)\n\npassed = 0\nfailed = 0\n\ndef check(desc, got, expected):\n    global passed, failed\n    if got == expected:\n        passed += 1\n    else:\n        print(f\"FAIL {desc}: got {got}, expected {expected}\")\n        failed += 1\n\n# Shortest distance A->H is 8 (A->C->B->E->D->H = 2+1+1+1+4=9, or A->C->F->H = 2+5+3=10, or A->B->E->D->H = 4+1+1+4=10, or A->C->B->D->H = 2+1+3+4=10, or A->C->B->E->D->H = 2+1+1+1+4=9, or A->C->F->E->D->H = 2+5+2+1+4=14, wait let me recalculate)\n# A->C (2), C->B (1), B->E (1), E->D (1), D->H (4) = 9\n# A->B (4), B->E (1), E->D (1), D->H (4) = 10\n# A->C (2), C->F (5), F->H (3) = 10\n# A->C (2), C->B (1), B->D (3), D->H (4) = 10\n# A->C (2), C->F (5), F->E (2), E->D (1), D->H (4) = 14\n# A->C (2), C->F (5), F->E (2), E->G (7), G->H (1) = 17\n# Shortest: A->C->B->E->D->H = 9\ncheck('shortest distance', int(lines[0].strip()), 9)\n\n# Validate the path\npath = lines[1].strip().split()\ncheck('path starts at A', path[0], 'A')\ncheck('path ends at H', path[-1], 'H')\n\n# Validate path is connected and has correct total weight\ngraph = {}\nwith open('graph.txt') as f:\n    for line in f:\n        parts = line.strip().split()\n        if len(parts) == 3:\n            graph[(parts[0], parts[1])] = int(parts[2])\n\ntotal = 0\nvalid_path = True\nfor i in range(len(path) - 1):\n    edge = (path[i], path[i+1])\n    if edge not in graph:\n        valid_path = False\n        print(f\"FAIL: edge {edge} not in graph\")\n        break\n    total += graph[edge]\n\ncheck('path is valid', valid_path, True)\ncheck('path total equals distance', total, 9)\n\nprint(f\"{passed}/{passed+failed} tests passed\")\nif failed > 0:\n    sys.exit(1)\n"
  },
  "verify": "cd $BENCH_WORK_DIR && python3 shortest_path.py && python3 test_shortest_path.py",
  "timeout": 240000,
  "tags": ["hard", "algorithm", "graph", "implementation", "python"]
}
