|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "c14e8ac8", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "from collections import heapq\n", |
| 11 | + "\n", |
| 12 | + "h = []\n", |
| 13 | + "INF = int(1e9)\n", |
| 14 | + "n, m = map(int, input().split())\n", |
| 15 | + "start = int(input())\n", |
| 16 | + "\n", |
| 17 | + "graph = [[] for i in range(n+1)]\n", |
| 18 | + "# 노드에 연결되어 있는 노드에 대한 정보를 담는 리스트를 만들기\n", |
| 19 | + "distance = [INF]*(n+1)\n", |
| 20 | + "\n", |
| 21 | + "for _ in range(m):\n", |
| 22 | + " a, b, c = map(int, input().split())\n", |
| 23 | + " graph[a].append((b, c))\n", |
| 24 | + " \n", |
| 25 | + "def dijkstra(start):\n", |
| 26 | + " h = []\n", |
| 27 | + " heapq.heappush(h, (0, start))\n", |
| 28 | + " distance[start] = 0\n", |
| 29 | + " \n", |
| 30 | + " while h:\n", |
| 31 | + " dist, now = heapq.heappop(h)\n", |
| 32 | + " \n", |
| 33 | + " if distance[now] < dist:\n", |
| 34 | + " continue\n", |
| 35 | + " for i in graph[now]:\n", |
| 36 | + " cost = dist + i[1]\n", |
| 37 | + " if cost < distance[i[0]]:\n", |
| 38 | + " distance[i[0]] = cost\n", |
| 39 | + " heapq.heappush(q, (cost, i[0]))\n", |
| 40 | + " \n", |
| 41 | + "dijkstra(start)\n", |
| 42 | + "\n", |
| 43 | + "for i in range(1, n+1):\n", |
| 44 | + " if distance[i] == INF:\n", |
| 45 | + " print(\"INFINITY\")\n", |
| 46 | + " else:\n", |
| 47 | + " print(distance[i])" |
| 48 | + ] |
| 49 | + } |
| 50 | + ], |
| 51 | + "metadata": { |
| 52 | + "kernelspec": { |
| 53 | + "display_name": "Python 3 (ipykernel)", |
| 54 | + "language": "python", |
| 55 | + "name": "python3" |
| 56 | + }, |
| 57 | + "language_info": { |
| 58 | + "codemirror_mode": { |
| 59 | + "name": "ipython", |
| 60 | + "version": 3 |
| 61 | + }, |
| 62 | + "file_extension": ".py", |
| 63 | + "mimetype": "text/x-python", |
| 64 | + "name": "python", |
| 65 | + "nbconvert_exporter": "python", |
| 66 | + "pygments_lexer": "ipython3", |
| 67 | + "version": "3.10.0" |
| 68 | + } |
| 69 | + }, |
| 70 | + "nbformat": 4, |
| 71 | + "nbformat_minor": 5 |
| 72 | +} |
0 commit comments