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 .
#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.
import java.io.*;import java.util.*;public class FlightDiscount {static ArrayList<int[]>[] adj;// dist[i][0] = shortest distance to node i without using discount// dist[i][1] = shortest distance to node i after using discountstatic long[][] dist;static final long INF = (long)1e18;
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!