#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(a) (a).begin(), (a).end()
const int N=1e5+1;
int n, m, k, dd[N];
int dist[N], par[N], d1[N], d2[N];
vector<pair<int, int>> g[N];
pair<pair<int, int>, pair<int, int>> dijkstra(int root, int d[]){
memset(d, 0, sizeof(int)*N);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
d[root]=0;
pq.emplace(d[root], root);
pair<pair<int, int>, pair<int, int>> ans={{1e18, -1}, {1e18, -1}};
while (pq.size()){
int u=pq.top().second, f=pq.top().first;
pq.pop();
if (d[u]!=f) continue;
if (dd[u]){
if (ans.first.first>dist[u]){
ans.second=ans.first;
ans.first={dist[u], u};
}else ans.second=min(ans.second, {dist[u], u});
}
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (d[v]>d[u]+w){
d[v]=d[u]+w;
pq.emplace(d[v], v);
}
}
}
return ans;
}
pair<int, pair<int, int>> dijkstra(vector<int> root){
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
memset(dist, 0x3f, sizeof dist);
for (int x:root){
dist[x]=0; par[x]=x;
pq.emplace(dist[x], x);
}
pair<int, pair<int, int>> best={-1, {-1, -1}};
while (pq.size()){
int u=pq.top().second, d=pq.top().first; pq.pop();
if (dist[u]!=d) continue;
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (par[u]!=par[v]){
if (best.first==-1) best={dist[u]+dist[v]+w, {par[u], par[v]}};
else best=min(best, {dist[u]+dist[v]+w, {par[u], par[v]}});
}
if (dist[v]>dist[u]+w){
dist[v]=dist[u]+w;
par[v]=par[u];
pq.emplace(dist[v], v);
}
}
}
return best;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
for (int i=1; i<=m; ++i){
int u, v, w; cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
vector<int> v(k);
for (int &i:v) cin >> i, dd[i]=1;
auto best1=dijkstra(v);
int t1=best1.second.first, t2=best1.second.second;
v.erase(find(all(v), t1));
v.erase(find(all(v), t2));
dd[t1]=dd[t2]=0;
auto g1=dijkstra(t1, d1);
auto g2=dijkstra(t2, d2);
auto best2=dijkstra(v);
int ans=best1.first+best2.first;
if (g1.first.second!=-1 && g2.first.second!=-1 && g1.first.second!=g2.first.second){
ans=min(ans, g1.first.first+g2.first.first);
}
if (g1.first.second!=-1 && g2.second.second!=-1 && g1.first.second!=g2.second.second){
ans=min(ans, g1.first.first+g2.second.first);
}
if (g1.second.second!=-1 && g2.first.second!=-1 && g1.second.second!=g2.first.second){
ans=min(ans, g1.second.first+g2.first.first);
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
5468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
5468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
10000 KB |
Output is correct |
2 |
Correct |
3 ms |
5980 KB |
Output is correct |
3 |
Correct |
996 ms |
124864 KB |
Output is correct |
4 |
Correct |
445 ms |
65952 KB |
Output is correct |
5 |
Correct |
102 ms |
16592 KB |
Output is correct |
6 |
Correct |
81 ms |
13948 KB |
Output is correct |
7 |
Correct |
115 ms |
17784 KB |
Output is correct |
8 |
Correct |
37 ms |
10064 KB |
Output is correct |
9 |
Correct |
89 ms |
13516 KB |
Output is correct |
10 |
Correct |
59 ms |
11100 KB |
Output is correct |
11 |
Correct |
1068 ms |
132268 KB |
Output is correct |
12 |
Correct |
55 ms |
11596 KB |
Output is correct |
13 |
Correct |
315 ms |
40648 KB |
Output is correct |
14 |
Correct |
119 ms |
18924 KB |
Output is correct |
15 |
Correct |
1006 ms |
130272 KB |
Output is correct |
16 |
Correct |
26 ms |
9308 KB |
Output is correct |
17 |
Correct |
730 ms |
98404 KB |
Output is correct |
18 |
Correct |
4 ms |
6236 KB |
Output is correct |
19 |
Correct |
1211 ms |
141720 KB |
Output is correct |
20 |
Correct |
133 ms |
18092 KB |
Output is correct |
21 |
Correct |
95 ms |
15712 KB |
Output is correct |
22 |
Correct |
50 ms |
11048 KB |
Output is correct |
23 |
Correct |
13 ms |
8540 KB |
Output is correct |
24 |
Correct |
726 ms |
107352 KB |
Output is correct |
25 |
Correct |
75 ms |
13364 KB |
Output is correct |
26 |
Correct |
37 ms |
10076 KB |
Output is correct |
27 |
Correct |
61 ms |
12232 KB |
Output is correct |
28 |
Correct |
8 ms |
7256 KB |
Output is correct |
29 |
Correct |
135 ms |
17796 KB |
Output is correct |
30 |
Correct |
233 ms |
32468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
5468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |