#!/usr/bin/env python3
"""Auto-generated by AGI Loop cycle #1282
Task: \boxed{}</think>

Write a Python function that calculates the shortest path between two nodes in a graph using Dijkstra's algorithm, given the graph as an adjacency list.
Generated: 2026-02-13T02:52:14.404860
"""

import heapq

def dijkstra_shortest_path(graph, start, end):
    distances = {node: float('infinity') for node in graph}
    distances[start] = 0
    heap = [(0, start)]
    previous = {node: None for node in graph}
    
    while heap:
        current_distance, current_node = heapq.heappop(heap)
        if current_node == end:
            break
        if current_distance > distances[current_node]:
            continue
        for neighbor, weight in graph[current_node]:
            distance = current_distance + weight
            if distance < distances[neighbor]:
                distances[neighbor] = distance
                previous[neighbor] = current_node
                heapq.heappush(heap, (distance, neighbor))
    
    path = []
    current = end
    while current:
        path.append(current)
        current = previous[current]
    path.reverse()
    return path, distances[end]

if __name__ == '__main__':
    graph = {
        'A': [('B', 1), ('C', 4)],
        'B': [('A', 1), ('C', 2), ('D', 5)],
        'C': [('A', 4), ('B', 2), ('D', 1)],
        'D': [('B', 5), ('C', 1)]
    }
    start = 'A'
    end = 'D'
    path, distance = dijkstra_shortest_path(graph, start, end)
    print(f"Shortest path: {path}, Distance: {distance}")