#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
int main(){
const int inf = 1e9 + 5;
int n,m,k;
cin >> n >> m >> k;
vector<vector<int>> dist(n + 1,vector<int>(n + 1,inf));
vector<vector<pair<int,int>>> adj(n);
for(int i = 0;i < m;i++){
int u,v,w;
cin >> u >> v >> w;
u --;
v --;
adj[u].emplace_back(v,w);
adj[v].emplace_back(u,w);
}
vector<int> s(k);
for(int i = 0;i < k;i++){
cin >> s[i];
s[i] --;
}
sort(s.begin(),s.end());
multiset<int> ms;
for(int i = 0;i < k;i++){
int e = s[i];
dist[e][e] = 0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
pq.push({0,e});
while(!pq.empty()){
auto p = pq.top();
int u = p.second;
int tot = p.first;
pq.pop();
if(tot > dist[e][u]) continue;
for(auto pp : adj[u]){
int v = pp.first;
int w = pp.second;
if(dist[e][v] > tot + w){
dist[e][v] = tot + w;
pq.push({dist[e][v],v});
}
}
}
for(int j = i + 1;j < k;j++){
int e2 = s[j];
if(e == e2) continue;
ms.insert(dist[e][e2]);
}
}
int ans = inf;
for(int &s1 : s){
for(int &e : s){
if(e == s1) continue;
ms.erase(ms.find(dist[e][s1]));
}
for(int &f1 : s){
if(s1 == f1) continue;
for(int &e : s){
if(e == f1 || e == s1) continue;
ms.erase(ms.find(dist[f1][e]));
}
if(!ms.empty()){
ans = min(ans,*ms.begin() + dist[s1][f1]);
}
for(int &e : s){
if(e == f1 || e == s1) continue;
ms.insert(dist[f1][e]);
}
}
for(int &e : s){
if(e == s1) continue;
ms.insert(dist[s1][e]);
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |