Submission #247033

# Submission time Handle Problem Language Result Execution time Memory
247033 2020-07-10T19:41:59 Z keko37 Toll (APIO13_toll) C++14
0 / 100
2500 ms 512 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long llint;
typedef pair <int, int> pi;

const int MAXN = 5005;

llint n, m, k, sol;
int a[MAXN], b[MAXN], c[MAXN], na[25], nb[25];
int par[MAXN], red[MAXN], dub[MAXN], p[MAXN], ok[MAXN];
llint siz[MAXN], cost[MAXN], mn[MAXN];
vector <int> e, rip;
vector <pi> v[MAXN];

void dsu () {
    for (int i = 1; i <= n; i++) {
        par[i] = i;
    }
}

int nadi (int x) {
    if (x == par[x]) return x;
    return par[x] = nadi(par[x]);
}

inline bool spoji (int x, int y) {
    x = nadi(x); y = nadi(y);
    par[x] = y;
    return x != y;
}

bool cmp (int e1, int e2) {
    return c[e1] < c[e2];
}

void edge_init () {
    sort(red, red + m, cmp);
    dsu();
    for (int i = 0; i < m; i++) {
        if (spoji(a[red[i]], b[red[i]])) e.push_back(red[i]);
    }
}

void dfs (int x, int rod) {
    siz[x] = cost[x];
    dub[x] = 1 + dub[rod];
    p[x] = rod;
    for (auto par : v[x]) {
        int sus = par.first;
        if (sus == rod) {
            if (par.second == 1) ok[x] = 1;
            continue;
        }
        dfs(sus, x);
        siz[x] += siz[sus];
    }
}

void digni (int &x, llint w) {
    mn[x] = min(mn[x], w);
    x = par[x];
}

void lca (int x, int y, llint w) {
    if (dub[x] < dub[y]) swap(x, y);
    while (dub[x] != dub[y]) digni(x, w);
    while (x != y) digni(x, w), digni(y, w);
}

void solve (int mask) {
    dsu();
    for (int i = 1; i <= n; i++) {
        v[i].clear();
        mn[i] = 1e9;
        ok[i] = 0;
    }
    for (int i = 0; i < k; i++) {
        if (mask & (1 << i) && spoji(na[i], nb[i])) {
            v[na[i]].push_back({nb[i], 1});
            v[nb[i]].push_back({na[i], 1});
        }
    }
    rip.clear();
    for (auto i : e) {
        if (spoji(a[i], b[i])) {
            v[a[i]].push_back({b[i], 0});
            v[b[i]].push_back({a[i], 0});
        } else {
            rip.push_back(i);
        }
    }
    dfs(1, 0);
    for (auto i : rip) {
        lca(a[i], b[i], c[i]);
    }
    llint res = 0;
    for (int i = 1; i <= n; i++) {
        if (ok[i]) res += siz[i] * mn[i];
    }
    sol = max(sol, res);
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 0; i < m; i++) {
        cin >> a[i] >> b[i] >> c[i];
        red[i] = i;
    }
    edge_init();
    for (int i = 0; i < k; i++) {
        cin >> na[i] >> nb[i];
    }
    for (int i = 1; i <= n; i++) cin >> cost[i];
    for (int mask = 0; mask < (1 << k); mask++) solve(mask);
    cout << sol;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 2572 ms 512 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2572 ms 512 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2572 ms 512 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2572 ms 512 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2572 ms 512 KB Time limit exceeded
2 Halted 0 ms 0 KB -