답안 #977733

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
977733 2024-05-08T10:13:05 Z steveonalex 통행료 (APIO13_toll) C++17
16 / 100
1 ms 348 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)

// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}

ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}

ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}

template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

struct DSU{
    int n;
    vector<int> parent, sz;

    DSU(int _n){
        n = _n;
        parent.resize(n+1); sz.resize(n+1, 1);
        for(int i = 1; i<=n; ++i) parent[i] = i;
    }

    int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));}
    bool same_set(int u, int v){return find_set(u) == find_set(v);}
    bool join_set(int u, int v){
        u = find_set(u), v = find_set(v);
        if (u != v){
            if (sz[u] < sz[v]) swap(u, v);
            parent[v] = u;
            sz[u] += sz[v];
            return true;
        }
        return false;
    }
};

const int N = 1e3 + 69, LOG_N = 10;

int n, m, k;
vector<array<int, 3>> path, useful;
vector<pair<int, int>> money;
vector<pair<int, int>> graph[N];
int parent[LOG_N][N];
int max_path[LOG_N][N];
int h[N];
int goon[N];

void dfs(int u, int p){
    h[u] = h[p] + 1;
    for(int j = 1; MASK(j) < h[u]; ++j){
        int prev = parent[j-1][u];
        parent[j][u] = parent[j-1][prev];
        max_path[j][u] = max(max_path[j-1][u], max_path[j-1][prev]);
    }

    for(pair<int, int> v: graph[u]) if (v.first != p){
        parent[0][v.first] = u;
        max_path[0][v.first] = v.second;
        dfs(v.first, u);
    }
}

int LCA(int u, int v){
    if (h[u] < h[v]) swap(u, v);
    int diff = h[u] - h[v];
    for(int j = 0; j < LOG_N; ++j) if (GETBIT(diff, j))
        u = parent[j][u];
    if (u == v) return u;
    for(int j = LOG_N - 1; j>=0; --j) if (parent[j][u] != parent[j][v]){
        u = parent[j][u];
        v = parent[j][u];
    }
    return parent[0][u];
}

int get_max_path(int u, int v){
    int lck = LCA(u, v);
    int diff = h[u] - h[lck];
    int ans = 0;
    for(int j = 0; j< LOG_N; ++j) if (GETBIT(diff, j)) {
        maximize(ans, max_path[j][u]);
        u = parent[j][u];
    }
    diff = h[v] - h[lck];
    for(int j = 0; j<LOG_N; ++j) if (GETBIT(diff, j)){
        maximize(ans, max_path[j][v]);
        v = parent[j][v];
    }
    return ans;
}

namespace SimulatedAnnealing{
    vector<pair<int, int>> graph[N];
    ll chode[N];
    ll ans;
    void reset(){
        for(int i = 1; i<=n; ++i){
            graph[i].clear();
        }
    }

    void dfs(int u, int p){
        chode[u] = goon[u];
        for(pair<int, int> v: graph[u]) if (v.first != p){
            dfs(v.first, u);
            chode[u] += chode[v.first];
            ans += chode[v.first] * v.second;
        }
    }

    ll solve(DSU &mst){
        ans = 0;
        for(array<int, 3> i: useful){
            int u = i[0], v = i[1];
            if (mst.join_set(u, v)){
                graph[u].push_back({v, 0});
                graph[v].push_back({u, 0});
            }
        }
        dfs(1, 1);
        return ans;
    }
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> m >> k;
    path.resize(m);
    for(int i = 0; i<m; ++i){
        for(int j = 0;j < 3; ++j) cin >> path[i][j];
    }
    sort(ALL(path), [] (array<int, 3> x, array<int, 3> y){return x[2] < y[2];});

    DSU mst(n);
    for(int i = 0; i<m; ++i){
        int u = path[i][0], v = path[i][1], w = path[i][2];
        if (mst.join_set(u, v)){
            useful.push_back(path[i]);
            graph[u].push_back({v, w});
            graph[v].push_back({u, w});
        }
    }

    dfs(1, 1);

    money.resize(k);
    for(int i = 0; i<k; ++i) cin >> money[i].first >> money[i].second;

    for(int i = 1; i<=n; ++i) cin >> goon[i];

    ll ans = 0;
    for(int mask = 1; mask < MASK(k); ++mask){
        DSU mst(n);
        bool check = true;
        for(int i = 0; i<k; ++i) if (GETBIT(mask, i)){
            check = check && mst.join_set(money[i].first, money[i].second);
        }
        if (check == false) continue;

        SimulatedAnnealing::reset();
        for(int i=0; i<k; ++i) if (GETBIT(mask, i)){    
            int u = money[i].first, v = money[i].second, w = get_max_path(u, v);
            SimulatedAnnealing::graph[u].push_back({v, w});
            SimulatedAnnealing::graph[v].push_back({u, w});
        }
        maximize(ans, SimulatedAnnealing::solve(mst));
    }
    cout << ans << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -