枚举一下每个牧场作为目标点就行了,模型同1375. 奶牛回家
const int N=810;
vector<PII> g[N];
int dist[N];
bool vis[N];
int pos[N];
int c,n,m;
void dijkstra(int s)
{
memset(dist,0x3f,sizeof dist);
memset(vis,0,sizeof vis);
priority_queue<PII,vector<PII>,greater<PII> > heap;
heap.push({0,s});
dist[s]=0;
while(heap.size())
{
int t=heap.top().second;
heap.pop();
if(vis[t]) continue;
vis[t]=true;
for(int i=0;i<g[t].size();i++)
{
int j=g[t][i].fi,w=g[t][i].se;
if(dist[j] > dist[t] + w)
{
dist[j]=dist[t]+w;
heap.push({dist[j],j});
}
}
}
}
int main()
{
cin>>c>>n>>m;
for(int i=1;i<=c;i++) cin>>pos[i];
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
g[a].pb({b,c});
g[b].pb({a,c});
}
int ans=INF;
for(int i=1;i<=n;i++)
{
dijkstra(i);
int res=0;
for(int j=1;j<=c;j++)
if(dist[pos[j]] == INF)
{
res=INF;
break;
}
else res+=dist[pos[j]];
ans=min(ans,res);
}
cout<<ans<<endl;
//system("pause");
}