Submission #669847

# Submission time Handle Problem Language Result Execution time Memory
669847 2022-12-07T13:14:06 Z ghostwriter Cities (BOI16_cities) C++11
100 / 100
3061 ms 38200 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 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 667 ms 38072 KB Output is correct
2 Correct 664 ms 37708 KB Output is correct
3 Correct 416 ms 32960 KB Output is correct
4 Correct 65 ms 7504 KB Output is correct
5 Correct 334 ms 38140 KB Output is correct
6 Correct 53 ms 7500 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 6 ms 3028 KB Output is correct
3 Correct 4 ms 2900 KB Output is correct
4 Correct 5 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1402 ms 38100 KB Output is correct
2 Correct 1406 ms 37812 KB Output is correct
3 Correct 939 ms 32960 KB Output is correct
4 Correct 804 ms 23136 KB Output is correct
5 Correct 188 ms 10872 KB Output is correct
6 Correct 94 ms 9212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2955 ms 38200 KB Output is correct
2 Correct 3004 ms 38100 KB Output is correct
3 Correct 3061 ms 37760 KB Output is correct
4 Correct 2064 ms 32960 KB Output is correct
5 Correct 1645 ms 23088 KB Output is correct
6 Correct 386 ms 10888 KB Output is correct
7 Correct 196 ms 9164 KB Output is correct