Submission #669845

#TimeUsernameProblemLanguageResultExecution timeMemory
669845ghostwriterCities (BOI16_cities)C++17
100 / 100
3128 ms42436 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include <debug.h> #else #define debug(...) #endif #define ft front #define bk back #define st first #define nd second #define ins insert #define ers erase #define pb push_back #define pf push_front #define _pb pop_back #define _pf pop_front #define lb lower_bound #define ub upper_bound #define mtp make_tuple #define bg begin #define ed end #define all(x) (x).bg(), (x).ed() #define sz(x) (int)(x).size() typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair<int, int> pi; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll; typedef string str; #define FOR(i, l, r) for (int i = (l); i <= (r); ++i) #define FOS(i, r, l) for (int i = (r); i >= (l); --i) #define FRN(i, n) for (int i = 0; i < (n); ++i) #define FSN(i, n) for (int i = (n) - 1; i >= 0; --i) #define EACH(i, x) for (auto &i : (x)) #define WHILE while template<typename T> T gcd(T a, T b) { WHILE(b) { a = a % b; swap(a, b); } return a; } template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } #define file "TEST" mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); } /* ---------------------------------------------------------------- END OF TEMPLATE ---------------------------------------------------------------- Tran The Bao - ghostwriter Training for VOI23 gold medal ---------------------------------------------------------------- GOAT ---------------------------------------------------------------- */ struct Edge { int u, v, w; Edge() {} Edge(int u, int v, int w) : u(u), v(v), w(w) {} }; const ll oo = 1e18; const int M = 2e5 + 5; const int N = 1e5 + 5; const int K = 10; int n, k, m, a[K]; ll d[N][1 << 5], rs = oo; vpi adj[N]; priority_queue<pll, vpll, greater<pll> > pq; void input() { cin >> n >> k >> m; FOR(i, 1, k) cin >> a[i]; FOR(i, 1, m) { int u, v, w; cin >> u >> v >> w; adj[u].pb({v, w}); adj[v].pb({u, w}); } } void solve() { FOR(j, 1, (1 << k) - 1) { int bc = __builtin_popcount(j), pos = __builtin_ctz(j & -j) + 1; FOR(i, 1, n) { d[i][j] = oo; if (bc == 1 && i == a[pos]) d[i][j] = 0; if (bc > 1) { for (int z = (j - 1) & j; z > 0; z = (z - 1) & j) EACH(k, adj[i]) { int v = k.st, w = k.nd; d[i][j] = min(d[i][j], d[v][z] + d[i][j ^ z] + w); } } } FOR(i, 1, n) pq.push({d[i][j], i}); WHILE(!pq.empty()) { int u = pq.top().nd; ll du = pq.top().st; pq.pop(); if (du > d[u][j]) continue; EACH(z, adj[u]) { int v = z.st, w = z.nd; if (d[v][j] > du + w) { d[v][j] = du + w; pq.push({d[v][j], v}); } } } } FOR(i, 1, n) rs = min(rs, d[i][(1 << k) - 1]); cout << rs; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); // freopen(file".inp", "r", stdin); // freopen(file".out", "w", stdout); input(); solve(); return 0; } /* ---------------------------------------------------------------- From Benq: stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH ---------------------------------------------------------------- */
#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...