제출 #1225114

#제출 시각아이디문제언어결과실행 시간메모리
1225114Bui_Quoc_Cuong통행료 (APIO13_toll)C++20
0 / 100
5 ms8512 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX = 3e5 + 5;

int n, m, k;
struct Edges{
    int u, v, w; 
} MST[MAX], SE[MAX];
int c[MAX];

vector <int> g[MAX];
int weight[MAX], par[MAX], h[MAX];
int sz[MAX];
int lab[MAX];

int find(int x){
    return lab[x] < 0 ? x : lab[x] = find(lab[x]);
}

bool joint(int u, int v){
    int x = find(u), y = find(v);
    if(x == y) return false;
    if(lab[x] > lab[y]) swap(x, y);
    lab[x]+= lab[y];
    lab[y] = x;
    return true;
}

void reset_dsu(){
    memset(lab, - 1, sizeof lab);
}

void dfs(int u, int p){
    sz[u] = c[u];
    for(int v : g[u]) if(v != p){
        par[v] = u;
        h[v] = h[u] + 1;
        dfs(v, u);
        sz[u]+= sz[v];
    }
}

void update_edge(int u, int v, int w){
    if(h[u] < h[v]) swap(u, v);
    while(h[u] > h[v]){
        weight[u] = min(weight[u], w);
        u = par[u];
    }
    while(u != v){
        weight[u] = min(weight[u], w);
        weight[v] = min(weight[v], w);
        u = par[u], v = par[v];
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("kieuoanh.inp", "r", stdin);
    freopen("kieuoanh.out", "w", stdout);

    cin >> n >> m >> k;
    vector <array <int, 3>> edges;
    for(int i = 1; i <= m; i++){
        int u, v, w; cin >> u >> v >> w;
        edges.push_back({w, u, v});
    }
    for(int i = 1; i <= k; i++) cin >> SE[i].u >> SE[i].v;
    for(int i = 1; i <= n; i++) cin >> c[i];

    reset_dsu();
    sort(edges.begin(), edges.end());

    int cnt = 0;
    for(array <int, 3> it : edges){
        int u = it[1], v = it[2], w = it[0];
        if(joint(u, v)){
            MST[++ cnt] = {u, v, w};
        }
    }   
    
    ll ans = 0;
    reset_dsu();

    for(int mask = 0; mask < (1 << k); mask++){
        bool ok = 1;
        for(int i = 1; i <= n; i++) g[i].clear(), lab[i] = - 1, weight[i] = 2e9;
        for(int i = 0; i < k; i++) if((mask >> i) & 1){
            if(!joint(SE[i + 1].u, SE[i + 1].v)){
                ok = false;
                break;
            }
            if((mask >> i) & 1){
                g[SE[i + 1].u].push_back(SE[i + 1].v);
                g[SE[i + 1].v].push_back(SE[i + 1].u);
            }
        }
        
        if(!ok) continue;   

        vector <int> in_MST(n + 2);
        for(int i = 1; i <= n - 1; i++){
            if(joint(MST[i].u, MST[i].v)){
                in_MST[i] = 1;
                g[MST[i].u].push_back(MST[i].v);
                g[MST[i].v].push_back(MST[i].u);
            }
        }   

        dfs(1, - 1);
        for(int i = 1; i <= n; i++) if(!in_MST[i]){
            update_edge(MST[i].u, MST[i].v, MST[i].w);
        }

        ll sum = 0;
        for(int i = 0; i < k; i++) if((mask >> i) & 1){
            int u = SE[i + 1].u, v = SE[i + 1].v;
            if(h[u] > h[v]) swap(u, v);
            sum+= 1LL * weight[v] * sz[v];
        }
        ans = max(ans, sum);
    }   
    cout << ans;
    
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

toll.cpp: In function 'int main()':
toll.cpp:60:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |     freopen("kieuoanh.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:61:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |     freopen("kieuoanh.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...