Submission #218064

# Submission time Handle Problem Language Result Execution time Memory
218064 2020-03-31T20:13:26 Z osaaateiasavtnl Toll (APIO13_toll) C++14
56 / 100
555 ms 21192 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int INF = 1e9 + 7, N = 1001;
int n, m, k, w[N];
struct Edge {
    int u, v, c;
};  
bool comp(Edge a, Edge b) {
    return a.c < b.c;
}   
int par[N], cost[N], cnt[N];
vector <int> g[N];
int root(int u) {
    if (par[u] == u)
        return u;
    else
        return par[u] = root(par[u]);
}   
bool merge(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v)
        return 0;
    par[u] = v;
    return 1;
}   
int h[N];
void dfs(int u, int p) {
    par[u] = p;
    cnt[u] = w[u];
    for (int v : g[u]) {
        if (v != p) {
            h[v] = h[u] + 1;
            dfs(v, u);
            cnt[u] += cnt[v];                    
        }   
    }
}   
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n >> m >> k;
    vector <Edge> ed;
    for (int i = 0; i < m; ++i) {
        int u, v, c;
        cin >> u >> v >> c;
        ed.app({u, v, c});
    }   
    sort(all(ed), comp);
    vector <ii> add(k);
    for (int i = 0; i < k; ++i)
        cin >> add[i].f >> add[i].s;
    for (int i = 1; i <= n; ++i)
        cin >> w[i];
    int ans = 0;
    for (int mask = 0; mask < (1 << k); ++mask) {
        for (int i = 1; i <= n; ++i) {
            par[i] = i;
            cost[i] = INF;
            g[i].clear();
        }
        bool c = 1;
        for (int i = 0; i < k; ++i) {
            if ((mask >> i) & 1) {
                c &= merge(add[i].f, add[i].s); 
                g[add[i].f].app(add[i].s);
                g[add[i].s].app(add[i].f);
            }   
        }   
        if (!c)
            continue;
        vector <Edge> me;
        for (int i = 0; i < m; ++i) {
            if (root(ed[i].u) == root(ed[i].v)) {
                me.app(ed[i]);
            }   
            else {
                merge(ed[i].u, ed[i].v);
                g[ed[i].u].app(ed[i].v);
                g[ed[i].v].app(ed[i].u);
            }   
        }                   
        dfs(1, 1);
        for (auto e : me) {
            int u = e.u, v = e.v;
            if (h[v] > h[u])
                swap(u, v);
            while (h[u] > h[v]) {
                cost[u] = min(cost[u], e.c);
                u = par[u];
            }   
            while (u != v) {
                cost[u] = min(cost[u], e.c);
                cost[v] = min(cost[v], e.c);
                u = par[u]; v = par[v];
            }   
        }
        int nn = 0;
        for (int i = 0; i < k; ++i) {
            if ((mask >> i) & 1) {
                if (par[add[i].f] == add[i].s)
                    nn += cnt[add[i].f] * cost[add[i].f];
                else
                    nn += cnt[add[i].s] * cost[add[i].s];
            }   
        }   
        ans = max(ans, nn);
    }   
    cout << ans << endl;
}   
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 8 ms 384 KB Output is correct
4 Correct 7 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 8 ms 384 KB Output is correct
4 Correct 7 ms 384 KB Output is correct
5 Correct 555 ms 1004 KB Output is correct
6 Correct 249 ms 904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 8 ms 384 KB Output is correct
4 Correct 7 ms 384 KB Output is correct
5 Correct 555 ms 1004 KB Output is correct
6 Correct 249 ms 904 KB Output is correct
7 Runtime error 169 ms 21192 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 8 ms 384 KB Output is correct
4 Correct 7 ms 384 KB Output is correct
5 Correct 555 ms 1004 KB Output is correct
6 Correct 249 ms 904 KB Output is correct
7 Runtime error 169 ms 21192 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Halted 0 ms 0 KB -