#P2935. Optimal Pasture Sleep Location

    ID: 16193 Type: Default 1000ms 256MiB

Optimal Pasture Sleep Location

Optimal Pasture Sleep Location

Bessie wants to minimize the average travel time from her sleeping pasture to all of her favorite pastures. Farmer John's farm is composed of P pastures (numbered 1 to P), connected by C bidirectional cowpaths. Bessie has F favorite pastures. Each cowpath connects two pastures and takes T units of time to traverse.

Formally, let d(x, y) be the shortest distance between pastures x and y. For a candidate pasture x, the average time to reach all favorites is given by:

\(\frac{\sum_{f \in Favorites} d(x, f)}{F}\)

Your task is to choose the pasture from which this average is minimized. In the event of a tie (i.e. multiple pastures yield the same average travel time), choose the one with the smallest numerical label.

Note: It is guaranteed that all pastures are connected (i.e. it is possible to reach any pasture from any other pasture).

inputFormat

Input Format:

  • The first line contains three integers: P, F, and C where  1 ≤ P ≤ 500, 1 ≤ FP, and 1 < C ≤ 8000.
  • The second line contains F integers representing the favorite pastures.
  • Each of the next C lines contains three integers: a, b, and T (with 1 ≤ T < 892), indicating a bidirectional cowpath connecting pastures a and b that takes T time units to traverse.

outputFormat

Output Format:

Print a single integer: the number of the pasture that minimizes the average travel time to all favorite pastures. If multiple pastures yield the same minimum average time, output the smallest numbered pasture.

sample

5 2 4
2 4
1 2 2
1 3 1
1 4 3
1 5 4
1