Submission #1082020

# Submission time Handle Problem Language Result Execution time Memory
1082020 2024-08-30T14:42:48 Z TrinhKhanhDung Cities (BOI16_cities) C++14
100 / 100
1553 ms 77776 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(998244353)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

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>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

const int MAX = 1e5 + 10;

int N, M, K;
vector<int> vertex;
vector<pair<int, int>> adj[MAX];
ll f[MASK(6) + 10][MAX];

void solve(){
    cin >> N >> K >> M;
    for(int i=1; i<=K; i++){
        int u; cin >> u;
        vertex.push_back(u);
    }
    for(int i=1; i<=M; i++){
        int u, v, c;
        cin >> u >> v >> c;
        adj[u].push_back({v, c});
        adj[v].push_back({u, c});
    }
    memset(f, 0x3f, sizeof f);
    for(int i=0; i<K; i++){
        f[MASK(i)][vertex[i]] = 0;
    }
    for(int mask=1; mask<MASK(K); mask++){
        for(int submask=mask&(mask-1); submask>0; submask=mask&(submask - 1)){
            for(int i=1; i<=N; i++){
                minimize(f[mask][i], f[submask][i] + f[mask ^ submask][i]);
            }
        }
        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
        for(int i=1; i<=N; i++){
            pq.push({f[mask][i], i});
        }
        while(sz(pq)){
            int u = pq.top().se;
            ll kc = pq.top().fi;
            pq.pop();
            if(kc > f[mask][u]) continue;
            for(auto o: adj[u]){
                int v = o.fi;
                ll newkc = kc + o.se;
                if(minimize(f[mask][v], newkc)){
                    pq.push({newkc, v});
                }
            }
        }
    }
    ll ans = oo;
    for(int i=1; i<=N; i++){
        minimize(ans, f[MASK(K) - 1][i]);
    }
    cout << ans << '\n';
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    // freopen("flowers.inp", "r", stdin);
    // freopen("flowers.out", "w", stdout);

    int nTest = 1;

    while(nTest--)
        solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 27 ms 60504 KB Output is correct
2 Correct 25 ms 60500 KB Output is correct
3 Correct 24 ms 60504 KB Output is correct
4 Correct 26 ms 60556 KB Output is correct
5 Correct 32 ms 60612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 387 ms 77636 KB Output is correct
2 Correct 373 ms 77748 KB Output is correct
3 Correct 261 ms 69556 KB Output is correct
4 Correct 64 ms 68852 KB Output is correct
5 Correct 215 ms 77672 KB Output is correct
6 Correct 71 ms 68860 KB Output is correct
7 Correct 28 ms 60904 KB Output is correct
8 Correct 27 ms 60700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 60752 KB Output is correct
2 Correct 41 ms 60856 KB Output is correct
3 Correct 36 ms 60660 KB Output is correct
4 Correct 25 ms 60760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 748 ms 77776 KB Output is correct
2 Correct 753 ms 77700 KB Output is correct
3 Correct 451 ms 69592 KB Output is correct
4 Correct 457 ms 74288 KB Output is correct
5 Correct 134 ms 70932 KB Output is correct
6 Correct 71 ms 70564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1553 ms 77692 KB Output is correct
2 Correct 1495 ms 77724 KB Output is correct
3 Correct 1408 ms 77676 KB Output is correct
4 Correct 926 ms 69612 KB Output is correct
5 Correct 774 ms 74304 KB Output is correct
6 Correct 207 ms 70944 KB Output is correct
7 Correct 79 ms 70560 KB Output is correct