답안 #222024

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
222024 2020-04-11T22:07:11 Z Bruteforceman 통행료 (APIO13_toll) C++11
56 / 100
2500 ms 19340 KB
#include <bits/stdc++.h>
using namespace std;
using pii = pair <int, int>;
const int inf = 1000000001;
struct edge {
    int l, r, cost;
    edge () {}
    edge (int l, int r, int cost) : l(l), r(r), cost(cost) {}
    bool operator < (edge e) const {
        return cost < e.cost;
    }
} e[300010], sp[30];
int p[100010];
int val[100010];

int n, m, k;

vector <pii> g[100010];
int sub[100010];
long long total;
int par[100010];
int sz[100010];
int root(int x) {
    if(x == par[x]) return par[x];
    return par[x] = root(par[x]);
}
void join(int x, int y) {
    x = root(x);
    y = root(y);
    if(x != y) {
        if(sz[x] > sz[y]) swap(x, y);
        sz[y] += sz[x];
        par[x] = y;
    }
}

void dfs(int x, int par) {
    sub[x] = p[x];
    for(auto y : g[x]) {
        int i = y.first;
        if(i != par) {
            dfs(i, x);
            sub[x] += sub[i];
            total += 1LL * (y.second == inf) * val[i] * sub[i];
        }
    }
}
long long solve(vector <int> take) {
    for(int i = 1; i <= n; i++) {
        par[i] = i;
        sz[i] = 1;
        g[i].clear();
    }
    auto add_edge = [&] (int i, int j, int type) {
        g[i].emplace_back(j, type);
        g[j].emplace_back(i, type);
    };
    for(int i : take) {
        int l, r;
        tie(l, r) = make_pair(sp[i].l, sp[i].r);
        if(root(l) == root(r)) return 0;
        join(l, r);
        add_edge(l, r, inf);
    }
    for(int i = 0; i < m; i++) {
        int l = e[i].l;
        int r = e[i].r;
        if(root(l) != root(r)) {
            join(l, r);
            add_edge(l, r, e[i].cost);
        }
    }
    vector <int> dad (n + 1, 0);
    vector <int> dep (n + 1, 0);
    function <void(int, int)> getPar = [&] (int x, int prev) {
        dad[x] = prev;
        for(auto y : g[x]) {
            int i = y.first;
            if(i != prev) {
                dep[i] = 1 + dep[x];
                getPar(i, x);
            }
        }
    };
    getPar(1, 0);
    for(int i = 1; i <= n; i++) {
        val[i] = inf;
    }
    for(int i = 0; i < m; i++) {
        int l, r;
        tie(l, r) = make_pair(e[i].l, e[i].r);
        if(dep[l] > dep[r]) swap(l, r);
        while(dep[l] < dep[r]) {
            val[r] = min(val[r], e[i].cost);
            r = dad[r];
        }
        while(l != r) {
            val[l] = min(val[l], e[i].cost);
            val[r] = min(val[r], e[i].cost);
            l = dad[l]; r = dad[r];
        }
    }
    total = 0;
    dfs(1, 0);
    return total;
}

int main() {
    scanf("%d %d %d", &n, &m, &k);
    for(int i = 0; i < m; i++) {
        scanf("%d %d %d", &e[i].l, &e[i].r, &e[i].cost);
    }
    sort(e, e + m);
    for(int i = 0; i < k; i++) {
        scanf("%d %d", &sp[i].l, &sp[i].r);
    }
    for(int i = 1; i <= n; i++) {
        scanf("%d", &p[i]);
    }
    for(int i = 1; i <= n; i++) {
        par[i] = i;
        sz[i] = 1;
    }
    for(int i = 0; i < m; i++) {
        int l, r;
        tie(l, r) = make_pair(e[i].l, e[i].r);
        if(root(l) != root(r)) {
            join(l, r);
            g[l].emplace_back(r, e[i].cost);
            g[r].emplace_back(l, e[i].cost);
        }
    }
    long long ans = 0;
    for(int i = 1; i < (1 << k); i++) {
        vector <int> take;
        for(int j = 0; j < k; j++) {
            if((i >> j) & 1) {
                take.push_back(j);
            }
        }
        ans = max(ans, solve(take));
    }
    printf("%lld\n", ans);
    return 0;
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:109:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d", &n, &m, &k);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:111:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &e[i].l, &e[i].r, &e[i].cost);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:115:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &sp[i].l, &sp[i].r);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &p[i]);
         ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 9 ms 2560 KB Output is correct
4 Correct 9 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 9 ms 2560 KB Output is correct
4 Correct 9 ms 2688 KB Output is correct
5 Correct 520 ms 2932 KB Output is correct
6 Correct 236 ms 2944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 9 ms 2560 KB Output is correct
4 Correct 9 ms 2688 KB Output is correct
5 Correct 520 ms 2932 KB Output is correct
6 Correct 236 ms 2944 KB Output is correct
7 Execution timed out 2555 ms 19340 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 9 ms 2560 KB Output is correct
4 Correct 9 ms 2688 KB Output is correct
5 Correct 520 ms 2932 KB Output is correct
6 Correct 236 ms 2944 KB Output is correct
7 Execution timed out 2555 ms 19340 KB Time limit exceeded
8 Halted 0 ms 0 KB -