Skip to content

Conversation

@AnjaliPai16
Copy link
Contributor

@AnjaliPai16 AnjaliPai16 commented Oct 28, 2025

🎯 Problem Information

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

📝 Description

This PR adds an optimized solution for the CSES Flight Routes problem.
The task is to compute the k shortest flight routes from city 1 Syrjälä to city n Metsälä in a directed weighted graph.
A route may visit the same city multiple times, and if multiple paths have the same total cost, they must each be considered separately.
The problem is a variation of Dijkstra’s algorithm, but we must track up to k distinct shortest paths per node.

🧩 Solution Approach

  • Algorithm Used: Modified Dijkstra’s Algorithm with Multi-Path Tracking
  • Core Idea:
  1. Maintain a min-heap (priority queue) to process nodes by their current shortest distance.
  2. Each node stores up to k shortest distances in a vector.
  3. When a new distance to a node is found:
  4. If that node has fewer than k distances stored, we push it.
  5. If it already has k distances and the new one is smaller than the largest (worst) stored distance, we replace it.
  6. Stop once we have collected k distances for the destination node (n).

Key Optimizations:

  1. Use vector for distance lists to handle large weights.
  2. Use priority_queue<pair<long long, int>, vector<...>, greater<...>> for min-heap.
  3. Skip processing when dist[node].size() == k and the new distance is larger than the worst stored one.
  4. Use fast I/O (ios::sync_with_stdio(false); cin.tie(NULL);).
  • Time Complexity: O(m * log(n * k))
  • Space Complexity: O(n * k)
  • Key Insights:
  1. Instead of tracking only the single best distance per node (as in standard Dijkstra), we maintain up to k shortest distances for each node.
  2. Each node keeps a max-heap of its shortest distances — ensuring insertion and pruning take only O(log k) time.
  3. A global min-heap (priority_queue) drives exploration in increasing order of total path cost.
    4)nSince k ≤ 10, this approach remains efficient even for n = 1e5 and m = 2e5.
  4. Using a max-heap per node avoids expensive sorting operations and fixes TLE issues.

✅ Checklist

Please ensure your PR meets these requirements:

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

Describe how you tested your solution:

  1. Ran the solution locally on CSES official input sets.
  2. Verified output correctness and performance.
  3. Cross-checked with edge cases for repeated nodes and cycles.

Test Cases Used:

Sample Test Case 1:

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

Expected Output:
4 4 7
Actual Output:
4 4 7

Custom Test Case 2:

Input:
3 4 2
1 2 2
1 3 5
2 3 2
2 1 1

Expected Output:
4 5
Actual Output:
4 5

📸 Screenshots (if applicable)

Screenshot 2025-10-28 at 11 39 18 AM Screenshot 2025-10-28 at 11 39 52 AM

📎 Additional Notes

Any additional information, considerations, or context for reviewers:

  1. The solution avoids sorting vectors in every iteration — drastically reducing time complexity.
  2. Uses a max-heap per node for fast pruning (O(log k) per update).
  3. This version is more stable and efficient than the naive vector-based version that caused TLE.

For Maintainers:

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

@AnjaliPai16
Copy link
Contributor Author

@ks-iitjmu PR for issue #138 kindly review

@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 28, 2025
@ks-iitjmu ks-iitjmu linked an issue Oct 28, 2025 that may be closed by this pull request
9 tasks
@ks-iitjmu ks-iitjmu merged commit 1ae92bf into ks-iitjmu:main Oct 28, 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 Flight Routes

2 participants