답안 #257527

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
257527 2020-08-04T11:32:56 Z davooddkareshki 통행료 (APIO13_toll) C++17
16 / 100
5 ms 3456 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define F first 
#define S second 
#define pii pair<int,int> 
#define mpr make_pair

typedef long long ll;

const int maxn = 1e5+10;
const int mod = 1e9+7;
const ll inf = 1e18+10;

int n, m, k;

struct dsu
{
        int par[maxn];
        void f_reset() {memset(par, -1, sizeof par);}
        void reset() {fill(par, par+27, -1);}

        int get_par(int v)
        {
                if(par[v] < 0) return v;
                return par[v] = get_par(par[v]);
        }

        void add(int u, int v)
        {
                u = get_par(u);
                v = get_par(v);

                if(u == v) return;
                if(par[u] < par[v]) swap(u,v);
                par[v] += par[u];
                par[u] = v;
        }

        bool ask(int u, int v) {return (get_par(u) == get_par(v));}
} dsu;

vector<int> T[maxn];
int st[maxn], sz[maxn], comp[maxn], p[maxn], mark[maxn], num;

void dfs(int v)
{
        mark[v] = 1;
        comp[v] = num; sz[num] += p[v];
        for(auto u : T[v])
                if(!mark[u]) dfs(u);
}

vector<int> Tree[25];
int pa[maxn], W[maxn], stt[maxn];
void dfs_T(int v)
{
        W[v] = inf;
        stt[v] = sz[v];
        for(auto u : Tree[v])
                if(u != pa[v])
                {
                        pa[u] = v;
                        dfs_T(u);
                        stt[v] += stt[u];
                }
}

ll ans = 0;
void reset()
{
        ans = 0;
        dsu.reset();
        for(int i = 0; i <= 25; i++)
        {
                Tree[i].clear();
                W[i] = inf;
                pa[i] = 0;
                stt[i] = 0;
        }
}

signed main()
{
        //ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);    

        cin>> n >> m >> k;
        vector<pair<int,pii>> edge, Bedge; vector<pii> Redge;
        for(int i = 1, u, v, c; i <= m; i++)
        {
                cin>> u >> v >> c;
                edge.push_back({c,{u,v}});
        }
        sort(edge.begin(), edge.end());

        dsu.f_reset();
        for(int i = 1, u, v; i <= k; i++)
        {
                cin>> u >> v;
                Redge.push_back({u,v});
                dsu.add(u,v);
        }

        for(auto e : edge)
        {
                int u = e.S.F, v = e.S.S, w = e.F;
                if(!dsu.ask(u,v))
                {
                        T[u].push_back(v);
                        T[v].push_back(u);
                        dsu.add(u,v);
                }
                else Bedge.push_back(e);
                      }
        for(int i = 1; i <= n; i++) cin>> p[i];


        // comp[Root = 1] = 1
        for(int i = 1; i <= n; i++)
                if(!mark[i])
                {
                        num++;
                        dfs(i);
                }

        dsu.f_reset();
        ll last_ans = 0;
        for(int msk = 0; msk < (1<<k); msk++)
        {
                // in k ta dori nabashano ham bayad check koni

                reset();
                bool sw = 0;
                for(int i = 0; i < k; i++)
                        if((1<<i) & msk)
                        {
                                int u = comp[Redge[i].F], v = comp[Redge[i].S];
                                if(dsu.ask(u,v)) {sw = 1; break;}

                                dsu.add(u,v);
                                Tree[u].push_back(v);
                                Tree[v].push_back(u);
                        }
                if(sw) continue;

                for(auto e : Bedge)
                {
                        int u = comp[e.S.F], v = comp[e.S.S];
                        if(!dsu.ask(u,v))
                        {
                                dsu.add(u,v);
                                Tree[u].push_back(v);
                                Tree[v].push_back(u);
                        }
                }

                dfs_T(1);

                for(auto e : Bedge)
                {
                        int w = e.F, u = comp[e.S.F], v = comp[e.S.S];
                        bool seen[k+2];
                        memset(seen, 0, sizeof seen);
                        while(u != 1)
                        {
                                (seen[u] ^= 1);
                                u = pa[u];
                        }
                        while(v != 1)
                        {
                                (seen[v] ^= 1);
                                v = pa[v];
                        }
                        for(int i = 1; i <= k+1; i++)
                                if(seen[i]) W[i] = min(W[i],w);

                        if(u == pa[v]) W[v] = 0;
                        if(v == pa[u]) W[u] = 0;
                }

                for(int i = 2; i <= num; i++) ans += stt[i] * W[i];
                last_ans = max(last_ans, ans);
        }

        cout<< last_ans;
}
/*
5 5 2
1 5 1
1 2 2
2 3 2
3 4 1
4 5 2
2 4
2 5
1 1 1 1 1
*/


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:108:43: warning: unused variable 'w' [-Wunused-variable]
                 int u = e.S.F, v = e.S.S, w = e.F;
                                           ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Incorrect 5 ms 3456 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Incorrect 5 ms 3456 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Incorrect 5 ms 3456 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Incorrect 5 ms 3456 KB Output isn't correct
4 Halted 0 ms 0 KB -