#C1157. Shortest Delivery Time
Shortest Delivery Time
Shortest Delivery Time
Luna runs a delivery service where she must deliver packages to several locations using directed roads between cities. The network is represented by a directed graph with (N) nodes and (M) edges. The first number in the list of points represents the starting location (S), and the remaining (Q) numbers represent the delivery destinations. Your task is to compute the sum of the shortest distances from (S) to each delivery destination. Formally, if (D) is the set of delivery destinations and (dist(S, i)) is the least travel time from (S) to (i), then the answer is given by
[ \sum_{i \in D} dist(S,i) ]
If any destination is unreachable from (S), output (-1).
inputFormat
The input is given via standard input (stdin). The first line contains three integers (N), (M), and (Q), where:
- (N) is the number of nodes.
- (M) is the number of directed roads.
- (Q) is the number of delivery destinations.
The second line contains (Q+1) integers: the first integer is the starting node (S) and the following (Q) integers correspond to the delivery destinations. The next (M) lines each contain three integers (u), (v), and (w) describing a directed road from node (u) to node (v) with a travel time of (w).
outputFormat
Print a single integer to standard output (stdout) representing the total minimum travel time to deliver all packages. If at least one destination is not reachable, output (-1).## sample
5 7 2
1 4 5
1 2 10
1 3 5
2 4 10
3 4 10
2 5 5
4 5 5
3 5 20
30