Dijkstra
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 3000;
const int M = INT_MAX / 2;
int n, p, m;
int h[N], e[N], ne[N], w[N], idx;
int dist[N], cnt[N];
bool st[N];
int ans = M * 2 - 1;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
void dijkstra(int pos)
{
for(int i = 1; i <= p; i ++)
dist[i] = M;
dist[pos] = 0;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, pos});
while(heap.size())
{
auto t = heap.top();
heap.pop();
int d = t.first, v = t.second;
if(st[v]) continue;
st[v] = true;
for(int i = h[v]; i != -1; i = ne[i])
{
int j = e[i];
if(dist[j] > d + w[i])
{
dist[j] = d + w[i];
heap.push({dist[j], j});
}
}
}
int sum = 0;
for(int i = 1; i <= p; i ++)
if(dist[i] == M) continue;
else sum += (cnt[i] * dist[i]);
if(sum || cnt[pos]) ans = min(ans, sum); //特判
}
int main()
{
cin >> n >> p >> m;
memset(h, -1, sizeof h);
for(int i = 0; i < n; i ++)
{
int x;
cin >> x;
cnt[x] ++;
}
while(m --)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
for(int i = 1; i <= p; i ++)
{
memset(st, 0, sizeof st);
dijkstra(i);
}
cout << ans << endl;
return 0;
}