#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 502;
int n, m;
int p[N], sz[N];
vector<int> g[N];
map<int, vector<int>> mp;
map<pair<int, int>, long long> ps;
int get(int v) {
return p[v] = (p[v] == v ? v : get(p[v]));
}
void unite(int u, int v) {
u = get(u); v = get(v);
if (sz[u] > sz[v]) {
swap(u, v);
}
sz[v] += sz[u];
p[u] = v;
}
void simulate(int x, vector<array<int, 3>> e) {
if (mp.count(x)) {
return;
}
sort(e.begin(), e.end(), [&](array<int, 3>& i, array<int, 3>& j) {
int a = abs(i[2] - x), b = abs(j[2] - x);
//if (x == 4) {
//debug(i, j, a, b);
//}
return a == b ? i[2] > j[2] : a < b;
});
for (int i = 1; i <= n; ++i) {
p[i] = i;
sz[i] = 1;
}
//debug(x);
long long res = 0;
for (int i = 0; i < m; ++i) {
auto [u, v, w] = e[i];
//debug(u, v, w);
if (get(u) != get(v)) {
unite(u, v);
res += (long long) abs(w - x);
mp[x].push_back(w);
}
}
sort(mp[x].begin(), mp[x].end());
assert((int) mp[x].size() == n - 1);
long long sum = 0;
for (int i = 0; i < n - 1; ++i) {
sum += mp[x][i];
ps[{x, i}] = sum;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
vector<array<int, 3>> e(m);
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].push_back(i);
g[v].push_back(i);
e[i] = {u, v, w};
}
for (int i = 1; i <= n; ++i) {
sort(g[i].begin(), g[i].end(), [&](int x, int y) {
return e[x][2] < e[y][2];
});
}
simulate(0, e);
for (int i = 1; i <= n; ++i) {
simulate(e[g[i][0]][2], e);
for (int j = 1; j < (int) g[i].size(); ++j) {
simulate(e[g[i][j]][2], e);
int x = (e[g[i][j - 1]][2] + e[g[i][j]][2] + 1) / 2;
simulate(x, e);
}
}
simulate(1e9, e);
//sort(e.begin(), e.end(), [&](array<int, 3>& i, array<int, 3>& j) {
//return i[2] < j[2];
//});
//simulate(0, e);
//for (int i = 1; i < m; ++i) {
//simulate((e[i - 1][2] + e[i][2]) / 2, e);
//simulate((e[i - 1][2] + e[i][2] + 2) / 2, e);
//}
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
auto [rx, vec] = *prev(mp.upper_bound(x));
auto ind = upper_bound(vec.begin(), vec.end(), x) - vec.begin();
long long ans = 0;
long long pref = 0;
if (ind > 0) {
pref = ps[{rx, ind - 1}];
ans += (long long) (ind) * x - pref;
}
if (ind < n - 1) {
long long suf = ps[{rx, n - 2}] - pref;
ans += suf - (long long) (n - ind - 1) * x;
}
long long res = ans;
//auto tmp = *mp.upper_bound(x);
//debug(rx);
//int rx2 = tmp.first;
//vec = tmp.second;
//ind = upper_bound(vec.begin(), vec.end(), x) - vec.begin();
//ans = 0; pref = 0;
//if (ind > 0) {
//pref = ps[{rx2, ind - 1}];
//ans += (long long) (ind) * x - pref;
//}
//if (ind < n - 1) {
//long long suf = ps[{rx2, n - 2}] - pref;
//ans += suf - (long long) (n - ind - 1) * x;
//}
//res = min(res, ans);
cout << res << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
472 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
472 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
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 |
Execution timed out |
5095 ms |
25780 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
472 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
472 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
472 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |