#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct Edge {
int u, v, w;
bool operator < (const Edge& o) const {
return w < o.w;
}
} e[100100], e2[100100];
int w[100100];
ll qs[100100];
int p[505];
int fr(int i) {
return p[i] == i ? i : p[i] = fr(p[i]);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 1;i <= m;i++) {
cin >> e[i].u >> e[i].v >> e[i].w;
}
int q;
cin >> q;
if (q <= 10) {
while (q--) {
int x;
cin >> x;
for (int i = 1;i <= m;i++) {
e2[i].u = e[i].u;
e2[i].v = e[i].v;
e2[i].w = abs(e[i].w - x);
}
for (int i = 1;i <= n;i++) p[i] = i;
sort(e2 + 1, e2 + 1 + m);
ll ans = 0;
for (int i = 1;i <= m;i++) {
int pu = fr(e2[i].u), pv = fr(e2[i].v);
if (pu == pv) continue;
ans += e2[i].w;
p[pu] = pv;
}
cout << ans << '\n';
}
return 0;
}
sort(e + 1, e + 1 + m);
for (int i = 1;i <= m;i++) {
w[i] = e[i].w;
qs[i] = qs[i - 1] + w[i];
}
ll p = 0;
while (q--) {
int x;
cin >> x;
while (p < n && w[p + 1] <= x) p++;
cout << (p * x - qs[p]) + ((qs[n] - qs[p]) - (n - p) * x) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
77 ms |
3676 KB |
Output is correct |
21 |
Correct |
103 ms |
3832 KB |
Output is correct |
22 |
Correct |
103 ms |
3676 KB |
Output is correct |
23 |
Correct |
96 ms |
3664 KB |
Output is correct |
24 |
Correct |
78 ms |
3672 KB |
Output is correct |
25 |
Correct |
77 ms |
3676 KB |
Output is correct |
26 |
Correct |
97 ms |
3780 KB |
Output is correct |
27 |
Correct |
78 ms |
3672 KB |
Output is correct |
28 |
Correct |
60 ms |
3672 KB |
Output is correct |
29 |
Correct |
41 ms |
3672 KB |
Output is correct |
30 |
Correct |
79 ms |
3688 KB |
Output is correct |
31 |
Correct |
100 ms |
3664 KB |
Output is correct |
32 |
Correct |
85 ms |
3672 KB |
Output is correct |
33 |
Correct |
99 ms |
3676 KB |
Output is correct |
34 |
Correct |
30 ms |
3672 KB |
Output is correct |
35 |
Correct |
82 ms |
3784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Incorrect |
142 ms |
16540 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Incorrect |
124 ms |
15168 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
77 ms |
3676 KB |
Output is correct |
21 |
Correct |
103 ms |
3832 KB |
Output is correct |
22 |
Correct |
103 ms |
3676 KB |
Output is correct |
23 |
Correct |
96 ms |
3664 KB |
Output is correct |
24 |
Correct |
78 ms |
3672 KB |
Output is correct |
25 |
Correct |
77 ms |
3676 KB |
Output is correct |
26 |
Correct |
97 ms |
3780 KB |
Output is correct |
27 |
Correct |
78 ms |
3672 KB |
Output is correct |
28 |
Correct |
60 ms |
3672 KB |
Output is correct |
29 |
Correct |
41 ms |
3672 KB |
Output is correct |
30 |
Correct |
79 ms |
3688 KB |
Output is correct |
31 |
Correct |
100 ms |
3664 KB |
Output is correct |
32 |
Correct |
85 ms |
3672 KB |
Output is correct |
33 |
Correct |
99 ms |
3676 KB |
Output is correct |
34 |
Correct |
30 ms |
3672 KB |
Output is correct |
35 |
Correct |
82 ms |
3784 KB |
Output is correct |
36 |
Incorrect |
23 ms |
3928 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
77 ms |
3676 KB |
Output is correct |
21 |
Correct |
103 ms |
3832 KB |
Output is correct |
22 |
Correct |
103 ms |
3676 KB |
Output is correct |
23 |
Correct |
96 ms |
3664 KB |
Output is correct |
24 |
Correct |
78 ms |
3672 KB |
Output is correct |
25 |
Correct |
77 ms |
3676 KB |
Output is correct |
26 |
Correct |
97 ms |
3780 KB |
Output is correct |
27 |
Correct |
78 ms |
3672 KB |
Output is correct |
28 |
Correct |
60 ms |
3672 KB |
Output is correct |
29 |
Correct |
41 ms |
3672 KB |
Output is correct |
30 |
Correct |
79 ms |
3688 KB |
Output is correct |
31 |
Correct |
100 ms |
3664 KB |
Output is correct |
32 |
Correct |
85 ms |
3672 KB |
Output is correct |
33 |
Correct |
99 ms |
3676 KB |
Output is correct |
34 |
Correct |
30 ms |
3672 KB |
Output is correct |
35 |
Correct |
82 ms |
3784 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Incorrect |
142 ms |
16540 KB |
Output isn't correct |
40 |
Halted |
0 ms |
0 KB |
- |