#!/usr/bin/env python3
"""Auto-generated by AGI Loop cycle #1280
Task: , 1.00

</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:49:21.733756
"""

import heapq

def dijkstra(graph, start, end):
    shortest_distances = {node: float('inf') for node in graph}
    shortest_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 > shortest_distances[current_node]:
            continue
        for neighbor, weight in graph[current_node].items():
            distance = current_distance + weight
            if distance < shortest_distances[neighbor]:
                shortest_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, shortest_distances[end]

if __name__ == '__main__':
    # Example graph as an adjacency list
    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, distance = dijkstra(graph, start_node, end_node)
    print(f"Shortest path from {start_node} to {end_node}: {path}")
    print(f"Shortest distance: {distance}")