답안 #726343

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
726343 2023-04-18T18:36:16 Z vjudge1 통행료 (APIO13_toll) C++17
0 / 100
0 ms 340 KB
#include <bits/stdc++.h>
using namespace std;

int n, m, k;
int a[300000], b[300000], c[300000], ord[300000], cnt[300000];
int x[20], y[20], cost[20];
int id[100000];
int pa[40], up[40], depth[40];
int64_t p[100000], pp[100000];
int par[100000];
vector<pair<int, int>> adj[40];

inline int root(int u) { return par[u] < 0 ? u : par[u] = root(par[u]); }

inline bool is_connected(int u, int v) { return root(u) == root(v); }

inline bool unite(int u, int v) {
        u = root(u), v = root(v);
        if (u == v) return 0;
        if (par[u] > par[v]) swap(u, v);
        p[u] += p[v];
        par[u] += par[v];
        par[v] = u;
        return 1;
}

int64_t ans;

int main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cin >> n >> m >> k;
        for (int i = 0; i < m; i++) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--;
        for (int i = 0; i < k; i++) cin >> x[i] >> y[i], x[i]--, y[i]--;
        for (int i = 0; i < n; i++) cin >> pp[i];

        memset(par, -1, n * sizeof *par);
        iota(ord, ord + m, 0);
        sort(ord, ord + m, [&](int a, int b) {
                return c[a] < c[b];
        });
        for (int j = 0; j < m; j++) {
                cnt[ord[j]] += unite(a[ord[j]], b[ord[j]]);
        }  // spanning tree with 0 new edge

        memset(par, -1, n * sizeof *par);
        for (int i = 0; i < k; i++) {
                unite(x[i], y[i]);
        }
        for (int j = 0; j < m; j++) {
                cnt[ord[j]] += unite(a[ord[j]], b[ord[j]]);
        }  // spanning tree with k new edges

        vector<int> almost_mst;
        memset(par, -1, n * sizeof *par);
        for (int i = 0; i < n; i++) p[i] = pp[i];
        for (int i = 0; i < m; i++) {
                if (cnt[ord[i]] == 1) almost_mst.emplace_back(ord[i]);
                if (cnt[ord[i]] == 2) unite(a[ord[i]], b[ord[i]]);
        }

        int N = 0;
        for (int i = 0; i < n; i++) {
                if (root(i) == i) id[i] = N, pp[N++] = p[i];
        }
        for (int j = 0; j < k; j++) x[j] = id[root(x[j])];
        for (int j = 0; j < k; j++) y[j] = id[root(y[j])];
        for (int j : almost_mst) a[j] = id[root(a[j])];
        for (int j : almost_mst) b[j] = id[root(b[j])];

        const int ROOT = id[root(0)];

        int64_t res = 0;

        for (int i = 1; i < 1 << k; i++) {
                memset(par, -1, N * sizeof *par);
                for (int j = 0; j < N; j++) p[j] = pp[j];
                bool ok = 1;
                for (int j = 0; j < k; j++) {
                        if (i >> j & 1) {
                                cost[j] = 1e9;
                                if (!unite(x[j], y[j])) ok = 0;
                        }
                }
                if (!ok) continue;
                vector<int> not_mst, mst;

                for (int j : almost_mst) {
                        if (!unite(a[j], b[j])) {
                                not_mst.emplace_back(j);
                        } else {
                                mst.emplace_back(j);
                                adj[a[j]].emplace_back(b[j], -1);
                                adj[b[j]].emplace_back(a[j], -1);
                        }
                }

                for (int j = 0; j < k; j++) adj[x[j]].emplace_back(y[j], j);
                for (int j = 0; j < k; j++) adj[y[j]].emplace_back(x[j], j);

                int64_t ans = 0;

                function<void(int, int)> dfs = [&](int u, int pr) {
                        pa[u] = pr;
                        pp[u] = p[u];
                        for (pair<int, int>& e : adj[u]) {
                                if (e.first == pr) continue;
                                up[e.first] = e.second;
                                depth[e.first] = depth[u] + 1;
                                dfs(e.first, u);
                                pp[u] += pp[e.first];
                                if (e.second != -1) ans += 1ll * cost[e.second] * pp[e.first];
                        }
                };

                depth[root(ROOT)] = 0;
                dfs(root(ROOT), -1);

                for (int j : not_mst) {
                        int u = a[j], v = b[j];
                        if (depth[u] < depth[v]) swap(u, v);
                        while (depth[u] > depth[v]) {
                                int id = up[u];
                                if (id != -1) cost[id] = min(cost[id], c[j]);
                                u = pa[u];
                        }
                        while (u != v) {
                                int id;
                                id = up[u];
                                if (id != -1) cost[id] = min(cost[id], c[j]);
                                u = pa[u];
                                id = up[v];
                                if (id != -1) cost[id] = min(cost[id], c[j]);
                                v = pa[v];
                        }
                }
                ans = 0;
                dfs(root(ROOT), -1);
                for (int j = 0; j < N; j++) vector<pair<int, int>>().swap(adj[j]);
                res = max(res, ans);
        }

        cout << res;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -