#!/usr/bin/env python3
"""Auto-generated by AGI Loop cycle #1292
Task: [GWT_BROADCAST] Processing task...

</think>

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

import heapq

def dijkstra(graph, start, end):
    distances = {node: float('inf') for node in graph}
    distances[start] = 0
    priority_queue = [(0, start)]
    previous_nodes = {node: None for node in graph}

    while priority_queue:
        current_distance, current_node = heapq.heappop(priority_queue)
        if current_node == end:
            break
        if current_distance > distances[current_node]:
            continue
        for neighbor, weight in graph[current_node].items():
            distance = current_distance + weight
            if distance < distances[neighbor]:
                distances[neighbor] = distance
                previous_nodes[neighbor] = current_node
                heapq.heappush(priority_queue, (distance, neighbor))

    # Reconstruct path
    path = []
    current = end
    while current:
        path.append(current)
        current = previous_nodes[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_node = 'A'
    end_node = 'D'
    path, shortest_distance = dijkstra(graph, start_node, end_node)
    print(f"Shortest path from {start_node} to {end_node}: {path}")
    print(f"Shortest distance: {shortest_distance}")