堆优化dijkstra
/*
PROB:butter
LANG:C++
*/ #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int Ni = ;
const int INF = <<;
struct node{
int x,d;
node(){}
node(int a,int b){x=a;d=b;}
bool operator < (const node & a) const
{
if(d==a.d) return x<a.x;
else return d > a.d;
}
};
vector<node> eg[Ni];
int dis[Ni],cow[Ni],ans,p,c,n; void Dijkstra(int s)
{
int i;
for(i=;i<=p;i++) dis[i]=INF;
dis[s]=;
priority_queue<node> q;
q.push(node(s,dis[s]));
while(!q.empty())
{
node x=q.top();q.pop();
for(i=;i<eg[x.x].size();i++)
{
node y=eg[x.x][i];
if(dis[y.x]>x.d+y.d)
{
dis[y.x]=x.d+y.d;
q.push(node(y.x,dis[y.x]));
}
}
}
}
int main()
{
freopen("butter.in","r",stdin);
freopen("butter.out","w",stdout); ans=INF;
scanf("%d %d %d",&n,&p,&c);
for (int i=;i<=n;i++)
scanf("%d",&cow[i]);
for (int i=;i<=c;i++)
{
int x,y,t;
scanf("%d %d %d",&x,&y,&t);
//a[x][y]=t; a[y][x]=t;
eg[x].push_back(node(y,t));
eg[y].push_back(node(x,t));
} for (int k=;k<=p;k++)
{
Dijkstra(k); int tmp=;
for (int i=;i<=n;i++)
tmp+=dis[cow[i]];
if (tmp<ans)
ans=tmp;
}
printf("%d\n",ans); return ;
}