Submission #669846

# Submission time Handle Problem Language Result Execution time Memory
669846 2022-12-07T13:13:19 Z ghostwriter Cities (BOI16_cities) C++17
100 / 100
3006 ms 38180 KB
#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 = 1e15;
const int N = 1e5 + 1;
const int K = 6;
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 time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 667 ms 38092 KB Output is correct
2 Correct 645 ms 37716 KB Output is correct
3 Correct 445 ms 32960 KB Output is correct
4 Correct 63 ms 7500 KB Output is correct
5 Correct 312 ms 38076 KB Output is correct
6 Correct 61 ms 7588 KB Output is correct
7 Correct 4 ms 3028 KB Output is correct
8 Correct 3 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3028 KB Output is correct
2 Correct 7 ms 3060 KB Output is correct
3 Correct 5 ms 2900 KB Output is correct
4 Correct 4 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1449 ms 38124 KB Output is correct
2 Correct 1431 ms 37788 KB Output is correct
3 Correct 1002 ms 32960 KB Output is correct
4 Correct 809 ms 23040 KB Output is correct
5 Correct 184 ms 10944 KB Output is correct
6 Correct 97 ms 9244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2973 ms 38104 KB Output is correct
2 Correct 3006 ms 38180 KB Output is correct
3 Correct 3002 ms 37764 KB Output is correct
4 Correct 2074 ms 32920 KB Output is correct
5 Correct 1655 ms 23096 KB Output is correct
6 Correct 385 ms 11004 KB Output is correct
7 Correct 201 ms 9136 KB Output is correct