#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define ll long long
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=505;
const int M=1000005;
const ll INF=1e18;
struct DSU{
vector<int> pa,sz;
DSU(int n){
pa.resize(n);
sz.resize(n,1);
iota(all(pa),0);
}
int Find(int k){
return (k==pa[k]?k:pa[k]=Find(pa[k]));
}
void onion(int a,int b){
a=Find(a),b=Find(b);
if(a==b)return;
if(sz[a]>sz[b]){
swap(a,b);
}
pa[a]=b;
sz[b]+=sz[a];
}
int same(int a,int b){
return Find(a)==Find(b);
}
};
int32_t main() {
LCBorz;
int n,m;cin>>n>>m;
vector<array<int,3>> v(m);
for(int i=0;i<m;i++){
cin>>v[i][1]>>v[i][2]>>v[i][0];
}
sort(all(v));
int ptr=0;
int q;cin>>q;
for(int q1=0;q1<q;q1++){
int x;cin>>x;
while(ptr<m&&v[ptr][0]<=x){
ptr++;
}
DSU dsu(n+1);
ll ans=0;
int l=ptr-1,r=ptr;
while(l>=0&&r<m){
if(abs(x-v[l][0])<abs(x-v[r][0])){
if(!dsu.same(v[l][1],v[l][2])){
dsu.onion(v[l][1],v[l][2]);
ans+=abs(x-v[l][0]);
}
l--;
}
else{
if(!dsu.same(v[r][1],v[r][2])){
dsu.onion(v[r][1],v[r][2]);
ans+=abs(x-v[r][0]);
}
r++;
}
}
while(l>=0){
if(!dsu.same(v[l][1],v[l][2])){
dsu.onion(v[l][1],v[l][2]);
ans+=abs(x-v[l][0]);
}
l--;
}
while(r<m){
if(!dsu.same(v[r][1],v[r][2])){
dsu.onion(v[r][1],v[r][2]);
ans+=abs(x-v[r][0]);
}
r++;
}
cout<<ans<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
31 ms |
1624 KB |
Output is correct |
21 |
Correct |
32 ms |
1628 KB |
Output is correct |
22 |
Correct |
32 ms |
1624 KB |
Output is correct |
23 |
Correct |
31 ms |
1624 KB |
Output is correct |
24 |
Correct |
32 ms |
1624 KB |
Output is correct |
25 |
Correct |
30 ms |
1628 KB |
Output is correct |
26 |
Correct |
33 ms |
1628 KB |
Output is correct |
27 |
Correct |
35 ms |
1628 KB |
Output is correct |
28 |
Correct |
33 ms |
1624 KB |
Output is correct |
29 |
Correct |
34 ms |
1624 KB |
Output is correct |
30 |
Correct |
32 ms |
1632 KB |
Output is correct |
31 |
Correct |
34 ms |
1624 KB |
Output is correct |
32 |
Correct |
32 ms |
1628 KB |
Output is correct |
33 |
Correct |
37 ms |
1624 KB |
Output is correct |
34 |
Correct |
37 ms |
1624 KB |
Output is correct |
35 |
Correct |
35 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5079 ms |
1892 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Execution timed out |
5068 ms |
8216 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
31 ms |
1624 KB |
Output is correct |
21 |
Correct |
32 ms |
1628 KB |
Output is correct |
22 |
Correct |
32 ms |
1624 KB |
Output is correct |
23 |
Correct |
31 ms |
1624 KB |
Output is correct |
24 |
Correct |
32 ms |
1624 KB |
Output is correct |
25 |
Correct |
30 ms |
1628 KB |
Output is correct |
26 |
Correct |
33 ms |
1628 KB |
Output is correct |
27 |
Correct |
35 ms |
1628 KB |
Output is correct |
28 |
Correct |
33 ms |
1624 KB |
Output is correct |
29 |
Correct |
34 ms |
1624 KB |
Output is correct |
30 |
Correct |
32 ms |
1632 KB |
Output is correct |
31 |
Correct |
34 ms |
1624 KB |
Output is correct |
32 |
Correct |
32 ms |
1628 KB |
Output is correct |
33 |
Correct |
37 ms |
1624 KB |
Output is correct |
34 |
Correct |
37 ms |
1624 KB |
Output is correct |
35 |
Correct |
35 ms |
1628 KB |
Output is correct |
36 |
Execution timed out |
5039 ms |
1956 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
31 ms |
1624 KB |
Output is correct |
21 |
Correct |
32 ms |
1628 KB |
Output is correct |
22 |
Correct |
32 ms |
1624 KB |
Output is correct |
23 |
Correct |
31 ms |
1624 KB |
Output is correct |
24 |
Correct |
32 ms |
1624 KB |
Output is correct |
25 |
Correct |
30 ms |
1628 KB |
Output is correct |
26 |
Correct |
33 ms |
1628 KB |
Output is correct |
27 |
Correct |
35 ms |
1628 KB |
Output is correct |
28 |
Correct |
33 ms |
1624 KB |
Output is correct |
29 |
Correct |
34 ms |
1624 KB |
Output is correct |
30 |
Correct |
32 ms |
1632 KB |
Output is correct |
31 |
Correct |
34 ms |
1624 KB |
Output is correct |
32 |
Correct |
32 ms |
1628 KB |
Output is correct |
33 |
Correct |
37 ms |
1624 KB |
Output is correct |
34 |
Correct |
37 ms |
1624 KB |
Output is correct |
35 |
Correct |
35 ms |
1628 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
5079 ms |
1892 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |