답안 #726325

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
726325 2023-04-18T18:00:37 Z vjudge1 통행료 (APIO13_toll) C++17
78 / 100
2500 ms 10624 KB
#pragma GCC optimize("O3")
#pragma GCC optimize("O1")
#pragma GCC optimize("O1")
#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")

#pragma GCC target("avx")
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,mmx,abm")

#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];
int64_t p[100000], pp[100000];
int par[100000];
vector<pair<int, int>> adj[100000];

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;

int64_t dfs(int u, int pr) {
        int64_t res = 0;
        for (pair<int, int>& e : adj[u]) {
                if (e.first == pr) continue;
                int64_t dpv = dfs(e.first, u);
                res += dpv;
                ans += 1ll * dpv * e.second;
        }
        return res + p[u];
}

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) {
                                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);
                        }
                }

                for (int j = 0; j < k; j++) {
                        memset(par, -1, N * sizeof *par);
                        for (int j = 0; j < N; j++) p[j] = pp[j];
                        for (int j : mst) unite(a[j], b[j]);
                        if (i >> j & 1) {
                                for (int z = 0; z < k; z++) {
                                        if ((i >> z & 1) && j != z) unite(x[z], y[z]);
                                }
                        }
                        cost[j] = 1e9;
                        for (int z : not_mst) {
                                if (!is_connected(a[z], b[z])) cost[j] = min(cost[j], c[z]);
                        }
                }

                memset(par, -1, N * sizeof *par);
                for (int j = 0; j < N; j++) p[j] = pp[j];
                for (int j : mst) unite(a[j], b[j]);

                for (int j = 0; j < k; j++) {
                        if (i >> j & 1) {
                                adj[root(x[j])].emplace_back(root(y[j]), cost[j]);
                                adj[root(y[j])].emplace_back(root(x[j]), cost[j]);
                        }
                }

                ans = 0;
                dfs(root(ROOT), -1);
                res = max(res, ans);

                for (int j = 0; j < k; j++) {
                        if (i >> j & 1) {
                                adj[root(x[j])].pop_back();
                                adj[root(y[j])].pop_back();
                        }
                }
        }

        cout << res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 4 ms 2644 KB Output is correct
4 Correct 4 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 4 ms 2644 KB Output is correct
4 Correct 4 ms 2644 KB Output is correct
5 Correct 6 ms 2772 KB Output is correct
6 Correct 5 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 4 ms 2644 KB Output is correct
4 Correct 4 ms 2644 KB Output is correct
5 Correct 6 ms 2772 KB Output is correct
6 Correct 5 ms 2772 KB Output is correct
7 Correct 277 ms 10444 KB Output is correct
8 Correct 298 ms 10624 KB Output is correct
9 Correct 312 ms 10600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 4 ms 2644 KB Output is correct
4 Correct 4 ms 2644 KB Output is correct
5 Correct 6 ms 2772 KB Output is correct
6 Correct 5 ms 2772 KB Output is correct
7 Correct 277 ms 10444 KB Output is correct
8 Correct 298 ms 10624 KB Output is correct
9 Correct 312 ms 10600 KB Output is correct
10 Execution timed out 2563 ms 10568 KB Time limit exceeded
11 Halted 0 ms 0 KB -