CSES - Flight Discount

Authors: Kai Wang, Stanley Zhong

Solution 1 by Kai Wang

Say I use the discount coupon on the edge between cities A and B.

There are two cases: I can go from , or . We need to know the distance between and , and and .

We can use Dijkstra's to compute the distance from and to every vertex. Then our answer is , where is the cost to travel from city A to city B after applying the coupon to that flight.

1import java.io.*;
2import java.util.*;
3
4public class FlightDiscount {
5
6 /**
7 * Author : Kai Wang
8 */
9 static class Pair implements Comparable<Pair>{
10 int v; long w;

Solution 2 by Stanley Zhong

Alternatively, we can run Dijkstra's and modify our distance array slightly to track whether the discount has been used or not.

will represent the shortest distance from the start node to node , without using the discount. will represent the shortest distance after using the discount.

1// Author: Stanley Zhong
2#include <bits/stdc++.h>
3using namespace std;
4
5using ll = long long;
6using pll = pair<ll,ll>;
7#define f first
8#define s second
9
10// create a struct to store data in a priority_queue

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

Give Us Feedback on CSES - Flight Discount!