| Ko

The Floyd–Warshall algorithm

Most shortest-path algorithms solve for distances from one vertex to the rest. Dijkstra and Bellman-Ford both start from a single source. For distances from every vertex to every other vertex, you could run Dijkstra V times, but that rebuilds the priority queue and adjacency list each time, and it fails entirely if any edge is negative. Floyd-Warshall does not fix a source. It puts down a single distance matrix and runs one triple loop, and when it finishes the shortest distance for every pair is in the matrix. The code is twelve lines, it handles negative edges, and it reports whether a negative cycle exists. ...

June 17, 2024 · 8 min · 1612 words · In-Jun

Baekjoon 27440 Make it One 3 Solution

Baekjoon 27440 “Make it One 3” is a problem that asks for the minimum number of operations needed to reduce a very large integer up to 10^18 to 1 using three operations (divide by 3, divide by 2, subtract 1). Since the input range reaches 10^18, the standard dynamic programming approach (O(N) time, O(N) space) is infeasible. The problem must be solved efficiently using a recursive recurrence relation and hash map-based memoization, achieving O(log² n) time complexity. ...

May 29, 2024 · 7 min · 1446 words · In-Jun
[email protected]