Graph Cycles and Longest Path Computations

Cycle Detection and Connectivity Union-Find, DFS/BFS, and topological sorting can detect cycles and verify graph connectivity in $O(n + m)$ time. Topological sorting also identifies cycles in directed graphs. Problem: Acyclic Directed Graph Check Description: Given a directed graph with $N$ nodes and $M$ edges where each edge $(a_i, b_i)$ conne ...

Posted on Wed, 23 Sep 2026 16:42:07 +0000 by casty

Essential Graph Algorithms for Programming Contests

Dikjstra's Algorithm (Adjacency Matrix) #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int MAX_NODES = 510; int node_count, edge_count; int graph[MAX_NODES][MAX_NODES]; int min_distance[MAX_NODES]; bool visited[MAX_NODES]; int dijkstra_shortest_path() { memset(min_distance, 0x3f, s ...

Posted on Fri, 18 Sep 2026 16:11:15 +0000 by sledgeweb

Calculating Network Delay Time with Dijkstra and Floyd-Warshall Algorithms

Dijkstra's Algorithm ApproachDijkstra's algorithm is suitable for finding the shortest paths from a single source node to all other nodes in a weighted graph with non-negative weights. For the network delay problem, we aim to determine the maximum shortest-path distance from the source node k to every other node. If any node remains unreachable ...

Posted on Mon, 07 Sep 2026 16:54:29 +0000 by mindfield

Dijkstra's Algorithm with Heap Optimization: Pseudocode and Implementation Guide

Understanding Dijkstra's Algorithm Dijkstra's algorithm solves the single-source sohrtest path problem in graphs where all edge weights are non-negative. Given a source node s, it computes the shortest distance from s to every other reachable node in the graph. Core Intuition Initially, only the distance from the source to itself is known (0), ...

Posted on Thu, 20 Aug 2026 16:54:42 +0000 by mike16889

Dijkstra's Algorithm for Single-Source Shortest Paths

Dijkstra's algorithm computes the shortest path distances from a designated source vertex to all other vertices in a weighted, directed or undirected graph with non-negative edge weights. It operates greedily: at each step, it selects the unvisited vertex with the smallest known distance from the source, marks it as visited, and relaxes (i.e., ...

Posted on Tue, 18 Aug 2026 16:44:06 +0000 by Arya

Advanced Graph Traversal and Dynamic Programming Strategies in C++

In competitive programming and system design, efficiently navigating complex networks and optimizing resource allocation often require mastery of graph algorithms. The following collection demonstrates implementations for several classic challenges, including broadcast optimization, structural analysis of trees, and constrained dynamic programm ...

Posted on Sat, 15 Aug 2026 16:53:32 +0000 by I Am Chris

Algorithm Problem Solutions: Snowflakes, Sequences, and Graph Theory

Problem 1: Unique Snowflake Collection Problem Statement: At n different times, snowflakes of various shapes fall (represented by distinct integers). We want to collect snowflakes from time a to time b such that no duplicate shapes are collected, and the total number of snowflakes collected is maximized. Solution Approach: Two Pointers Techniqu ...

Posted on Thu, 06 Aug 2026 16:35:07 +0000 by mispris006

Essential Algorithms for Programming Competition Preparation

This collection presents fundamental algorithms and their applications to simple problems, primari sourced from the Lanqiao Cup competition. The problems are relatively straightforward, focusing more on algorithm templates and basic approaches. For better algorithm retention, the implementations are concise, frequently utilizing built-in C++ fu ...

Posted on Wed, 29 Jul 2026 16:32:20 +0000 by ThaboTheWuff

Finding the Shortest Path with Time-Based Road Closures using Dijkstra's Algorithm

This problem involves finding the shrotest path in a graph where certain edges are temporarily closed. The graph has $N$ nodes and $M$ edges, with $N \le 1000$ and $M \le 10000$. Given the constraints, an adjacency matrix is a suitable choice for representing the graph. We need to determine the optimal travel time for a character, let's call th ...

Posted on Tue, 28 Jul 2026 17:09:19 +0000 by lar5

Essential Algorithm Templates for Competitive Programming

Data Structures Segment Tree #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; const int MAXN = 100010; int n, m; vector<ll> arr; vector<ll> tree; vector<ll> lazy; inline ll read() { ll x = 0, f = 1; char ch = getchar(); while (ch < '0' || ...

Posted on Tue, 28 Jul 2026 16:49:08 +0000 by Daggeth