Implement Dijkstra's Algorithm in python

What i did

I also implemented Dijkstra's algorithm in python to study Dijkstra's algorithm. As an example, I used "D-Treasure Hunt of AtCoder Beginner Contest 035" that first appeared in "Dijkstra AtCoder".

Difference between Dijkstra's algorithm and DFS or BFS

I don't understand the difference between Dijkstra's algorithm, DFS, and BFS, so I will briefly summarize the results of my research. (DFS and BFS are implemented below) Implement depth-first search (DFS) and breadth-first search (BFS) in python

--DFS (depth-first search) / BFS (breadth-first search) --Algorithms for searching for unweighted graphs --Dijkstra's algorithm --Algorithm for searching for breadth-first graphs (an extended version of BFS because it uses queues (more precisely, weighted queues))

Dijkstra's Algorithm

Note

start → The shortest route to reach the destination is g_list, go Manage the shortest route from destination to start with b_list, back

#ABC035 D Treasure Hunt

import heapq

def dijkstra(s, n, c_list):
    _list = [float("Inf")]*n
    _list[s] = 0
    hq = [[0,s]]
    heapq.heapify(hq)
    while len(hq) > 0:
        _ci, _i = heapq.heappop(hq)
        if _list[_i] < _ci:
            continue
        for _cj,_j in c_list[_i]:
            if _list[_j] > (_list[_i] + _cj):
                _list[_j] = _list[_i] + _cj
                heapq.heappush(hq, [_list[_j],_j])
    return _list
                
n, m, t = map(int, input().split())
a_list = [int(x) for x in input().split()]
g_list = [[] for i in range(n)]
b_list = [[] for i in range(n)]

for i in range(m):
    _a, _b, _c = map(int, input().split())
    g_list[_a-1].append([_c,_b-1])
    b_list[_b-1].append([_c,_a-1])

go = dijkstra(0, n, g_list)
back = dijkstra(0, n, b_list)
ans = 0

for i in range(n):
    _tmp = t - (go[i] + back[i])
    if a_list[i]*_tmp > ans:
        ans = a_list[i]*_tmp

print(ans)

Recommended Posts

Implement Dijkstra's Algorithm in python
Algorithm in Python (Dijkstra's algorithm)
Implement Enigma in python
Ant book in python: Sec.2-5 Dijkstra's algorithm
Genetic algorithm in python
Implement recommendations in Python
Implement XENO in python
Algorithm in Python (Bellman-Ford)
Implement sum in Python
Implement Traceroute in Python 3
Implement PRML algorithm in Python (almost Numpy only)
Algorithm in Python (primality test)
Implement naive bayes in Python 3.3
Implement ancient ciphers in python
Reproduce Euclidean algorithm in Python
Algorithm in Python (binary search)
[Python3] Dijkstra's algorithm with 14 lines
Implement Redis Mutex in Python
Implement extension field in Python
Implement fast RPC in Python
Implement Slack chatbot in Python
Python algorithm
Implement stacking learning in Python [Kaggle]
Algorithm in Python (breadth-first search, bfs)
Sorting algorithm and implementation in Python
Write A * (A-star) algorithm in Python
Implement the Singleton pattern in Python
Algorithm in Python (depth-first search, dfs)
Implementing a simple algorithm in Python 2
Quickly implement REST API in Python
Implementation of Dijkstra's algorithm with python
Algorithm (segment tree) in Python (practice)
Run a simple algorithm in Python
Quadtree in Python --2
Python in optimization
CURL in python
I tried to implement PLSA in Python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Implement __eq__ etc. generically in Python class
I tried to implement permutation in Python
Meta-analysis in Python
Python memorandum (algorithm)
Unittest in python
Algorithm in Python (ABC 146 C Binary Search
Collectively implement statistical hypothesis testing in Python
I tried to implement PLSA in Python 2
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Plink in Python
I tried to implement ADALINE in Python
Constant in python
Write a simple greedy algorithm in Python
I tried to implement PPO in Python
Lifegame in Python.