#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()){
int u=pq.top().second; pq.pop();
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
5 ms |
5340 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 |
5028 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
6 ms |
5212 KB |
Output is correct |
8 |
Correct |
5 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5128 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
5 ms |
5212 KB |
Output is correct |
14 |
Correct |
4 ms |
5212 KB |
Output is correct |
15 |
Correct |
4 ms |
5164 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
4956 KB |
Output is correct |
19 |
Correct |
4 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5212 KB |
Output is correct |
21 |
Correct |
4 ms |
5156 KB |
Output is correct |
22 |
Correct |
1 ms |
4952 KB |
Output is correct |
23 |
Correct |
3 ms |
5256 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 |
2 ms |
4952 KB |
Output is correct |
28 |
Correct |
5 ms |
5212 KB |
Output is correct |
29 |
Correct |
4 ms |
5164 KB |
Output is correct |
30 |
Correct |
6 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
5 ms |
5340 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 |
5028 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
6 ms |
5212 KB |
Output is correct |
8 |
Correct |
5 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5128 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
5 ms |
5212 KB |
Output is correct |
14 |
Correct |
4 ms |
5212 KB |
Output is correct |
15 |
Correct |
4 ms |
5164 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
4956 KB |
Output is correct |
19 |
Correct |
4 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5212 KB |
Output is correct |
21 |
Correct |
4 ms |
5156 KB |
Output is correct |
22 |
Correct |
1 ms |
4952 KB |
Output is correct |
23 |
Correct |
3 ms |
5256 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 |
2 ms |
4952 KB |
Output is correct |
28 |
Correct |
5 ms |
5212 KB |
Output is correct |
29 |
Correct |
4 ms |
5164 KB |
Output is correct |
30 |
Correct |
6 ms |
5212 KB |
Output is correct |
31 |
Correct |
3 ms |
5208 KB |
Output is correct |
32 |
Correct |
8 ms |
5296 KB |
Output is correct |
33 |
Correct |
9 ms |
5208 KB |
Output is correct |
34 |
Correct |
2 ms |
4956 KB |
Output is correct |
35 |
Correct |
1 ms |
4956 KB |
Output is correct |
36 |
Correct |
75 ms |
9676 KB |
Output is correct |
37 |
Correct |
30 ms |
6868 KB |
Output is correct |
38 |
Correct |
16 ms |
5468 KB |
Output is correct |
39 |
Correct |
812 ms |
11304 KB |
Output is correct |
40 |
Correct |
221 ms |
10280 KB |
Output is correct |
41 |
Correct |
18 ms |
5464 KB |
Output is correct |
42 |
Correct |
177 ms |
10532 KB |
Output is correct |
43 |
Correct |
49 ms |
7880 KB |
Output is correct |
44 |
Correct |
15 ms |
5468 KB |
Output is correct |
45 |
Correct |
2 ms |
4956 KB |
Output is correct |
46 |
Correct |
870 ms |
14908 KB |
Output is correct |
47 |
Correct |
215 ms |
9960 KB |
Output is correct |
48 |
Correct |
776 ms |
11720 KB |
Output is correct |
49 |
Correct |
926 ms |
14308 KB |
Output is correct |
50 |
Correct |
15 ms |
5208 KB |
Output is correct |
51 |
Correct |
9 ms |
5468 KB |
Output is correct |
52 |
Correct |
12 ms |
5212 KB |
Output is correct |
53 |
Correct |
528 ms |
10060 KB |
Output is correct |
54 |
Correct |
857 ms |
11632 KB |
Output is correct |
55 |
Correct |
8 ms |
5208 KB |
Output is correct |
56 |
Correct |
2 ms |
5212 KB |
Output is correct |
57 |
Correct |
12 ms |
5384 KB |
Output is correct |
58 |
Correct |
1348 ms |
13544 KB |
Output is correct |
59 |
Correct |
1 ms |
4956 KB |
Output is correct |
60 |
Correct |
14 ms |
6148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6035 ms |
12668 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
5 ms |
5340 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 |
5028 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
6 ms |
5212 KB |
Output is correct |
8 |
Correct |
5 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5128 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
5 ms |
5212 KB |
Output is correct |
14 |
Correct |
4 ms |
5212 KB |
Output is correct |
15 |
Correct |
4 ms |
5164 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
4956 KB |
Output is correct |
19 |
Correct |
4 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5212 KB |
Output is correct |
21 |
Correct |
4 ms |
5156 KB |
Output is correct |
22 |
Correct |
1 ms |
4952 KB |
Output is correct |
23 |
Correct |
3 ms |
5256 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 |
2 ms |
4952 KB |
Output is correct |
28 |
Correct |
5 ms |
5212 KB |
Output is correct |
29 |
Correct |
4 ms |
5164 KB |
Output is correct |
30 |
Correct |
6 ms |
5212 KB |
Output is correct |
31 |
Correct |
3 ms |
5208 KB |
Output is correct |
32 |
Correct |
8 ms |
5296 KB |
Output is correct |
33 |
Correct |
9 ms |
5208 KB |
Output is correct |
34 |
Correct |
2 ms |
4956 KB |
Output is correct |
35 |
Correct |
1 ms |
4956 KB |
Output is correct |
36 |
Correct |
75 ms |
9676 KB |
Output is correct |
37 |
Correct |
30 ms |
6868 KB |
Output is correct |
38 |
Correct |
16 ms |
5468 KB |
Output is correct |
39 |
Correct |
812 ms |
11304 KB |
Output is correct |
40 |
Correct |
221 ms |
10280 KB |
Output is correct |
41 |
Correct |
18 ms |
5464 KB |
Output is correct |
42 |
Correct |
177 ms |
10532 KB |
Output is correct |
43 |
Correct |
49 ms |
7880 KB |
Output is correct |
44 |
Correct |
15 ms |
5468 KB |
Output is correct |
45 |
Correct |
2 ms |
4956 KB |
Output is correct |
46 |
Correct |
870 ms |
14908 KB |
Output is correct |
47 |
Correct |
215 ms |
9960 KB |
Output is correct |
48 |
Correct |
776 ms |
11720 KB |
Output is correct |
49 |
Correct |
926 ms |
14308 KB |
Output is correct |
50 |
Correct |
15 ms |
5208 KB |
Output is correct |
51 |
Correct |
9 ms |
5468 KB |
Output is correct |
52 |
Correct |
12 ms |
5212 KB |
Output is correct |
53 |
Correct |
528 ms |
10060 KB |
Output is correct |
54 |
Correct |
857 ms |
11632 KB |
Output is correct |
55 |
Correct |
8 ms |
5208 KB |
Output is correct |
56 |
Correct |
2 ms |
5212 KB |
Output is correct |
57 |
Correct |
12 ms |
5384 KB |
Output is correct |
58 |
Correct |
1348 ms |
13544 KB |
Output is correct |
59 |
Correct |
1 ms |
4956 KB |
Output is correct |
60 |
Correct |
14 ms |
6148 KB |
Output is correct |
61 |
Execution timed out |
6035 ms |
12668 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |