#include <bits/stdc++.h>
using namespace std;
#define F first
#define int long long
#define S second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
int n , m , k , d[100005] , pai[100005] , peso[100005] , fn[100005] , sparse[100005][22] , custo[100005] , anc[100005][22] , h[100005];
vector<pii> adj[100005];
struct edges{
int x , y, z;
};
bool cmp(edges a , edges b){
return a.z > b.z;
}
void pre(int i , int j){
if(i == j) h[i] = 0;
for(int y = 0 ; y < adj[i].size();y++){
pii v = adj[i][y];
if(v.S == j) continue;
fn[v.S] = i;
custo[v.S] = v.F;
h[v.S] = h[i] + 1;
pre(v.S , i);
}
}
int fd(int x){ return pai[x] == x ? x : pai[x] = fd(pai[x]);}
void join(int u , int v){
u = fd(u) , v = fd(v);
if(peso[u] > peso[v]) swap(u,v);
pai[u] = v, peso[v] += peso[u] + 1;
}
int query(int x , int y){
if(h[x] < h[y]) swap(x,y);
int ansj = (int) 1e18;
for(int i = 21 ; i >= 0 ; i--){
if(h[x] >= h[y] + (1LL<<i)){
ansj = min(ansj , sparse[x][i]);
x = anc[x][i];
}
}
if(x == y) return ansj;
for(int i = 21 ; i >= 0 ; i--){
if(anc[x][i] != anc[y][i] && anc[x][i] != -1){
ansj = min(ansj , sparse[x][i]);
ansj = min(ansj , sparse[y][i]);
x = anc[x][i] , y = anc[y][i];
}
}
ansj = min(ansj , sparse[x][0]);
ansj = min(ansj , sparse[y][0]);
return ansj;
}
int32_t main(){
scanf("%lld%lld" , &n, &m);
vector<edges> v;
for(int i = 0 ; i <n;i++){
pai[i] = i , peso[i] = 0 , d[i] = (int) 1e18;
}
for(int i = 0 ; i < m ; i++){
int x,y,z;
scanf("%lld%lld%lld" , &x , &y , &z);
x--, y--;
v.pb({x,y,z});
adj[x].pb(pii(z,y));
adj[y].pb(pii(z,x));
}
scanf("%lld" , &k);
priority_queue<pii , vector<pii> , greater<pii> > pq;
for(int i = 0 ; i < k ; i++) {
int x;
scanf("%lld" , &x);
x--;
d[x] = 0;
pq.push(pii(0,x));
}
while(!pq.empty()){
pii u = pq.top();
pq.pop();
if(d[u.S] != u.F) continue;
for(int j = 0 ; j < adj[u.S].size();j++){
pii v = adj[u.S][j];
if(d[v.S] > v.F + u.F){
d[v.S] = v.F + u.F;
pq.push(pii(d[v.S] , v.S));
}
}
}
for(int i = 0 ; i < m ; i++){
v[i].z = min(d[v[i].x] , d[v[i].y]);
adj[v[i].x].clear();
adj[v[i].y].clear();
}
sort(v.begin() , v.end() , cmp);
for(int i = 0 ; i <m;i++){
if(fd(v[i].x) != fd(v[i].y)){
join(v[i].x , v[i].y);
adj[v[i].x].pb(pii(v[i].z , v[i].y));
adj[v[i].y].pb(pii(v[i].z , v[i].x));
}
}
for(int i=0;i<100005;i++) custo[i] = (int) 1e18;
for(int i = 0 ; i < 100005 ; i++)
for(int j = 0 ; j < 22 ; j++) anc[i][j] = -1 , sparse[i][j] = (int) 1e18;
memset(fn , -1 , sizeof fn);
pre(0 , 0);
for(int i = 0 ; i < 100005 ; i++){
if(fn[i] != -1) anc[i][0] = fn[i] , sparse[i][0] = custo[i];
}
for(int j = 1; j < 22 ; j++){
for(int i = 0 ; i < 100005;i++){
if(anc[i][j-1] != -1) anc[i][j] = anc[anc[i][j-1]][j-1] , sparse[i][j] = min(sparse[i][j-1] , sparse[anc[i][j-1]][j-1]);
}
}
int q;
scanf("%lld" , &q);
while(q--){
int x,y;
scanf("%lld%lld" , &x , &y);
x-- ,y--;
printf("%lld\n" , query(x,y));
}
}
Compilation message
plan.cpp: In function 'void pre(long long int, long long int)':
plan.cpp:22:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int y = 0 ; y < adj[i].size();y++){
~~^~~~~~~~~~~~~~~
plan.cpp: In function 'int32_t main()':
plan.cpp:90:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0 ; j < adj[u.S].size();j++){
~~^~~~~~~~~~~~~~~~~
plan.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld" , &n, &m);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
plan.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld" , &x , &y , &z);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:77:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld" , &k);
~~~~~^~~~~~~~~~~~~
plan.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld" , &x);
~~~~~^~~~~~~~~~~~~
plan.cpp:125:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld" , &q);
~~~~~^~~~~~~~~~~~~
plan.cpp:128:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld" , &x , &y);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
38748 KB |
Output is correct |
2 |
Correct |
45 ms |
38876 KB |
Output is correct |
3 |
Correct |
44 ms |
38776 KB |
Output is correct |
4 |
Correct |
43 ms |
38648 KB |
Output is correct |
5 |
Correct |
45 ms |
38800 KB |
Output is correct |
6 |
Correct |
45 ms |
38776 KB |
Output is correct |
7 |
Correct |
44 ms |
38776 KB |
Output is correct |
8 |
Correct |
44 ms |
38756 KB |
Output is correct |
9 |
Correct |
45 ms |
38824 KB |
Output is correct |
10 |
Correct |
45 ms |
38760 KB |
Output is correct |
11 |
Correct |
45 ms |
38876 KB |
Output is correct |
12 |
Correct |
44 ms |
38776 KB |
Output is correct |
13 |
Correct |
44 ms |
38776 KB |
Output is correct |
14 |
Correct |
44 ms |
38776 KB |
Output is correct |
15 |
Correct |
46 ms |
38776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
38748 KB |
Output is correct |
2 |
Correct |
45 ms |
38876 KB |
Output is correct |
3 |
Correct |
44 ms |
38776 KB |
Output is correct |
4 |
Correct |
43 ms |
38648 KB |
Output is correct |
5 |
Correct |
45 ms |
38800 KB |
Output is correct |
6 |
Correct |
45 ms |
38776 KB |
Output is correct |
7 |
Correct |
44 ms |
38776 KB |
Output is correct |
8 |
Correct |
44 ms |
38756 KB |
Output is correct |
9 |
Correct |
45 ms |
38824 KB |
Output is correct |
10 |
Correct |
45 ms |
38760 KB |
Output is correct |
11 |
Correct |
45 ms |
38876 KB |
Output is correct |
12 |
Correct |
44 ms |
38776 KB |
Output is correct |
13 |
Correct |
44 ms |
38776 KB |
Output is correct |
14 |
Correct |
44 ms |
38776 KB |
Output is correct |
15 |
Correct |
46 ms |
38776 KB |
Output is correct |
16 |
Correct |
264 ms |
51552 KB |
Output is correct |
17 |
Correct |
771 ms |
85636 KB |
Output is correct |
18 |
Correct |
97 ms |
43476 KB |
Output is correct |
19 |
Correct |
228 ms |
54060 KB |
Output is correct |
20 |
Correct |
775 ms |
85812 KB |
Output is correct |
21 |
Correct |
430 ms |
62616 KB |
Output is correct |
22 |
Correct |
270 ms |
56172 KB |
Output is correct |
23 |
Correct |
792 ms |
86112 KB |
Output is correct |
24 |
Correct |
813 ms |
85820 KB |
Output is correct |
25 |
Correct |
748 ms |
86136 KB |
Output is correct |
26 |
Correct |
219 ms |
53912 KB |
Output is correct |
27 |
Correct |
223 ms |
53920 KB |
Output is correct |
28 |
Correct |
220 ms |
53804 KB |
Output is correct |
29 |
Correct |
222 ms |
54100 KB |
Output is correct |
30 |
Correct |
221 ms |
54196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
38748 KB |
Output is correct |
2 |
Correct |
52 ms |
38648 KB |
Output is correct |
3 |
Correct |
44 ms |
38776 KB |
Output is correct |
4 |
Correct |
44 ms |
38776 KB |
Output is correct |
5 |
Correct |
44 ms |
38748 KB |
Output is correct |
6 |
Correct |
43 ms |
38652 KB |
Output is correct |
7 |
Correct |
44 ms |
38648 KB |
Output is correct |
8 |
Correct |
44 ms |
38648 KB |
Output is correct |
9 |
Correct |
42 ms |
38708 KB |
Output is correct |
10 |
Correct |
43 ms |
38692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
347 ms |
60452 KB |
Output is correct |
2 |
Correct |
685 ms |
85480 KB |
Output is correct |
3 |
Correct |
671 ms |
85116 KB |
Output is correct |
4 |
Correct |
220 ms |
52900 KB |
Output is correct |
5 |
Correct |
691 ms |
84992 KB |
Output is correct |
6 |
Correct |
682 ms |
85144 KB |
Output is correct |
7 |
Correct |
694 ms |
84892 KB |
Output is correct |
8 |
Correct |
665 ms |
85064 KB |
Output is correct |
9 |
Correct |
676 ms |
86032 KB |
Output is correct |
10 |
Correct |
654 ms |
86604 KB |
Output is correct |
11 |
Correct |
666 ms |
86660 KB |
Output is correct |
12 |
Correct |
667 ms |
86504 KB |
Output is correct |
13 |
Correct |
669 ms |
86300 KB |
Output is correct |
14 |
Correct |
714 ms |
86876 KB |
Output is correct |
15 |
Correct |
706 ms |
88920 KB |
Output is correct |
16 |
Correct |
704 ms |
86004 KB |
Output is correct |
17 |
Correct |
691 ms |
86752 KB |
Output is correct |
18 |
Correct |
685 ms |
86220 KB |
Output is correct |
19 |
Correct |
203 ms |
54868 KB |
Output is correct |
20 |
Correct |
698 ms |
86748 KB |
Output is correct |
21 |
Correct |
635 ms |
86160 KB |
Output is correct |
22 |
Correct |
164 ms |
53676 KB |
Output is correct |
23 |
Correct |
209 ms |
51216 KB |
Output is correct |
24 |
Correct |
170 ms |
53840 KB |
Output is correct |
25 |
Correct |
169 ms |
53792 KB |
Output is correct |
26 |
Correct |
208 ms |
51888 KB |
Output is correct |
27 |
Correct |
223 ms |
54536 KB |
Output is correct |
28 |
Correct |
167 ms |
53716 KB |
Output is correct |
29 |
Correct |
221 ms |
53680 KB |
Output is correct |
30 |
Correct |
172 ms |
53840 KB |
Output is correct |
31 |
Correct |
218 ms |
54036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
38748 KB |
Output is correct |
2 |
Correct |
45 ms |
38876 KB |
Output is correct |
3 |
Correct |
44 ms |
38776 KB |
Output is correct |
4 |
Correct |
43 ms |
38648 KB |
Output is correct |
5 |
Correct |
45 ms |
38800 KB |
Output is correct |
6 |
Correct |
45 ms |
38776 KB |
Output is correct |
7 |
Correct |
44 ms |
38776 KB |
Output is correct |
8 |
Correct |
44 ms |
38756 KB |
Output is correct |
9 |
Correct |
45 ms |
38824 KB |
Output is correct |
10 |
Correct |
45 ms |
38760 KB |
Output is correct |
11 |
Correct |
45 ms |
38876 KB |
Output is correct |
12 |
Correct |
44 ms |
38776 KB |
Output is correct |
13 |
Correct |
44 ms |
38776 KB |
Output is correct |
14 |
Correct |
44 ms |
38776 KB |
Output is correct |
15 |
Correct |
46 ms |
38776 KB |
Output is correct |
16 |
Correct |
264 ms |
51552 KB |
Output is correct |
17 |
Correct |
771 ms |
85636 KB |
Output is correct |
18 |
Correct |
97 ms |
43476 KB |
Output is correct |
19 |
Correct |
228 ms |
54060 KB |
Output is correct |
20 |
Correct |
775 ms |
85812 KB |
Output is correct |
21 |
Correct |
430 ms |
62616 KB |
Output is correct |
22 |
Correct |
270 ms |
56172 KB |
Output is correct |
23 |
Correct |
792 ms |
86112 KB |
Output is correct |
24 |
Correct |
813 ms |
85820 KB |
Output is correct |
25 |
Correct |
748 ms |
86136 KB |
Output is correct |
26 |
Correct |
219 ms |
53912 KB |
Output is correct |
27 |
Correct |
223 ms |
53920 KB |
Output is correct |
28 |
Correct |
220 ms |
53804 KB |
Output is correct |
29 |
Correct |
222 ms |
54100 KB |
Output is correct |
30 |
Correct |
221 ms |
54196 KB |
Output is correct |
31 |
Correct |
51 ms |
38748 KB |
Output is correct |
32 |
Correct |
52 ms |
38648 KB |
Output is correct |
33 |
Correct |
44 ms |
38776 KB |
Output is correct |
34 |
Correct |
44 ms |
38776 KB |
Output is correct |
35 |
Correct |
44 ms |
38748 KB |
Output is correct |
36 |
Correct |
43 ms |
38652 KB |
Output is correct |
37 |
Correct |
44 ms |
38648 KB |
Output is correct |
38 |
Correct |
44 ms |
38648 KB |
Output is correct |
39 |
Correct |
42 ms |
38708 KB |
Output is correct |
40 |
Correct |
43 ms |
38692 KB |
Output is correct |
41 |
Correct |
347 ms |
60452 KB |
Output is correct |
42 |
Correct |
685 ms |
85480 KB |
Output is correct |
43 |
Correct |
671 ms |
85116 KB |
Output is correct |
44 |
Correct |
220 ms |
52900 KB |
Output is correct |
45 |
Correct |
691 ms |
84992 KB |
Output is correct |
46 |
Correct |
682 ms |
85144 KB |
Output is correct |
47 |
Correct |
694 ms |
84892 KB |
Output is correct |
48 |
Correct |
665 ms |
85064 KB |
Output is correct |
49 |
Correct |
676 ms |
86032 KB |
Output is correct |
50 |
Correct |
654 ms |
86604 KB |
Output is correct |
51 |
Correct |
666 ms |
86660 KB |
Output is correct |
52 |
Correct |
667 ms |
86504 KB |
Output is correct |
53 |
Correct |
669 ms |
86300 KB |
Output is correct |
54 |
Correct |
714 ms |
86876 KB |
Output is correct |
55 |
Correct |
706 ms |
88920 KB |
Output is correct |
56 |
Correct |
704 ms |
86004 KB |
Output is correct |
57 |
Correct |
691 ms |
86752 KB |
Output is correct |
58 |
Correct |
685 ms |
86220 KB |
Output is correct |
59 |
Correct |
203 ms |
54868 KB |
Output is correct |
60 |
Correct |
698 ms |
86748 KB |
Output is correct |
61 |
Correct |
635 ms |
86160 KB |
Output is correct |
62 |
Correct |
164 ms |
53676 KB |
Output is correct |
63 |
Correct |
209 ms |
51216 KB |
Output is correct |
64 |
Correct |
170 ms |
53840 KB |
Output is correct |
65 |
Correct |
169 ms |
53792 KB |
Output is correct |
66 |
Correct |
208 ms |
51888 KB |
Output is correct |
67 |
Correct |
223 ms |
54536 KB |
Output is correct |
68 |
Correct |
167 ms |
53716 KB |
Output is correct |
69 |
Correct |
221 ms |
53680 KB |
Output is correct |
70 |
Correct |
172 ms |
53840 KB |
Output is correct |
71 |
Correct |
218 ms |
54036 KB |
Output is correct |
72 |
Correct |
432 ms |
62660 KB |
Output is correct |
73 |
Correct |
767 ms |
85888 KB |
Output is correct |
74 |
Correct |
771 ms |
84328 KB |
Output is correct |
75 |
Correct |
796 ms |
84320 KB |
Output is correct |
76 |
Correct |
792 ms |
84484 KB |
Output is correct |
77 |
Correct |
759 ms |
84284 KB |
Output is correct |
78 |
Correct |
752 ms |
84232 KB |
Output is correct |
79 |
Correct |
771 ms |
84168 KB |
Output is correct |
80 |
Correct |
761 ms |
84152 KB |
Output is correct |
81 |
Correct |
755 ms |
84252 KB |
Output is correct |
82 |
Correct |
759 ms |
83980 KB |
Output is correct |
83 |
Correct |
752 ms |
83840 KB |
Output is correct |
84 |
Correct |
778 ms |
84024 KB |
Output is correct |
85 |
Correct |
768 ms |
86500 KB |
Output is correct |
86 |
Correct |
778 ms |
83916 KB |
Output is correct |
87 |
Correct |
802 ms |
84040 KB |
Output is correct |
88 |
Correct |
753 ms |
84248 KB |
Output is correct |
89 |
Correct |
347 ms |
53664 KB |
Output is correct |
90 |
Correct |
762 ms |
84420 KB |
Output is correct |
91 |
Correct |
722 ms |
83948 KB |
Output is correct |
92 |
Correct |
247 ms |
53448 KB |
Output is correct |
93 |
Correct |
321 ms |
51104 KB |
Output is correct |
94 |
Correct |
274 ms |
53640 KB |
Output is correct |
95 |
Correct |
234 ms |
53332 KB |
Output is correct |
96 |
Correct |
321 ms |
51476 KB |
Output is correct |
97 |
Correct |
405 ms |
53084 KB |
Output is correct |
98 |
Correct |
232 ms |
53148 KB |
Output is correct |
99 |
Correct |
353 ms |
54268 KB |
Output is correct |
100 |
Correct |
242 ms |
53616 KB |
Output is correct |