#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(a) (a).begin(), (a).end()
const int N=1e5+1, MAX_SIZE=6;
int n, m, k;
vector<pair<int, int>> dist[N];
vector<pair<int, int>> g[N];
bool optimize(int idx, pair<int, int> val){
for (auto &i:dist[idx]) if (i.second==val.second){
if (i.first>val.first){
i=val;
return 1;
}
return 0;
}
if ((int)dist[idx].size()<MAX_SIZE){
dist[idx].push_back(val);
sort(dist[idx].begin(), dist[idx].end());
return 1;
}
if (val.first<dist[idx].back().first){
dist[idx].push_back(val);
sort(dist[idx].begin(), dist[idx].end());
dist[idx].pop_back();
return 1;
}
return 0;
}
bool optimize(int idx, int par, int w){
bool ans=0;
for (auto &i:dist[par]){
ans|=optimize(idx, {i.first+w, i.second});
}
return ans;
}
struct cmp{
bool operator()(const pair<vector<pair<int, int>>, int> &a, const pair<vector<pair<int, int>>, int> &b){
for (int i=0; i<min<int>(a.first.size(), b.first.size()); ++i) if (a.first[i]!=b.first[i]) return a.first[i]<b.first[i];
if (a.first.size()!=b.first.size()) return a.first.size()<b.first.size();
return a.second<b.second;
}
};
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);
}
priority_queue<pair<vector<pair<int, int>>, int>, vector<pair<vector<pair<int, int>>, int>>, cmp> pq;
for (int i=1; i<=k; ++i){
int x; cin >> x;
dist[x].emplace_back(0, x);
pq.emplace(dist[x], x);
}
while (pq.size()){
auto tt=pq.top().first;
int u=pq.top().second; pq.pop();
if (dist[u]!=tt) continue;
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (optimize(v, u, w)) pq.emplace(dist[v], v);
}
}
int ans=1e18;
set<pair<int, pair<int, int>>> st;
for (int i=1; i<=n; ++i){
vector<pair<int, int>> d2;
for (auto &j:dist[i]){
int u=i, v=j.second, d=j.first;
if (d && find(all(dist[v]), pair<int, int>{d, u})!=dist[v].end()){
st.insert({d, {u, v}});
st.insert({d, {v, u}});
d2.emplace_back(d, v);
}
}
dist[i].swap(d2);
}
for (int i=1; i<=n; ++i){
int u=i;
for (auto &j:dist[i]){
int v=j.second;
for (auto &t:dist[u]){
st.erase({t.first, {u, t.second}});
st.erase({t.first, {t.second, u}});
}
for (auto &t:dist[v]){
st.erase({t.first, {v, t.second}});
st.erase({t.first, {t.second, v}});
}
if (st.size()){
ans=min(ans, j.first+st.begin()->first);
}
for (auto &t:dist[u]){
st.insert({t.first, {u, t.second}});
st.insert({t.first, {t.second, u}});
}
for (auto &t:dist[v]){
st.insert({t.first, {v, t.second}});
st.insert({t.first, {t.second, v}});
}
}
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5112 KB |
Output is correct |
2 |
Correct |
4 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
4 ms |
5212 KB |
Output is correct |
8 |
Correct |
4 ms |
5292 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4952 KB |
Output is correct |
11 |
Correct |
4 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
4 ms |
5208 KB |
Output is correct |
15 |
Correct |
4 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
4 ms |
5268 KB |
Output is correct |
18 |
Correct |
1 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
5120 KB |
Output is correct |
20 |
Correct |
2 ms |
5212 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
1 ms |
5036 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
4956 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
2 ms |
4956 KB |
Output is correct |
27 |
Correct |
1 ms |
4952 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5312 KB |
Output is correct |
30 |
Correct |
4 ms |
5324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5112 KB |
Output is correct |
2 |
Correct |
4 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
4 ms |
5212 KB |
Output is correct |
8 |
Correct |
4 ms |
5292 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4952 KB |
Output is correct |
11 |
Correct |
4 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
4 ms |
5208 KB |
Output is correct |
15 |
Correct |
4 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
4 ms |
5268 KB |
Output is correct |
18 |
Correct |
1 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
5120 KB |
Output is correct |
20 |
Correct |
2 ms |
5212 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
1 ms |
5036 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
4956 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
2 ms |
4956 KB |
Output is correct |
27 |
Correct |
1 ms |
4952 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5312 KB |
Output is correct |
30 |
Correct |
4 ms |
5324 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
8 ms |
5272 KB |
Output is correct |
33 |
Correct |
11 ms |
5220 KB |
Output is correct |
34 |
Correct |
2 ms |
4956 KB |
Output is correct |
35 |
Correct |
1 ms |
4956 KB |
Output is correct |
36 |
Correct |
47 ms |
9424 KB |
Output is correct |
37 |
Correct |
18 ms |
6616 KB |
Output is correct |
38 |
Correct |
17 ms |
5424 KB |
Output is correct |
39 |
Correct |
502 ms |
10324 KB |
Output is correct |
40 |
Correct |
99 ms |
10016 KB |
Output is correct |
41 |
Correct |
18 ms |
5464 KB |
Output is correct |
42 |
Correct |
70 ms |
10288 KB |
Output is correct |
43 |
Correct |
33 ms |
7844 KB |
Output is correct |
44 |
Correct |
15 ms |
5212 KB |
Output is correct |
45 |
Correct |
1 ms |
4956 KB |
Output is correct |
46 |
Correct |
223 ms |
13652 KB |
Output is correct |
47 |
Correct |
87 ms |
9960 KB |
Output is correct |
48 |
Correct |
455 ms |
10688 KB |
Output is correct |
49 |
Correct |
343 ms |
13356 KB |
Output is correct |
50 |
Correct |
12 ms |
5212 KB |
Output is correct |
51 |
Correct |
7 ms |
5464 KB |
Output is correct |
52 |
Correct |
12 ms |
5212 KB |
Output is correct |
53 |
Correct |
331 ms |
9396 KB |
Output is correct |
54 |
Correct |
524 ms |
10444 KB |
Output is correct |
55 |
Correct |
9 ms |
5208 KB |
Output is correct |
56 |
Correct |
3 ms |
5364 KB |
Output is correct |
57 |
Correct |
16 ms |
5212 KB |
Output is correct |
58 |
Correct |
595 ms |
11888 KB |
Output is correct |
59 |
Correct |
1 ms |
4952 KB |
Output is correct |
60 |
Correct |
12 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
6070 ms |
11404 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5112 KB |
Output is correct |
2 |
Correct |
4 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
4 ms |
5212 KB |
Output is correct |
8 |
Correct |
4 ms |
5292 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4952 KB |
Output is correct |
11 |
Correct |
4 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
4 ms |
5208 KB |
Output is correct |
15 |
Correct |
4 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
4 ms |
5268 KB |
Output is correct |
18 |
Correct |
1 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
5120 KB |
Output is correct |
20 |
Correct |
2 ms |
5212 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
1 ms |
5036 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
4956 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
2 ms |
4956 KB |
Output is correct |
27 |
Correct |
1 ms |
4952 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5312 KB |
Output is correct |
30 |
Correct |
4 ms |
5324 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
8 ms |
5272 KB |
Output is correct |
33 |
Correct |
11 ms |
5220 KB |
Output is correct |
34 |
Correct |
2 ms |
4956 KB |
Output is correct |
35 |
Correct |
1 ms |
4956 KB |
Output is correct |
36 |
Correct |
47 ms |
9424 KB |
Output is correct |
37 |
Correct |
18 ms |
6616 KB |
Output is correct |
38 |
Correct |
17 ms |
5424 KB |
Output is correct |
39 |
Correct |
502 ms |
10324 KB |
Output is correct |
40 |
Correct |
99 ms |
10016 KB |
Output is correct |
41 |
Correct |
18 ms |
5464 KB |
Output is correct |
42 |
Correct |
70 ms |
10288 KB |
Output is correct |
43 |
Correct |
33 ms |
7844 KB |
Output is correct |
44 |
Correct |
15 ms |
5212 KB |
Output is correct |
45 |
Correct |
1 ms |
4956 KB |
Output is correct |
46 |
Correct |
223 ms |
13652 KB |
Output is correct |
47 |
Correct |
87 ms |
9960 KB |
Output is correct |
48 |
Correct |
455 ms |
10688 KB |
Output is correct |
49 |
Correct |
343 ms |
13356 KB |
Output is correct |
50 |
Correct |
12 ms |
5212 KB |
Output is correct |
51 |
Correct |
7 ms |
5464 KB |
Output is correct |
52 |
Correct |
12 ms |
5212 KB |
Output is correct |
53 |
Correct |
331 ms |
9396 KB |
Output is correct |
54 |
Correct |
524 ms |
10444 KB |
Output is correct |
55 |
Correct |
9 ms |
5208 KB |
Output is correct |
56 |
Correct |
3 ms |
5364 KB |
Output is correct |
57 |
Correct |
16 ms |
5212 KB |
Output is correct |
58 |
Correct |
595 ms |
11888 KB |
Output is correct |
59 |
Correct |
1 ms |
4952 KB |
Output is correct |
60 |
Correct |
12 ms |
5980 KB |
Output is correct |
61 |
Execution timed out |
6070 ms |
11404 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |