This page is still under construction.

Parts of this page are still being built. What you see may change.

Best Spot

Interview

Time limit1sMemory limit128 MB

Summary
Given a weighted undirected graph and a set of favorite vertices, find the vertex whose average shortest-path distance to all favorites is smallest, breaking ties by smallest index.
Level

Medium5 of 10

Topics
Graph, Shortest path, Heap, Brute force
Solved
No attempts yet

Problem

Bessie, always looking to optimize her life, has realized that she especially enjoys visiting her FF favorite pastures F1,F2,…,FFF_1, F_2, \dots, F_F (1≤F≤P1 \le F \le P, 1≤Fi≤P1 \le F_i \le P) among the PP pastures (1≤P≤5001 \le P \le 500; conveniently numbered 11 through PP) that make up Farmer John's holdings.

The farm has CC bidirectional cowpaths (1≤C≤80001 \le C \le 8000; conveniently numbered 11 through CC) connecting various pastures, and using them Bessie can travel to any pasture on the farm. The ii-th cowpath connects the two endpoints aia_i and bib_i (1≤ai≤P1 \le a_i \le P, 1≤bi≤P1 \le b_i \le P) and takes time TiT_i (1≤Ti≤8921 \le T_i \le 892) to traverse in either direction.

Bessie wants to find the number of the best pasture to sleep in, so that when she wakes up the average time to travel to each of her FF favorite pastures is minimized.

The map below shows an example farm. A pasture whose number is marked with an asterisk * is one of Bessie's favorites, and the number in brackets [] is the time to traverse that cowpath.

            1*--[4]--2--[2]--3
                     |       |
                    [3]     [4]
                     |       |
                     4--[3]--5--[1]---6---[6]---7--[7]--8*
                     |       |        |         |
                    [3]     [2]      [1]       [3]
                     |       |        |         |
                    13*      9--[3]--10*--[1]--11*--[3]--12*

The following table shows, for each candidate "best pasture" 4,5,6,7,9,10,11,124, 5, 6, 7, 9, 10, 11, 12, the distance to every favorite pasture and the resulting average.

                       * * * * * * Favorites * * * * * *
 Potential      Pasture Pasture Pasture Pasture Pasture Pasture     Average
Best Pasture       1       8      10      11      12      13        Distance
------------      --      --      --      --      --      --      -----------
    4              7      16       5       6       9       3      46/6 = 7.67
    5             10      13       2       3       6       6      40/6 = 6.67
    6             11      12       1       2       5       7      38/6 = 6.33
    7             16       7       4       3       6      12      48/6 = 8.00
    9             12      14       3       4       7       8      48/6 = 8.00
   10             12      11       0       1       4       8      36/6 = 6.00 ** BEST
   11             13      10       1       0       3       9      36/6 = 6.00
   12             16      13       4       3       0      12      48/6 = 8.00

Assuming these candidates really are the best options (a program must check all pastures somehow), the best place to sleep is pasture 1010, which has the smallest average distance.

Input

  • Line 1: Three space-separated integers PP, FF, and CC.
  • Next FF lines: Each line contains a single integer FiF_i, the number of one of Bessie's favorite pastures.
  • Next CC lines: Each line describes one cowpath with three space-separated integers aia_i, bib_i, and TiT_i.

Output

  • Print a single integer on one line: the number of the best pasture to sleep in. If more than one pasture is best, print the smallest such number.

Examples3

  1. Example 1

    Input
    13 6 15
    11
    13
    10
    12
    8
    1
    2 4 3
    7 11 3
    10 11 1
    4 13 3
    9 10 3
    2 3 2
    3 5 4
    5 9 2
    6 7 6
    5 6 1
    1 2 4
    4 5 3
    11 12 3
    6 10 1
    7 8 7
    
    Expected output
    10
    
  2. Example 2

    Input
    2 1 1
    2
    1 2 5
    
    Expected output
    2
    
  3. Example 3

    Input
    2 2 1
    1
    2
    1 2 4
    
    Expected output
    1