Submission #1082020

#TimeUsernameProblemLanguageResultExecution timeMemory
1082020TrinhKhanhDungCities (BOI16_cities)C++14
100 / 100
1553 ms77776 KiB
#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 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...