Skip to content

Conversation

@vingoel26
Copy link
Contributor

🎯 Problem Information

#139
Problem Name: Flight Discount
Category: Graphs / Shortest Path (Dijkstra’s Algorithm)
CSES Link: https://cses.fi/problemset/task/1195
Difficulty: Medium


📝 Description

This PR adds the solution for the Flight Discount problem from the CSES Problem Set.
The goal is to find the minimum-cost route from city 1 (Syrjälä) to city n (Metsälä) while optimally using one discount coupon that halves the price of a single flight.
The solution uses a state-based Dijkstra’s algorithm to efficiently handle both the “used” and “unused” discount states for each node.


🧩 Solution Approach

  • Algorithm Used: Modified Dijkstra’s Algorithm with 2D state tracking
  • Time Complexity: O((n + m) * log n)
  • Space Complexity: O(n + m)
  • Key Insights:
    • Maintain two distance arrays for each city:
      • dist[node][0]: Minimum distance without using the discount
      • dist[node][1]: Minimum distance after using the discount
    • Use a min-heap priority queue for efficient shortest path extraction.
    • Process each edge with and without discount consideration.
    • The final result is dist[n][1], representing the minimum cost after discount usage.

✅ 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
  • Added brief comment explaining the approach
  • Updated 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

The solution was tested on both CSES and local environments.

Test Cases Used:
Input:
3 4
1 2 3
2 3 1
1 3 7
2 1 5

Expected Output:
2

Actual Output:
2

Additional tests:

  • Verified for large graphs with up to 10⁵ nodes and 2×10⁵ edges.
  • Checked correctness when the discount is best applied mid-route, not at the beginning or end.
  • Tested with multiple paths having similar costs.

📸 Screenshots

image

📎 Additional Notes

  • Implemented with efficient priority queue and state management for discount usage.
  • Carefully handles large weights (1 ≤ c ≤ 10⁹).
  • Floor division ensures integer correctness in halved cost computation.

For Maintainers:

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

@ks-iitjmu ks-iitjmu added enhancement New feature or request help wanted Extra attention is needed 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 27, 2025
@ks-iitjmu ks-iitjmu linked an issue Oct 27, 2025 that may be closed by this pull request
9 tasks
@ks-iitjmu ks-iitjmu merged commit 1bd44a5 into ks-iitjmu:main Oct 27, 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 help wanted Extra attention is needed medium Medium difficulty problems

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NEW] Add solution for Flight Discount

2 participants