This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const int maxn = 510, maxm = 1e5 + 10, maxq = 1e6 + 10;
const ll inf = 1e18;
struct edge
{
int v, u;
ll w;
edge(int _v = 0, int _u = 0, ll _w = 0)
{
v = _v;
u = _u;
w = _w;
}
bool operator < (const edge &e) const
{
return w < e.w;
}
}edges[maxm];
int n, m, q;
ll ans[maxq];
struct query
{
int idx;
ll x;
bool operator < (const query &q1) const
{
return x < q1.x;
}
}task[maxq];
void input()
{
cin >> n >> m;
for (int i = 1; i <= m; i ++)
{
cin >> edges[i].v >> edges[i].u >> edges[i].w;
}
cin >> q;
for (int i = 1; i <= q; i ++)
{
cin >> task[i].x;
task[i].idx = i;
}
sort(edges + 1, edges + m + 1);
sort(task + 1, task + q + 1);
}
int par[maxn];
int find_leader(int v)
{
if (v == par[v])
return v;
return (par[v] = find_leader(par[v]));
}
bool unite(int v, int u)
{
v = find_leader(v);
u = find_leader(u);
if (v == u)
return false;
par[v] = u;
return true;
}
void answer()
{
int pt = 1;
for (int i = 1; i <= q; i ++)
{
while(pt <= m && edges[pt].w < task[i].x)
pt ++;
pt --;
int lf = pt, rf = pt + 1;
for (int j = 1; j <= n; j ++)
par[j] = j;
int cnt = 0;
ll sum = 0;
while(cnt < n - 1)
{
ll w_lf = inf, w_rf = inf;
if (lf > 0) w_lf = task[i].x - edges[lf].w;
if (rf <= m) w_rf = edges[rf].w - task[i].x;
if (w_lf < w_rf)
{
bool tie = unite(edges[lf].v, edges[lf].u);
if (tie)
{
sum = sum + task[i].x - edges[lf].w;
cnt ++;
}
lf --;
}
else
{
bool tie = unite(edges[rf].v, edges[rf].u);
if (tie)
{
sum = sum + edges[rf].w - task[i].x;
cnt ++;
}
rf ++;
}
}
ans[task[i].idx] = sum;
}
for (int i = 1; i <= q; i ++)
cout << ans[i] << endl;
}
void solve()
{
input();
answer();
}
int main()
{
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |