Your First Problem
Let’s solve a classic shortest path problem using the Dijkstra algorithm snippet. This guide will walk you through a complete example from template to solution.Problem Statement
Given a weighted graph withn nodes and m edges, find the shortest distance from a source node to all other nodes.
Input:
- First line:
n(number of nodes),m(number of edges) - Next
mlines:u v w(edge fromutovwith weightw) - Last line:
s(source node)
nspace-separated integers representing shortest distances from source
Building the Solution
Start with a Template
Create a new file
solution.cpp and type template_std then trigger autocomplete:Add Directed Graph Implementation
Type
graph_digraph to add the directed graph class:Use
graph_undigraph for undirected graphs instead.Complete Solution
Here’s the final solution combining all snippets:solution.cpp
Testing the Solution
Sample Input
Expected Output
Compile and Run
Competitive Programming Workflow
Efficient Snippet Usage
- Start Fast: Use
template_stdor competition-specific templates liketemplate_googleortemplate_usaco - Build Incrementally: Add only the snippets you need for the problem
- Test Quickly: Use
template_randomto generate test cases - Debug Efficiently: Add
misc_debugwhen you need detailed output
Common Snippet Combinations
Graph Problems
Graph Problems
Range Query Problems
Range Query Problems
String Problems
String Problems
Dynamic Programming with Math
Dynamic Programming with Math
Pro Tips
Use Template Headers
template_header provides optimized pragmas and common macros:- Compiler optimizations with
#pragma GCC - Shortened macros:
PB,SZ,all,rall - Loop macros:
forn,rof
Debug Smartly
misc_debug provides comprehensive debug output:- Works with vectors, maps, sets, pairs
- Only compiles when
-DDEBUGflag is set - Use
log(variable)to print
Handle Multiple Test Cases
Use
template_cases for problems with multiple test cases:- Automatic case numbering
- Clean structure for T test cases
Fast I/O Operations
All templates include fast I/O:
ios_base::sync_with_stdio(0)cin.tie(0)- Critical for large input problems
Next Steps
Snippet Usage
Learn advanced snippet triggers and editor configuration
Available Templates
Explore all available template options
Competitive Programming
Master best practices and optimization techniques
API Reference
Browse all available algorithms and data structures