Submission #1082017

# Submission time Handle Problem Language Result Execution time Memory
1082017 2024-08-30T14:31:32 Z TrinhKhanhDung Cities (BOI16_cities) C++14
37 / 100
6000 ms 48332 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(5) + 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; 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>> 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 15 ms 35672 KB Output is correct
2 Correct 14 ms 35676 KB Output is correct
3 Correct 13 ms 35668 KB Output is correct
4 Correct 13 ms 35676 KB Output is correct
5 Correct 17 ms 35672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 6041 ms 48332 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 35676 KB Output is correct
2 Correct 30 ms 35660 KB Output is correct
3 Correct 41 ms 35584 KB Output is correct
4 Correct 38 ms 35676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 6069 ms 48128 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6086 ms 48332 KB Time limit exceeded
2 Halted 0 ms 0 KB -