-
Notifications
You must be signed in to change notification settings - Fork 35
Closed
Labels
category: graphGraph algorithm problemsGraph algorithm problemscategory: mathematicsMathematical problemsMathematical problemsenhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomershacktoberfestIssues/PRs for Hacktoberfest participationIssues/PRs for Hacktoberfest participationhacktoberfest_2025Hacktoberfest 2025 specific contributionsHacktoberfest 2025 specific contributionshelp wantedExtra attention is neededExtra attention is needed
Description
🎯 Problem Information
- Problem Name: Flight Discount
- CSES Link: https://cses.fi/problemset/task/1195
- Category: Graphs / Shortest Path (Dijkstra’s Algorithm)
- Estimated Difficulty: Medium
📋 Problem Description
You are given a directed weighted graph with n cities (nodes) and m flights (edges).
- You must find the minimum total cost to travel from city 1 (Syrjälä) to city n (Metsälä).
- You have one discount coupon, which can be used on exactly one flight to halve its cost (using floor division, i.e., ⌊x/2⌋).
- You can use the coupon only once and on any flight in your route.
The task is to determine the minimum possible total flight cost considering the optimal place to use the coupon.
💡 Proposed Approach
We'll use Dijkstra’s algorithm with a state-space expansion approach:
- Each node can be reached in two states:
- Without using the discount yet
- After using the discount
Algorithm Steps:
- Build an adjacency list for all directed edges
(a, b, c). - Maintain a
dist[node][state]array where:state = 0: Discount not usedstate = 1: Discount already used
- Use a priority queue (min-heap) for Dijkstra traversal:
- For each edge
(u, v, w):- If discount not used yet, we have two choices:
- Travel normally:
dist[v][0] = dist[u][0] + w - Use discount now:
dist[v][1] = dist[u][0] + w // 2
- Travel normally:
- If discount already used:
- Only travel normally:
dist[v][1] = dist[u][1] + w
- Only travel normally:
- If discount not used yet, we have two choices:
- For each edge
- Continue until all reachable nodes are processed.
- The answer will be
dist[n][1], i.e., the minimum distance to citynwith discount used optimally.
Time Complexity:
- O((n + m) log n) using Dijkstra’s algorithm (each edge processed twice due to two states).
Space Complexity:
- O(n + m) for adjacency list + O(2n) for distance states.
📝 Implementation Notes
- Requires fast I/O
- Needs modular arithmetic
- Large input constraints
- Edge cases to consider
🏷️ Labels
-
hacktoberfest -
good first issue -
help wanted
👤 Assignee
- I would like to work on this issue
- I'm available for guidance/review
📎 Additional Context
Metadata
Metadata
Assignees
Labels
category: graphGraph algorithm problemsGraph algorithm problemscategory: mathematicsMathematical problemsMathematical problemsenhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomershacktoberfestIssues/PRs for Hacktoberfest participationIssues/PRs for Hacktoberfest participationhacktoberfest_2025Hacktoberfest 2025 specific contributionsHacktoberfest 2025 specific contributionshelp wantedExtra attention is neededExtra attention is needed