#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define int long long
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=2005;
const int INF=1e18;
int n,k,m;
vector<int> s;
vector<pair<int,int>> adj[N];
vector dis(5,vector<int>(N,INF));
vector dis1(N,vector<int>(N,INF));
void dijk(int s,vector<int> &dis){
dis[s]=0;
priority_queue<pair<int,int>> pq;
pq.push({0,s});
while(pq.size()){
auto [dis1,a]=pq.top();
pq.pop();
dis1=-dis1;
if(dis1!=dis[a])continue;
for(auto [c,d]:adj[a]){
int cost=dis1+d;
if(dis[c]>cost){
dis[c]=cost;
pq.push({-dis[c],c});
}
}
}
}
int32_t main() {
LCBorz;
cin>>n>>k>>m;
for(int i=0;i<k;i++){
int p;cin>>p;
s.push_back(p);
}
for(int i=0;i<m;i++){
int a,b,c;cin>>a>>b>>c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
for(int i=0;i<k;i++){
dijk(s[i],dis[i]);
}
for(int i=1;i<=n;i++){
dijk(i,dis1[i]);
}
int ans=INF;
for(int i=1;i<=n;i++){
int cost=0;
for(int j=0;j<k;j++){
cost+=dis[j][i];
}
ans=min(ans,cost);
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
int cost=dis1[i][j];
for(int p=0;p<k;p++){
cost+=min(dis[p][i],dis[p][j]);
}
ans=min(ans,cost);
}
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
32088 KB |
Output is correct |
2 |
Correct |
14 ms |
32088 KB |
Output is correct |
3 |
Correct |
15 ms |
32088 KB |
Output is correct |
4 |
Correct |
15 ms |
32092 KB |
Output is correct |
5 |
Incorrect |
15 ms |
32092 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
38 ms |
64592 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
32232 KB |
Output is correct |
2 |
Correct |
144 ms |
32272 KB |
Output is correct |
3 |
Correct |
51 ms |
32088 KB |
Output is correct |
4 |
Correct |
58 ms |
32276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
39 ms |
64596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
39 ms |
64596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |