Submission #1039239

#TimeUsernameProblemLanguageResultExecution timeMemory
1039239WhisperCities (BOI16_cities)C++17
100 / 100
2588 ms49584 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int long long #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (b); i >= (a); i --) #define REP(i, a) for (int i = 0; i < (a); ++i) #define REPD(i, a) for (int i = (a) - 1; i >= 0; --i) #define MASK(i) (1LL << (i)) #define BIT(x, i) (((x) >> (i)) & 1) constexpr ll LINF = (1ll << 60); constexpr int INF = (1ll << 30); constexpr int Mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); /* Phu Trong from Nguyen Tat Thanh High School for gifted student */ template <class X, class Y> bool minimize(X &x, const Y &y){ X eps = 1e-9; if (x > y + eps) {x = y; return 1;} return 0; } template <class X, class Y> bool maximize(X &x, const Y &y){ X eps = 1e-9; if (x + eps < y) {x = y; return 1;} return 0; } #define MAX_NODE 100005 #define MAX_EDGE 200005 int numNode, numCity, numEdge; struct Edge{ int u, v, w; Edge(){} Edge(int _u, int _v, int _w): u(_u), v(_v), w(_w){} int other(int x){return (x ^ u ^ v);} } edge[MAX_EDGE]; vector<int> G[MAX_NODE]; bool good[MAX_NODE]; int dp[MAX_NODE][MASK(5)]; int id[MAX_NODE]; int cnt = 0; void upd(int x){ priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; for (int i = 1; i <= numNode; ++i){ q.emplace(dp[i][x], i); } while(q.size()){ int du, u; tie(du, u) = q.top(); q.pop(); if (du > dp[u][x]) continue; for (int&i : G[u]){ int v = edge[i].other(u); if(minimize(dp[v][x], dp[u][x] + edge[i].w)){ q.emplace(dp[v][x], v); } } } } void process(void){ cin >> numNode >> numCity >> numEdge; for (int i = 1; i <= numCity; ++i){ int u; cin >> u; good[u] = true; id[u] = cnt++; } for (int i = 1; i <= numEdge; ++i){ cin >> edge[i].u >> edge[i].v >> edge[i].w; G[edge[i].u].emplace_back(i); G[edge[i].v].emplace_back(i); } memset(dp, 0x3f, sizeof dp); for (int i = 1; i <= numNode; ++i) { if (good[i]){ dp[i][MASK(id[i])] = 0; } } for (int msk = 0; msk < MASK(numCity); ++msk){ for (int sub_msk = msk; sub_msk > 0; sub_msk = (msk & (sub_msk - 1))){ for (int i = 1; i <= numNode; ++i){ minimize(dp[i][msk], dp[i][msk ^ sub_msk] + dp[i][sub_msk]); } } upd(msk); } int ans = LINF; for (int i = 1; i <= numNode; ++i) minimize(ans, dp[i][MASK(numCity) - 1]); cout << ans; } signed main(){ #define name "Whisper" cin.tie(nullptr) -> sync_with_stdio(false); //freopen(name".inp", "r", stdin); //freopen(name".out", "w", stdout); process(); return (0 ^ 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...