#include<bits/stdc++.h>
#define maxn 100010
#define ff first
#define ss second
using namespace std;
int n,m,k,q;
bool is_npp[maxn];
vector<pair<int,int>> edges[maxn];
vector<long long> sp(maxn,INT_MAX);
vector<int> npp;
int parent[maxn];
struct Query
{
int qu,qv,idx;
};
queue<Query> quer[maxn];
void dijkstra()
{
priority_queue<pair<long long,int>> q;
vector<long long> dist(n+1,INT_MAX);
for(int i=0;i<npp.size();i++)
{
q.push({0,npp[i]});
dist[npp[i]]=0;
sp[npp[i]]=0;
}
while(!q.empty())
{
int curr=q.top().second;
long long currDist=-q.top().first;
q.pop();
if(dist[curr]<currDist) continue;
for(auto edge:edges[curr])
{
int next=edge.first;
int weight=edge.second;
if(dist[curr]+weight<dist[next])
{
dist[next]=dist[curr]+weight;
sp[next]=min(sp[next],dist[next]);
q.push({-dist[next],next});
}
}
}
}
int findd(int u)
{
if(parent[u]==0) return u;
return parent[u]=findd(parent[u]);
}
int ans[maxn];
void unite(int u,int v,int mn)
{
u=findd(u);
v=findd(v);
if(u==v) return;
if(quer[u].size()<quer[v].size()) swap(u,v);
parent[v]=u;
while(!quer[v].empty())
{
Query curr=quer[v].front();
quer[v].pop();
if(findd(curr.qu)==findd(curr.qv) && ans[curr.idx]==-1) {ans[curr.idx]=mn;continue;}
if(ans[curr.idx]==-1) quer[u].push(curr);
}
}
vector<pair<int,int>> finale;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
int ai,bi,wi;
for(int i=0;i<m;i++)
{
cin>>ai>>bi>>wi;
edges[ai].push_back({bi,wi});
edges[bi].push_back({ai,wi});
}
cin>>k;
for(int i=0;i<k;i++)
{
int npp1;
cin>>npp1;
is_npp[npp1]=true;
npp.push_back(npp1);
}
dijkstra();
cin>>q;
for(int i=0;i<q;i++)
{
ans[i]=-1;
Query curr;
cin>>curr.qu>>curr.qv;
curr.idx=i;
quer[curr.qu].push(curr);
quer[curr.qv].push(curr);
}
for(int i=1;i<=n;i++)
{
finale.push_back({sp[i],i});
}
sort(finale.begin(),finale.end());
reverse(finale.begin(),finale.end());
for(int i=0;i<finale.size();i++)
{
auto curr=finale[i];
for(auto e:edges[curr.ss])
{
if(curr.ff<=sp[e.ff])
{
unite(curr.ss,e.ff,curr.ff);
}
}
}
for(int i=0;i<q;i++)
{
cout<<ans[i]<<"\n";
}
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
*/
Compilation message
plan.cpp: In function 'void dijkstra()':
plan.cpp:22:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i=0;i<npp.size();i++)
| ~^~~~~~~~~~~
plan.cpp: In function 'int main()':
plan.cpp:106:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int i=0;i<finale.size();i++)
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
69212 KB |
Output is correct |
2 |
Correct |
35 ms |
69296 KB |
Output is correct |
3 |
Correct |
42 ms |
69204 KB |
Output is correct |
4 |
Correct |
32 ms |
69204 KB |
Output is correct |
5 |
Correct |
39 ms |
69460 KB |
Output is correct |
6 |
Correct |
47 ms |
69344 KB |
Output is correct |
7 |
Correct |
35 ms |
69212 KB |
Output is correct |
8 |
Correct |
35 ms |
69212 KB |
Output is correct |
9 |
Correct |
33 ms |
69200 KB |
Output is correct |
10 |
Correct |
35 ms |
69720 KB |
Output is correct |
11 |
Correct |
36 ms |
69204 KB |
Output is correct |
12 |
Correct |
35 ms |
69464 KB |
Output is correct |
13 |
Correct |
34 ms |
69200 KB |
Output is correct |
14 |
Correct |
34 ms |
69432 KB |
Output is correct |
15 |
Correct |
38 ms |
69468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
69212 KB |
Output is correct |
2 |
Correct |
35 ms |
69296 KB |
Output is correct |
3 |
Correct |
42 ms |
69204 KB |
Output is correct |
4 |
Correct |
32 ms |
69204 KB |
Output is correct |
5 |
Correct |
39 ms |
69460 KB |
Output is correct |
6 |
Correct |
47 ms |
69344 KB |
Output is correct |
7 |
Correct |
35 ms |
69212 KB |
Output is correct |
8 |
Correct |
35 ms |
69212 KB |
Output is correct |
9 |
Correct |
33 ms |
69200 KB |
Output is correct |
10 |
Correct |
35 ms |
69720 KB |
Output is correct |
11 |
Correct |
36 ms |
69204 KB |
Output is correct |
12 |
Correct |
35 ms |
69464 KB |
Output is correct |
13 |
Correct |
34 ms |
69200 KB |
Output is correct |
14 |
Correct |
34 ms |
69432 KB |
Output is correct |
15 |
Correct |
38 ms |
69468 KB |
Output is correct |
16 |
Correct |
132 ms |
78376 KB |
Output is correct |
17 |
Correct |
303 ms |
95780 KB |
Output is correct |
18 |
Correct |
54 ms |
71760 KB |
Output is correct |
19 |
Correct |
138 ms |
80400 KB |
Output is correct |
20 |
Correct |
300 ms |
95800 KB |
Output is correct |
21 |
Correct |
201 ms |
83480 KB |
Output is correct |
22 |
Correct |
128 ms |
78652 KB |
Output is correct |
23 |
Correct |
293 ms |
95752 KB |
Output is correct |
24 |
Correct |
296 ms |
95736 KB |
Output is correct |
25 |
Correct |
308 ms |
95976 KB |
Output is correct |
26 |
Correct |
134 ms |
80224 KB |
Output is correct |
27 |
Correct |
145 ms |
80140 KB |
Output is correct |
28 |
Correct |
142 ms |
80152 KB |
Output is correct |
29 |
Correct |
134 ms |
80140 KB |
Output is correct |
30 |
Correct |
152 ms |
80144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
69376 KB |
Output is correct |
2 |
Correct |
36 ms |
69236 KB |
Output is correct |
3 |
Correct |
40 ms |
69204 KB |
Output is correct |
4 |
Correct |
37 ms |
69200 KB |
Output is correct |
5 |
Correct |
40 ms |
69376 KB |
Output is correct |
6 |
Correct |
36 ms |
69208 KB |
Output is correct |
7 |
Correct |
40 ms |
69208 KB |
Output is correct |
8 |
Correct |
41 ms |
69212 KB |
Output is correct |
9 |
Correct |
38 ms |
69328 KB |
Output is correct |
10 |
Correct |
49 ms |
69212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
145 ms |
81240 KB |
Output is correct |
2 |
Correct |
260 ms |
94412 KB |
Output is correct |
3 |
Correct |
254 ms |
94492 KB |
Output is correct |
4 |
Correct |
78 ms |
76308 KB |
Output is correct |
5 |
Correct |
240 ms |
94376 KB |
Output is correct |
6 |
Correct |
234 ms |
94412 KB |
Output is correct |
7 |
Correct |
254 ms |
94404 KB |
Output is correct |
8 |
Correct |
250 ms |
94512 KB |
Output is correct |
9 |
Correct |
236 ms |
94328 KB |
Output is correct |
10 |
Correct |
252 ms |
94416 KB |
Output is correct |
11 |
Correct |
287 ms |
94376 KB |
Output is correct |
12 |
Correct |
240 ms |
94408 KB |
Output is correct |
13 |
Correct |
273 ms |
94416 KB |
Output is correct |
14 |
Correct |
252 ms |
94408 KB |
Output is correct |
15 |
Correct |
252 ms |
96704 KB |
Output is correct |
16 |
Correct |
238 ms |
94548 KB |
Output is correct |
17 |
Correct |
224 ms |
94372 KB |
Output is correct |
18 |
Correct |
267 ms |
94388 KB |
Output is correct |
19 |
Correct |
82 ms |
76100 KB |
Output is correct |
20 |
Correct |
244 ms |
94336 KB |
Output is correct |
21 |
Correct |
238 ms |
95312 KB |
Output is correct |
22 |
Correct |
105 ms |
77900 KB |
Output is correct |
23 |
Correct |
83 ms |
76712 KB |
Output is correct |
24 |
Correct |
96 ms |
77900 KB |
Output is correct |
25 |
Correct |
90 ms |
77892 KB |
Output is correct |
26 |
Correct |
124 ms |
76796 KB |
Output is correct |
27 |
Correct |
85 ms |
76228 KB |
Output is correct |
28 |
Correct |
94 ms |
77900 KB |
Output is correct |
29 |
Correct |
80 ms |
76092 KB |
Output is correct |
30 |
Correct |
93 ms |
77804 KB |
Output is correct |
31 |
Correct |
75 ms |
76096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
69212 KB |
Output is correct |
2 |
Correct |
35 ms |
69296 KB |
Output is correct |
3 |
Correct |
42 ms |
69204 KB |
Output is correct |
4 |
Correct |
32 ms |
69204 KB |
Output is correct |
5 |
Correct |
39 ms |
69460 KB |
Output is correct |
6 |
Correct |
47 ms |
69344 KB |
Output is correct |
7 |
Correct |
35 ms |
69212 KB |
Output is correct |
8 |
Correct |
35 ms |
69212 KB |
Output is correct |
9 |
Correct |
33 ms |
69200 KB |
Output is correct |
10 |
Correct |
35 ms |
69720 KB |
Output is correct |
11 |
Correct |
36 ms |
69204 KB |
Output is correct |
12 |
Correct |
35 ms |
69464 KB |
Output is correct |
13 |
Correct |
34 ms |
69200 KB |
Output is correct |
14 |
Correct |
34 ms |
69432 KB |
Output is correct |
15 |
Correct |
38 ms |
69468 KB |
Output is correct |
16 |
Correct |
132 ms |
78376 KB |
Output is correct |
17 |
Correct |
303 ms |
95780 KB |
Output is correct |
18 |
Correct |
54 ms |
71760 KB |
Output is correct |
19 |
Correct |
138 ms |
80400 KB |
Output is correct |
20 |
Correct |
300 ms |
95800 KB |
Output is correct |
21 |
Correct |
201 ms |
83480 KB |
Output is correct |
22 |
Correct |
128 ms |
78652 KB |
Output is correct |
23 |
Correct |
293 ms |
95752 KB |
Output is correct |
24 |
Correct |
296 ms |
95736 KB |
Output is correct |
25 |
Correct |
308 ms |
95976 KB |
Output is correct |
26 |
Correct |
134 ms |
80224 KB |
Output is correct |
27 |
Correct |
145 ms |
80140 KB |
Output is correct |
28 |
Correct |
142 ms |
80152 KB |
Output is correct |
29 |
Correct |
134 ms |
80140 KB |
Output is correct |
30 |
Correct |
152 ms |
80144 KB |
Output is correct |
31 |
Correct |
39 ms |
69376 KB |
Output is correct |
32 |
Correct |
36 ms |
69236 KB |
Output is correct |
33 |
Correct |
40 ms |
69204 KB |
Output is correct |
34 |
Correct |
37 ms |
69200 KB |
Output is correct |
35 |
Correct |
40 ms |
69376 KB |
Output is correct |
36 |
Correct |
36 ms |
69208 KB |
Output is correct |
37 |
Correct |
40 ms |
69208 KB |
Output is correct |
38 |
Correct |
41 ms |
69212 KB |
Output is correct |
39 |
Correct |
38 ms |
69328 KB |
Output is correct |
40 |
Correct |
49 ms |
69212 KB |
Output is correct |
41 |
Correct |
145 ms |
81240 KB |
Output is correct |
42 |
Correct |
260 ms |
94412 KB |
Output is correct |
43 |
Correct |
254 ms |
94492 KB |
Output is correct |
44 |
Correct |
78 ms |
76308 KB |
Output is correct |
45 |
Correct |
240 ms |
94376 KB |
Output is correct |
46 |
Correct |
234 ms |
94412 KB |
Output is correct |
47 |
Correct |
254 ms |
94404 KB |
Output is correct |
48 |
Correct |
250 ms |
94512 KB |
Output is correct |
49 |
Correct |
236 ms |
94328 KB |
Output is correct |
50 |
Correct |
252 ms |
94416 KB |
Output is correct |
51 |
Correct |
287 ms |
94376 KB |
Output is correct |
52 |
Correct |
240 ms |
94408 KB |
Output is correct |
53 |
Correct |
273 ms |
94416 KB |
Output is correct |
54 |
Correct |
252 ms |
94408 KB |
Output is correct |
55 |
Correct |
252 ms |
96704 KB |
Output is correct |
56 |
Correct |
238 ms |
94548 KB |
Output is correct |
57 |
Correct |
224 ms |
94372 KB |
Output is correct |
58 |
Correct |
267 ms |
94388 KB |
Output is correct |
59 |
Correct |
82 ms |
76100 KB |
Output is correct |
60 |
Correct |
244 ms |
94336 KB |
Output is correct |
61 |
Correct |
238 ms |
95312 KB |
Output is correct |
62 |
Correct |
105 ms |
77900 KB |
Output is correct |
63 |
Correct |
83 ms |
76712 KB |
Output is correct |
64 |
Correct |
96 ms |
77900 KB |
Output is correct |
65 |
Correct |
90 ms |
77892 KB |
Output is correct |
66 |
Correct |
124 ms |
76796 KB |
Output is correct |
67 |
Correct |
85 ms |
76228 KB |
Output is correct |
68 |
Correct |
94 ms |
77900 KB |
Output is correct |
69 |
Correct |
80 ms |
76092 KB |
Output is correct |
70 |
Correct |
93 ms |
77804 KB |
Output is correct |
71 |
Correct |
75 ms |
76096 KB |
Output is correct |
72 |
Correct |
207 ms |
83796 KB |
Output is correct |
73 |
Correct |
289 ms |
95744 KB |
Output is correct |
74 |
Correct |
297 ms |
95740 KB |
Output is correct |
75 |
Correct |
296 ms |
95996 KB |
Output is correct |
76 |
Correct |
311 ms |
95812 KB |
Output is correct |
77 |
Correct |
295 ms |
95988 KB |
Output is correct |
78 |
Correct |
291 ms |
96004 KB |
Output is correct |
79 |
Correct |
369 ms |
95936 KB |
Output is correct |
80 |
Correct |
316 ms |
95832 KB |
Output is correct |
81 |
Correct |
303 ms |
95996 KB |
Output is correct |
82 |
Correct |
288 ms |
96004 KB |
Output is correct |
83 |
Correct |
296 ms |
95856 KB |
Output is correct |
84 |
Correct |
302 ms |
95984 KB |
Output is correct |
85 |
Correct |
293 ms |
96704 KB |
Output is correct |
86 |
Correct |
308 ms |
95824 KB |
Output is correct |
87 |
Correct |
314 ms |
95996 KB |
Output is correct |
88 |
Correct |
293 ms |
95892 KB |
Output is correct |
89 |
Correct |
136 ms |
78860 KB |
Output is correct |
90 |
Correct |
293 ms |
95996 KB |
Output is correct |
91 |
Correct |
290 ms |
96632 KB |
Output is correct |
92 |
Correct |
144 ms |
79124 KB |
Output is correct |
93 |
Correct |
147 ms |
79428 KB |
Output is correct |
94 |
Correct |
157 ms |
79148 KB |
Output is correct |
95 |
Correct |
151 ms |
79112 KB |
Output is correct |
96 |
Correct |
155 ms |
79940 KB |
Output is correct |
97 |
Correct |
129 ms |
79172 KB |
Output is correct |
98 |
Correct |
153 ms |
79140 KB |
Output is correct |
99 |
Correct |
133 ms |
79164 KB |
Output is correct |
100 |
Correct |
176 ms |
79328 KB |
Output is correct |