#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
struct datax{
ll u, v, w;
};
ll n, m, k, cities[100005], ans, t, u, v, w, dist[100005], h[100005], a, b, spe[100005];
ll flat[100005];
vector<ii> adj[100005];
datax queries[400005];
ii dijkstra(ll s){
priority_queue<ii, vector<ii>, greater<ii>> pq;
for(int i=1; i<=n; i++) dist[i] = 1e9;
dist[s]=0;
pq.push({0, s});
while(!pq.empty()){
ll val = pq.top().fi;
ll u = pq.top().se;
pq.pop();
if(dist[u]!=val) continue;
if(spe[u]){
return {dist[u], u};
}
for(auto edge:adj[u]){
ll v = edge.fi;
ll w = edge.se;
if(dist[v]>dist[u]+w){
dist[v] = dist[u]+w;
pq.push({dist[v], v});
}
}
}
return {1e9, 0};
}
void dijkstra2(){
priority_queue<ii, vector<ii>, greater<ii>> pq;
for(int i=1; i<=n; i++) dist[i] = 1e9;
for(int i=1; i<=n; i++){
if(!spe[i]) continue;
dist[i]=0;
pq.push({0, i});
h[i] = i;
}
while(!pq.empty()){
ll val = pq.top().fi;
ll u = pq.top().se;
pq.pop();
if(dist[u]!=val) continue;
for(auto edge:adj[u]){
ll v = edge.fi;
ll w = edge.se;
if(dist[v]==dist[u]+w) flat[v] = 1;
if(dist[v]>dist[u]+w){
dist[v] = dist[u]+w;
h[v] = h[u];
flat[v]=0;
pq.push({dist[v], v});
}
}
}
}
void inp(){
cin >> n >> m >> k;
for(int i=1; i<=m; i++){
cin >> u >> v >> w;
queries[i] = {u, v, w};
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for(int i=1; i<=k; i++){
cin >> cities[i];
spe[cities[i]]=1;
}
}
pair<ll, ii> choose(){
ll ans=LLONG_MAX, a=0, b=0;
for(int i=1; i<=m; i++){
ll u = queries[i].u, v = queries[i].v, w = queries[i].w;
if(flat[u]||flat[v]||h[u]!=h[v]){
ll t = dist[u] + dist[v] + w;
if(t<ans){
ans = t;
a = h[u];
b = h[v];
}
//cout << dist[u] << ' ' << dist[v] << '\n';
//cout << flat[u] << ' ' <<flat[v] << '\n';
}
}
return {ans, {a, b}};
}
ll nearest(ll a, ll b){
ii tmp1 = dijkstra(a);
ll c = tmp1.se;
ll t3 = tmp1.fi;
spe[c]=0;
ii tmp2 = dijkstra(b);
ll d = tmp2.se;
ll t4 = tmp2.fi;
spe[c]=1;
return t3+t4;
}
void solve(){
dijkstra2();
pair<ll, ii> tmp = choose();
ll t1 = tmp.fi;
ll a = tmp.se.fi;
ll b = tmp.se.se;
spe[a]=0, spe[b]=0;
dijkstra2();
ll t2 = choose().fi;
ll ans = t1+t2;
ans = min({ans, nearest(a, b), nearest(b, a)});
cout << ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
//freopen("cities.inp", "r", stdin);
//freopen("cities.out", "w", stdout);
inp();
solve();
return 0;
}
Compilation message
RelayMarathon.cpp: In function 'long long int nearest(long long int, long long int)':
RelayMarathon.cpp:114:5: warning: unused variable 'd' [-Wunused-variable]
114 | ll d = tmp2.se;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6720 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6488 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6720 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6488 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
2 ms |
6492 KB |
Output is correct |
34 |
Correct |
2 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
2 ms |
6748 KB |
Output is correct |
37 |
Correct |
3 ms |
7000 KB |
Output is correct |
38 |
Correct |
2 ms |
6492 KB |
Output is correct |
39 |
Correct |
18 ms |
13304 KB |
Output is correct |
40 |
Correct |
6 ms |
9820 KB |
Output is correct |
41 |
Correct |
2 ms |
6492 KB |
Output is correct |
42 |
Correct |
5 ms |
9820 KB |
Output is correct |
43 |
Correct |
2 ms |
6748 KB |
Output is correct |
44 |
Correct |
1 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
21 ms |
15452 KB |
Output is correct |
47 |
Correct |
4 ms |
9308 KB |
Output is correct |
48 |
Correct |
17 ms |
13180 KB |
Output is correct |
49 |
Correct |
18 ms |
13392 KB |
Output is correct |
50 |
Correct |
1 ms |
6492 KB |
Output is correct |
51 |
Correct |
2 ms |
6492 KB |
Output is correct |
52 |
Correct |
1 ms |
6492 KB |
Output is correct |
53 |
Correct |
10 ms |
11100 KB |
Output is correct |
54 |
Correct |
19 ms |
13404 KB |
Output is correct |
55 |
Correct |
1 ms |
6492 KB |
Output is correct |
56 |
Correct |
1 ms |
6492 KB |
Output is correct |
57 |
Correct |
2 ms |
6492 KB |
Output is correct |
58 |
Correct |
23 ms |
15348 KB |
Output is correct |
59 |
Correct |
1 ms |
6492 KB |
Output is correct |
60 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
13072 KB |
Output is correct |
2 |
Correct |
4 ms |
9564 KB |
Output is correct |
3 |
Runtime error |
178 ms |
68872 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6720 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6488 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
2 ms |
6492 KB |
Output is correct |
34 |
Correct |
2 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
2 ms |
6748 KB |
Output is correct |
37 |
Correct |
3 ms |
7000 KB |
Output is correct |
38 |
Correct |
2 ms |
6492 KB |
Output is correct |
39 |
Correct |
18 ms |
13304 KB |
Output is correct |
40 |
Correct |
6 ms |
9820 KB |
Output is correct |
41 |
Correct |
2 ms |
6492 KB |
Output is correct |
42 |
Correct |
5 ms |
9820 KB |
Output is correct |
43 |
Correct |
2 ms |
6748 KB |
Output is correct |
44 |
Correct |
1 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
21 ms |
15452 KB |
Output is correct |
47 |
Correct |
4 ms |
9308 KB |
Output is correct |
48 |
Correct |
17 ms |
13180 KB |
Output is correct |
49 |
Correct |
18 ms |
13392 KB |
Output is correct |
50 |
Correct |
1 ms |
6492 KB |
Output is correct |
51 |
Correct |
2 ms |
6492 KB |
Output is correct |
52 |
Correct |
1 ms |
6492 KB |
Output is correct |
53 |
Correct |
10 ms |
11100 KB |
Output is correct |
54 |
Correct |
19 ms |
13404 KB |
Output is correct |
55 |
Correct |
1 ms |
6492 KB |
Output is correct |
56 |
Correct |
1 ms |
6492 KB |
Output is correct |
57 |
Correct |
2 ms |
6492 KB |
Output is correct |
58 |
Correct |
23 ms |
15348 KB |
Output is correct |
59 |
Correct |
1 ms |
6492 KB |
Output is correct |
60 |
Correct |
2 ms |
6748 KB |
Output is correct |
61 |
Correct |
37 ms |
13072 KB |
Output is correct |
62 |
Correct |
4 ms |
9564 KB |
Output is correct |
63 |
Runtime error |
178 ms |
68872 KB |
Execution killed with signal 11 |
64 |
Halted |
0 ms |
0 KB |
- |