CSES - Flight Discount

Author: Kai Wang

Table of Contents


Edit on Github

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

There are two cases: I can go from 1ABN1\rightarrow A\rightarrow B\rightarrow N, or 1BAN1\rightarrow B\rightarrow A\rightarrow N. We need to know the distance between 11 and AA, and NN and BB.

We can use Dijkstra's to compute the distance from 11 and NN to every vertex. Then our answer is minABdist1[A]+c(A,B)+distN[B]\text{min}_{A\rightarrow B} dist1[A]+c(A,B)+distN[B], where c(A,B)c(A,B) is the cost to travel from city A to city B after applying the coupon to that flight.

import java.io.*;
import java.util.*;
public class FlightDiscount {
/**
* Author : Kai Wang
*/
static class Pair implements Comparable<Pair>{
int v; long w;

Give Us Feedback on CSES - Flight Discount!