#include <bits/stdc++.h>
using namespace std;
#define ii pair<int,int>
#define fi first
#define se second
#define endl '\n'
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound
#define rep(x,s,e) for (int x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e))?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct UFDS{
int p[505];
int r[505];
void reset(){ rep(x,0,505) p[x]=x,r[x]=0; }
int par(int i){
if (p[i]==i) return i;
else return p[i]=par(p[i]);
}
bool unions(int i,int j){
i=par(i),j=par(j);
if (i==j) return false;
if (r[i]<r[j]) swap(i,j);
p[i]=j;
if (r[i]==r[j]) r[j]++;
return true;
}
} ufds;
int n,m,q;
struct E{
int u,v;
int w;
};
E ee[100005];
int qu[1000005];
int split[1000005];
long long ans[1000005];
signed main(){
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(false);
cin>>n>>m;
rep(x,0,m) cin>>ee[x].u>>ee[x].v>>ee[x].w;
sort(ee,ee+m,[](E i,E j){
return i.w<j.w;
});
cin>>q;
rep(x,0,q) cin>>qu[x];
split[0]=0;
rep(x,0,q){
while (split[x]<m && ee[split[x]].w<=qu[x]) split[x]++;
split[x+1]=split[x];
}
rep(x,0,q){
ufds.reset();
int idx1=split[x]-1,idx2=split[x];
int num=0;
long long ans=0;
while (num<n-1){
if (idx2==m || (idx1!=-1 && qu[x]-ee[idx1].w<=ee[idx2].w-qu[x])){
if (ufds.unions(ee[idx1].u,ee[idx1].v)){
ans+=qu[x]-ee[idx1].w;
num++;
}
idx1--;
}
else{
if (ufds.unions(ee[idx2].u,ee[idx2].v)){
ans+=ee[idx2].w-qu[x];
num++;
}
idx2++;
}
}
cout<<ans<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
33 ms |
1500 KB |
Output is correct |
21 |
Correct |
39 ms |
1472 KB |
Output is correct |
22 |
Correct |
34 ms |
1412 KB |
Output is correct |
23 |
Correct |
33 ms |
1472 KB |
Output is correct |
24 |
Correct |
33 ms |
1492 KB |
Output is correct |
25 |
Correct |
31 ms |
1516 KB |
Output is correct |
26 |
Correct |
30 ms |
1524 KB |
Output is correct |
27 |
Correct |
29 ms |
1516 KB |
Output is correct |
28 |
Correct |
38 ms |
1428 KB |
Output is correct |
29 |
Correct |
36 ms |
1504 KB |
Output is correct |
30 |
Correct |
33 ms |
1504 KB |
Output is correct |
31 |
Correct |
29 ms |
1504 KB |
Output is correct |
32 |
Correct |
30 ms |
1456 KB |
Output is correct |
33 |
Correct |
31 ms |
1448 KB |
Output is correct |
34 |
Correct |
25 ms |
1440 KB |
Output is correct |
35 |
Correct |
28 ms |
1496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
5030 ms |
10508 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Execution timed out |
5055 ms |
13200 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
33 ms |
1500 KB |
Output is correct |
21 |
Correct |
39 ms |
1472 KB |
Output is correct |
22 |
Correct |
34 ms |
1412 KB |
Output is correct |
23 |
Correct |
33 ms |
1472 KB |
Output is correct |
24 |
Correct |
33 ms |
1492 KB |
Output is correct |
25 |
Correct |
31 ms |
1516 KB |
Output is correct |
26 |
Correct |
30 ms |
1524 KB |
Output is correct |
27 |
Correct |
29 ms |
1516 KB |
Output is correct |
28 |
Correct |
38 ms |
1428 KB |
Output is correct |
29 |
Correct |
36 ms |
1504 KB |
Output is correct |
30 |
Correct |
33 ms |
1504 KB |
Output is correct |
31 |
Correct |
29 ms |
1504 KB |
Output is correct |
32 |
Correct |
30 ms |
1456 KB |
Output is correct |
33 |
Correct |
31 ms |
1448 KB |
Output is correct |
34 |
Correct |
25 ms |
1440 KB |
Output is correct |
35 |
Correct |
28 ms |
1496 KB |
Output is correct |
36 |
Correct |
1038 ms |
1900 KB |
Output is correct |
37 |
Execution timed out |
5073 ms |
1812 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
33 ms |
1500 KB |
Output is correct |
21 |
Correct |
39 ms |
1472 KB |
Output is correct |
22 |
Correct |
34 ms |
1412 KB |
Output is correct |
23 |
Correct |
33 ms |
1472 KB |
Output is correct |
24 |
Correct |
33 ms |
1492 KB |
Output is correct |
25 |
Correct |
31 ms |
1516 KB |
Output is correct |
26 |
Correct |
30 ms |
1524 KB |
Output is correct |
27 |
Correct |
29 ms |
1516 KB |
Output is correct |
28 |
Correct |
38 ms |
1428 KB |
Output is correct |
29 |
Correct |
36 ms |
1504 KB |
Output is correct |
30 |
Correct |
33 ms |
1504 KB |
Output is correct |
31 |
Correct |
29 ms |
1504 KB |
Output is correct |
32 |
Correct |
30 ms |
1456 KB |
Output is correct |
33 |
Correct |
31 ms |
1448 KB |
Output is correct |
34 |
Correct |
25 ms |
1440 KB |
Output is correct |
35 |
Correct |
28 ms |
1496 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Execution timed out |
5030 ms |
10508 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |