CSES - Flight Discount
Authors: Kai Wang, Stanley Zhong, Maggie Liu, Kevin Sheng
Solution 1
Say we use the discount coupon on the edge between cities A and B.
There are two cases: we 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 to city after applying the coupon to that flight, is the cost to travel from city to city and is the cost to travel from city to city .
Java
import java.io.*;import java.util.*;public class FlightDiscount {static class Flight {int to;long cost;Flight(int v, long wt) {this.to = v;this.cost = wt;
C++
#include <iostream>#include <queue>#include <vector>using std::cout;using std::endl;using std::pair;using std::vector;/**
Solution 2
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.
C++
#include <iostream>#include <queue>#include <vector>using std::cout;using std::endl;using std::vector;int main() {int city_num;
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!