Skip to content

Conversation

@AnjaliPai16
Copy link
Contributor

🎯 Problem Information

Problem Name: Shortest Routes 1
Category: graph algorithms
CSES Link: https://cses.fi/problemset/task/1671/
Difficulty: [Medium]

📝 Description

This pull request implements a solution for the Shortest Routes I problem from CSES.
The task is to find the shortest distance from node 1 (source) to every other node in a directed weighted graph with n nodes and m edges.
If a node is not reachable from node 1, its distance is considered infinite (but in this problem, all nodes are assumed reachable or output remains large).
The solution leverages Dijkstra’s algorithm optimized with a min-priority queue (min-heap) for efficient extraction of the next closest node.

🧩 Solution Approach

  • Algorithm Used: Dijkstra’s Algorithm (using a priority queue)
  • Step-by-Step:
    1)Initialize distances as INF and set dist[1] = 0 for the source node.
    2)Use a min-heap to repeatedly extract the node with the smallest tentative distance.
    3)For each neighbor of the current node, check if the new path offers a smaller distance, if so, update and push to the heap.
    4)Continue until all nodes are processed or heap is empty.
    5)Print all shortest distances from node 1 to every other node.
  • Time Complexity: O((n + m) * log n)
  • Space Complexity: O(n+m)
  • Key Insights:

1)Using a priority queue ensures Dijkstra runs efficiently even for large sparse graphs.
2)long long type is used for distances to prevent overflow (since edge weights ≤ 1e9).
3)Fast I/O is recommended for large input sizes to avoid TLE.
4)No negative edge weights allowed (Dijkstra assumption).

✅ Checklist

Code Quality

  • Solution follows the required template format
  • Code is clean and well-commented
  • Variable names are descriptive
  • Proper error handling for edge cases

Testing

  • Solution passes all CSES test cases
  • Tested with custom edge cases
  • Handles large input constraints efficiently
  • No runtime errors or timeouts

Documentation

  • Added solution to appropriate category folder
  • File name follows naming convention (snake_case)
  • Added brief comment explaining the approach
  • Updated any relevant documentation if needed

Style Guide

  • Uses required headers (#include <bits/stdc++.h>)
  • Includes fast I/O optimization
  • Uses appropriate data types (long long for large numbers)
  • Follows competitive programming best practices

🏷️ Type of Change

  • 🐛 Bug fix (fixes an existing solution)
  • ✨ New problem solution
  • 📚 Documentation improvement
  • 🔧 Code optimization/refactoring
  • 🎃 Hacktoberfest contribution

🧪 Testing Details

To ensure the correctness, efficiency, and robustness of the Shortest Routes I implementation, I followed a structured multi-phase testing process:

  1. Basic Functional Testing
    I first validated the correctness of the algorithm using small, manually verifiable graphs:
    Simple 3-node graph to verify shortest path updates.
    Directed edges with asymmetric weights to ensure correct direction handling.
    Cases where multiple paths exist to confirm that Dijkstra always picks the shortest route.

  2. Edge Case Testing
    I covered edge scenarios to ensure stability:
    Single node (n=1, m=0): Expected output 0 (distance to itself).
    Disconnected nodes: Verified that unreachable nodes maintain large INF distances.
    Equal-weight edges: Confirmed algorithm correctly updates distances without bias.
    Multiple shortest paths: Ensured Dijkstra doesn’t produce duplicates and maintains minimal cost.

  3. Performance & Stress Testing
    To confirm the solution handles large constraints efficiently:
    Generated random large graphs with up to 2×10⁵ nodes and 5×10⁵ edges.
    Verified execution time remained within CSES time limits (~1s).
    Monitored memory usage to confirm it stayed under acceptable limits (≈300 MB).

  4. Cross-Validation
    Compared outputs with a known-correct Python Dijkstra implementation for small random graphs.
    Ensured outputs matched exactly for all tested cases.

  5. CSES Submission Verification
    Submitted final version to CSES online judge.
    Confirmed the solution passed all hidden test cases with Accepted (AC) verdict.
    Verified execution time and memory usage were well within limits.

Test Cases Used:

Input: 
3 4
1 2 6
1 3 2
3 2 3
1 3 4

Expected Output: 
0 5 2
Actual Output: 
0 5 2

📸 Screenshots (if applicable)

Problem statement:
Screenshot 2025-10-22 at 12 55 35 AM

Accepted Solution:

Screenshot 2025-10-22 at 12 56 13 AM

📎 Additional Notes

Fast I/O and priority_queue ensure the solution passes within time limits. The implementation follows a minimal-memory approach suited for large input graphs. Can be easily extended to reconstruct paths or handle multiple sources if needed.


For Maintainers:

  • Code review completed
  • Solution verified on CSES judge
  • Documentation updated if needed
  • Labels applied appropriately

@AnjaliPai16
Copy link
Contributor Author

@ks-iitjmu this is the pr for issue #117 kindly review it

@ks-iitjmu ks-iitjmu added enhancement New feature or request good first issue Good for newcomers hacktoberfest Issues/PRs for Hacktoberfest participation hacktoberfest-accepted Approved PRs for Hacktoberfest hacktoberfest_2025 Hacktoberfest 2025 specific contributions medium Medium difficulty problems category: graph Graph algorithm problems labels Oct 22, 2025
@ks-iitjmu ks-iitjmu linked an issue Oct 22, 2025 that may be closed by this pull request
9 tasks
@ks-iitjmu ks-iitjmu merged commit 6bab4be into ks-iitjmu:main Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: graph Graph algorithm problems enhancement New feature or request good first issue Good for newcomers hacktoberfest Issues/PRs for Hacktoberfest participation hacktoberfest_2025 Hacktoberfest 2025 specific contributions hacktoberfest-accepted Approved PRs for Hacktoberfest medium Medium difficulty problems

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NEW] Add solution for Shortest Route I

2 participants