根据数据范围选取spfa求其它所有点到该点的最小距离
#include<bits/stdc++.h>
using namespace std;
const int N = 810,M = 3000,INF = 0x3f3f3f3f;
int id[N];
int h[N],e[M],ne[M],w[M],idx;
bool st[N];
int dist[N];
int n,p,m;
void add(int a,int b,int c)
{
e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++;
}
int spfa(int start)
{
memset(st,0,sizeof st);
memset(dist,0x3f,sizeof dist);
dist[start] = 0;
queue<int> q;
q.push(start);
st[start] = true;
while(q.size())
{
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t];~i;i = ne[i])
{
int j = e[i];
if(dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if(!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
int res = 0;
for(int i = 0;i<n;i++)
{
int j = id[i];
if(dist[j] == INF)return INF;
else res += dist[j];
}
return res;
}
int main()
{
memset(h,-1,sizeof h);
cin>>n>>p>>m;
for(int i = 0;i<n;i++)cin>>id[i];
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c),add(b,a,c);
}
int res = INF;
for(int i = 1;i<=p;i++)res = min(res,spfa(i));
cout<<res<<endl;
return 0;
}