Submission #881265

#TimeUsernameProblemLanguageResultExecution timeMemory
881265RegulusReconstruction Project (JOI22_reconstruction)C++17
7 / 100
5058 ms8900 KiB
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using ppl = pair<pll, ll>;
using plp = pair<ll, pll>;

const int N = 505;
const int MxE = 1e5+5;
ll x[MxE], y[MxE], w[MxE], dsu[N];
vector<plp> v;

inline int query(int x) { return (dsu[x]<0)?x : dsu[x]=query(dsu[x]); }

int main(void)
{ IO
    ll n, i, m, Q, ans, j, want;

    cin >> n >> m;
    for (i=1; i <= m; ++i)
        cin >> x[i] >> y[i] >> w[i];
    cin >> Q;
    for (i=1; i <= Q; ++i)
    {
        cin >> want;
        for (j=1; j <= n; ++j) dsu[j] = -1;
        v.clear();
        for (j=1; j <= m; ++j)
            v.pb(abs(want-w[j]), pll{x[j], y[j]});
        sort(all(v));
        ans = 0;
        for (auto e : v)
        {
            int x = e.S.F, y = e.S.S;
            if (query(x) != query(y))
                ans += e.F, dsu[query(x)] = query(y);
        }
        cout << ans << '\n';
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...