#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const int nx=5e2+5, mx=1e5+5;
ll n, m, a[mx], b[mx], w[mx], dsu[nx], x, q;
int find(int x)
{
if (dsu[x]==x) return x;
return dsu[x]=find(dsu[x]);
}
int32_t main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=m; i++) cin>>a[i]>>b[i]>>w[i];
cin>>q;
while (q--)
{
cin>>x;
vector<pair<ll, ll>> edg;
for (int i=1; i<=n; i++) dsu[i]=i;
for (int i=1; i<=m; i++) edg.push_back({abs(w[i]-x), i});
sort(edg.begin(), edg.end());
ll res=0;
for (auto [k, idx]:edg)
{
if (find(a[idx])==find(b[idx])) continue;
res+=k;
dsu[find(a[idx])]=find(b[idx]);
}
cout<<res<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 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 |
0 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 |
Correct |
102 ms |
8124 KB |
Output is correct |
21 |
Correct |
101 ms |
8148 KB |
Output is correct |
22 |
Correct |
102 ms |
8116 KB |
Output is correct |
23 |
Correct |
100 ms |
8132 KB |
Output is correct |
24 |
Correct |
101 ms |
8148 KB |
Output is correct |
25 |
Correct |
99 ms |
8156 KB |
Output is correct |
26 |
Correct |
99 ms |
8152 KB |
Output is correct |
27 |
Correct |
100 ms |
8144 KB |
Output is correct |
28 |
Correct |
114 ms |
8144 KB |
Output is correct |
29 |
Correct |
110 ms |
8148 KB |
Output is correct |
30 |
Correct |
100 ms |
8152 KB |
Output is correct |
31 |
Correct |
101 ms |
8136 KB |
Output is correct |
32 |
Correct |
99 ms |
8156 KB |
Output is correct |
33 |
Correct |
101 ms |
8156 KB |
Output is correct |
34 |
Correct |
57 ms |
8288 KB |
Output is correct |
35 |
Correct |
100 ms |
8144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Execution timed out |
5079 ms |
8324 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2392 KB |
Output is correct |
20 |
Execution timed out |
5095 ms |
6984 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 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 |
0 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 |
Correct |
102 ms |
8124 KB |
Output is correct |
21 |
Correct |
101 ms |
8148 KB |
Output is correct |
22 |
Correct |
102 ms |
8116 KB |
Output is correct |
23 |
Correct |
100 ms |
8132 KB |
Output is correct |
24 |
Correct |
101 ms |
8148 KB |
Output is correct |
25 |
Correct |
99 ms |
8156 KB |
Output is correct |
26 |
Correct |
99 ms |
8152 KB |
Output is correct |
27 |
Correct |
100 ms |
8144 KB |
Output is correct |
28 |
Correct |
114 ms |
8144 KB |
Output is correct |
29 |
Correct |
110 ms |
8148 KB |
Output is correct |
30 |
Correct |
100 ms |
8152 KB |
Output is correct |
31 |
Correct |
101 ms |
8136 KB |
Output is correct |
32 |
Correct |
99 ms |
8156 KB |
Output is correct |
33 |
Correct |
101 ms |
8156 KB |
Output is correct |
34 |
Correct |
57 ms |
8288 KB |
Output is correct |
35 |
Correct |
100 ms |
8144 KB |
Output is correct |
36 |
Execution timed out |
5048 ms |
8344 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 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 |
0 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 |
Correct |
102 ms |
8124 KB |
Output is correct |
21 |
Correct |
101 ms |
8148 KB |
Output is correct |
22 |
Correct |
102 ms |
8116 KB |
Output is correct |
23 |
Correct |
100 ms |
8132 KB |
Output is correct |
24 |
Correct |
101 ms |
8148 KB |
Output is correct |
25 |
Correct |
99 ms |
8156 KB |
Output is correct |
26 |
Correct |
99 ms |
8152 KB |
Output is correct |
27 |
Correct |
100 ms |
8144 KB |
Output is correct |
28 |
Correct |
114 ms |
8144 KB |
Output is correct |
29 |
Correct |
110 ms |
8148 KB |
Output is correct |
30 |
Correct |
100 ms |
8152 KB |
Output is correct |
31 |
Correct |
101 ms |
8136 KB |
Output is correct |
32 |
Correct |
99 ms |
8156 KB |
Output is correct |
33 |
Correct |
101 ms |
8156 KB |
Output is correct |
34 |
Correct |
57 ms |
8288 KB |
Output is correct |
35 |
Correct |
100 ms |
8144 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Execution timed out |
5079 ms |
8324 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |