#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int n, m;
cin >> n >> m;
const int maxm = 1e6, maxn = 700;
struct edge {
int a, b, we;
};
vector<edge> elist(maxm);
for (int i = 0; i < m; i++)
cin >> elist[i].a >> elist[i].b >> elist[i].we;
int qs;
cin >> qs;
while (qs--) {
vector<int> dsu(maxn);
iota(dsu.begin(), dsu.end(), 0);
int x;
cin >> x;
vector<edge> ve(elist);
for (int i = 0; i < m; i++) {
ve[i].we = abs(ve[i].we - x);
}
sort(ve.begin(), ve.end(), [&](edge a, edge b) { return a.we < b.we; });
function<int(int)> par = [&](int cur) {
if (dsu[cur] == cur)
return cur;
dsu[cur] = par(dsu[cur]);
return dsu[cur];
};
int cost = 0;
for (auto a : ve) {
if (par(a.a) != par(a.b)) {
cost += a.we;
dsu[par(a.a)] = par(a.b);
}
}
cout << cost << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
47296 KB |
Output is correct |
2 |
Correct |
95 ms |
47296 KB |
Output is correct |
3 |
Correct |
234 ms |
47296 KB |
Output is correct |
4 |
Correct |
226 ms |
47416 KB |
Output is correct |
5 |
Correct |
235 ms |
47304 KB |
Output is correct |
6 |
Correct |
227 ms |
47420 KB |
Output is correct |
7 |
Correct |
227 ms |
47272 KB |
Output is correct |
8 |
Correct |
232 ms |
47300 KB |
Output is correct |
9 |
Correct |
233 ms |
47308 KB |
Output is correct |
10 |
Correct |
225 ms |
47296 KB |
Output is correct |
11 |
Correct |
224 ms |
47300 KB |
Output is correct |
12 |
Correct |
28 ms |
47196 KB |
Output is correct |
13 |
Correct |
225 ms |
47416 KB |
Output is correct |
14 |
Correct |
239 ms |
47272 KB |
Output is correct |
15 |
Correct |
227 ms |
47296 KB |
Output is correct |
16 |
Correct |
229 ms |
47296 KB |
Output is correct |
17 |
Correct |
238 ms |
47300 KB |
Output is correct |
18 |
Correct |
229 ms |
47292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
47296 KB |
Output is correct |
2 |
Correct |
95 ms |
47296 KB |
Output is correct |
3 |
Correct |
234 ms |
47296 KB |
Output is correct |
4 |
Correct |
226 ms |
47416 KB |
Output is correct |
5 |
Correct |
235 ms |
47304 KB |
Output is correct |
6 |
Correct |
227 ms |
47420 KB |
Output is correct |
7 |
Correct |
227 ms |
47272 KB |
Output is correct |
8 |
Correct |
232 ms |
47300 KB |
Output is correct |
9 |
Correct |
233 ms |
47308 KB |
Output is correct |
10 |
Correct |
225 ms |
47296 KB |
Output is correct |
11 |
Correct |
224 ms |
47300 KB |
Output is correct |
12 |
Correct |
28 ms |
47196 KB |
Output is correct |
13 |
Correct |
225 ms |
47416 KB |
Output is correct |
14 |
Correct |
239 ms |
47272 KB |
Output is correct |
15 |
Correct |
227 ms |
47296 KB |
Output is correct |
16 |
Correct |
229 ms |
47296 KB |
Output is correct |
17 |
Correct |
238 ms |
47300 KB |
Output is correct |
18 |
Correct |
229 ms |
47292 KB |
Output is correct |
19 |
Correct |
225 ms |
47304 KB |
Output is correct |
20 |
Correct |
330 ms |
47304 KB |
Output is correct |
21 |
Correct |
335 ms |
49092 KB |
Output is correct |
22 |
Correct |
348 ms |
49120 KB |
Output is correct |
23 |
Correct |
332 ms |
48976 KB |
Output is correct |
24 |
Correct |
336 ms |
49088 KB |
Output is correct |
25 |
Correct |
328 ms |
49060 KB |
Output is correct |
26 |
Correct |
336 ms |
48980 KB |
Output is correct |
27 |
Correct |
342 ms |
49104 KB |
Output is correct |
28 |
Correct |
316 ms |
49092 KB |
Output is correct |
29 |
Correct |
305 ms |
49104 KB |
Output is correct |
30 |
Correct |
329 ms |
49092 KB |
Output is correct |
31 |
Correct |
335 ms |
49088 KB |
Output is correct |
32 |
Correct |
335 ms |
49104 KB |
Output is correct |
33 |
Correct |
328 ms |
49020 KB |
Output is correct |
34 |
Correct |
298 ms |
49088 KB |
Output is correct |
35 |
Correct |
332 ms |
49084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
47392 KB |
Output is correct |
2 |
Correct |
31 ms |
47196 KB |
Output is correct |
3 |
Correct |
234 ms |
47288 KB |
Output is correct |
4 |
Execution timed out |
5068 ms |
47324 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
47296 KB |
Output is correct |
2 |
Correct |
95 ms |
47296 KB |
Output is correct |
3 |
Correct |
234 ms |
47296 KB |
Output is correct |
4 |
Correct |
226 ms |
47416 KB |
Output is correct |
5 |
Correct |
235 ms |
47304 KB |
Output is correct |
6 |
Correct |
227 ms |
47420 KB |
Output is correct |
7 |
Correct |
227 ms |
47272 KB |
Output is correct |
8 |
Correct |
232 ms |
47300 KB |
Output is correct |
9 |
Correct |
233 ms |
47308 KB |
Output is correct |
10 |
Correct |
225 ms |
47296 KB |
Output is correct |
11 |
Correct |
224 ms |
47300 KB |
Output is correct |
12 |
Correct |
28 ms |
47196 KB |
Output is correct |
13 |
Correct |
225 ms |
47416 KB |
Output is correct |
14 |
Correct |
239 ms |
47272 KB |
Output is correct |
15 |
Correct |
227 ms |
47296 KB |
Output is correct |
16 |
Correct |
229 ms |
47296 KB |
Output is correct |
17 |
Correct |
238 ms |
47300 KB |
Output is correct |
18 |
Correct |
229 ms |
47292 KB |
Output is correct |
19 |
Correct |
244 ms |
47300 KB |
Output is correct |
20 |
Execution timed out |
5067 ms |
47304 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
47296 KB |
Output is correct |
2 |
Correct |
95 ms |
47296 KB |
Output is correct |
3 |
Correct |
234 ms |
47296 KB |
Output is correct |
4 |
Correct |
226 ms |
47416 KB |
Output is correct |
5 |
Correct |
235 ms |
47304 KB |
Output is correct |
6 |
Correct |
227 ms |
47420 KB |
Output is correct |
7 |
Correct |
227 ms |
47272 KB |
Output is correct |
8 |
Correct |
232 ms |
47300 KB |
Output is correct |
9 |
Correct |
233 ms |
47308 KB |
Output is correct |
10 |
Correct |
225 ms |
47296 KB |
Output is correct |
11 |
Correct |
224 ms |
47300 KB |
Output is correct |
12 |
Correct |
28 ms |
47196 KB |
Output is correct |
13 |
Correct |
225 ms |
47416 KB |
Output is correct |
14 |
Correct |
239 ms |
47272 KB |
Output is correct |
15 |
Correct |
227 ms |
47296 KB |
Output is correct |
16 |
Correct |
229 ms |
47296 KB |
Output is correct |
17 |
Correct |
238 ms |
47300 KB |
Output is correct |
18 |
Correct |
229 ms |
47292 KB |
Output is correct |
19 |
Correct |
225 ms |
47304 KB |
Output is correct |
20 |
Correct |
330 ms |
47304 KB |
Output is correct |
21 |
Correct |
335 ms |
49092 KB |
Output is correct |
22 |
Correct |
348 ms |
49120 KB |
Output is correct |
23 |
Correct |
332 ms |
48976 KB |
Output is correct |
24 |
Correct |
336 ms |
49088 KB |
Output is correct |
25 |
Correct |
328 ms |
49060 KB |
Output is correct |
26 |
Correct |
336 ms |
48980 KB |
Output is correct |
27 |
Correct |
342 ms |
49104 KB |
Output is correct |
28 |
Correct |
316 ms |
49092 KB |
Output is correct |
29 |
Correct |
305 ms |
49104 KB |
Output is correct |
30 |
Correct |
329 ms |
49092 KB |
Output is correct |
31 |
Correct |
335 ms |
49088 KB |
Output is correct |
32 |
Correct |
335 ms |
49104 KB |
Output is correct |
33 |
Correct |
328 ms |
49020 KB |
Output is correct |
34 |
Correct |
298 ms |
49088 KB |
Output is correct |
35 |
Correct |
332 ms |
49084 KB |
Output is correct |
36 |
Execution timed out |
5064 ms |
49228 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
47296 KB |
Output is correct |
2 |
Correct |
95 ms |
47296 KB |
Output is correct |
3 |
Correct |
234 ms |
47296 KB |
Output is correct |
4 |
Correct |
226 ms |
47416 KB |
Output is correct |
5 |
Correct |
235 ms |
47304 KB |
Output is correct |
6 |
Correct |
227 ms |
47420 KB |
Output is correct |
7 |
Correct |
227 ms |
47272 KB |
Output is correct |
8 |
Correct |
232 ms |
47300 KB |
Output is correct |
9 |
Correct |
233 ms |
47308 KB |
Output is correct |
10 |
Correct |
225 ms |
47296 KB |
Output is correct |
11 |
Correct |
224 ms |
47300 KB |
Output is correct |
12 |
Correct |
28 ms |
47196 KB |
Output is correct |
13 |
Correct |
225 ms |
47416 KB |
Output is correct |
14 |
Correct |
239 ms |
47272 KB |
Output is correct |
15 |
Correct |
227 ms |
47296 KB |
Output is correct |
16 |
Correct |
229 ms |
47296 KB |
Output is correct |
17 |
Correct |
238 ms |
47300 KB |
Output is correct |
18 |
Correct |
229 ms |
47292 KB |
Output is correct |
19 |
Correct |
225 ms |
47304 KB |
Output is correct |
20 |
Correct |
330 ms |
47304 KB |
Output is correct |
21 |
Correct |
335 ms |
49092 KB |
Output is correct |
22 |
Correct |
348 ms |
49120 KB |
Output is correct |
23 |
Correct |
332 ms |
48976 KB |
Output is correct |
24 |
Correct |
336 ms |
49088 KB |
Output is correct |
25 |
Correct |
328 ms |
49060 KB |
Output is correct |
26 |
Correct |
336 ms |
48980 KB |
Output is correct |
27 |
Correct |
342 ms |
49104 KB |
Output is correct |
28 |
Correct |
316 ms |
49092 KB |
Output is correct |
29 |
Correct |
305 ms |
49104 KB |
Output is correct |
30 |
Correct |
329 ms |
49092 KB |
Output is correct |
31 |
Correct |
335 ms |
49088 KB |
Output is correct |
32 |
Correct |
335 ms |
49104 KB |
Output is correct |
33 |
Correct |
328 ms |
49020 KB |
Output is correct |
34 |
Correct |
298 ms |
49088 KB |
Output is correct |
35 |
Correct |
332 ms |
49084 KB |
Output is correct |
36 |
Correct |
98 ms |
47392 KB |
Output is correct |
37 |
Correct |
31 ms |
47196 KB |
Output is correct |
38 |
Correct |
234 ms |
47288 KB |
Output is correct |
39 |
Execution timed out |
5068 ms |
47324 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |