답안 #659345

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
659345 2022-11-17T14:06:19 Z thanh913 Cities (BOI16_cities) C++14
74 / 100
6000 ms 165460 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

//types
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define pii pair<int, int>

#define fi first
#define se second
#define inf 0x3f3f3f3f

#define pw2(x) (1LL << (x))
#define getBit(x, y) ((x) & (1LL << (y)))

template<class X, class Y>
bool cmax(X &a, const Y &b) {
    return a < b ? a = b, 1 : 0;
}

template<class X, class Y>
bool cmin(X &a, const Y &b) {
    return a > b ? a = b, 1 : 0;
}

//lowercase 31, all 53
//(P/Q) % M = (P % M) * (Q^(M-2) % M)
//-------------------------------------------------------

const ld PI = 3.14159265359;
const ll N = 1e5+5, mod = 1e9+7;
int n, k, m;
vector<int> imp;
vector<pii> adj[N];

ll f[N][1 << 5];

using T = tuple<ll, int, int>;
priority_queue<T, vector<T>, greater<T>> pq;

ll dijkstra() {
	memset(f, 0x3f, sizeof(f));
	for (int i = 0; i < k; i++) {
		f[imp[i]][1 << i] = 0;
		pq.push({0, imp[i], 1 << i});
	}
	for (int i = 1; i <= n; i++)
		f[i][0] = 0;

	while (!pq.empty()) {
		ll d; int u, mask;
		tie(d, u, mask) = pq.top(); pq.pop();
		if (d != f[u][mask]) continue;
		for (auto e : adj[u]) {
			int v, w; tie(v, w) = e;
			for (int mask2 = 0; mask2 < (1 << k); mask2++) {
				int newMask = mask | mask2;
				if (cmin(f[v][newMask], f[u][mask] + f[v][mask2] + w)) {
					pq.push({f[v][newMask], v, newMask});
				}
			}
		}
	}

	ll ans = 1e16;
	for (int i = 1; i <= n; i++) {
		cmin(ans, f[i][(1 << k) - 1]);
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> k >> m;
	for (int i = 1; i <= k; i++) {
		int tmp; cin >> tmp;
		imp.push_back(tmp);
	}
	for (int i = 1; i <= m; i++) {
		int u, v, w; cin >> u >> v >> w;
		adj[u].push_back({v, w});
		adj[v].push_back({u, w});
	}
	cout << dijkstra();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 27604 KB Output is correct
2 Correct 12 ms 27716 KB Output is correct
3 Correct 11 ms 27664 KB Output is correct
4 Correct 11 ms 27732 KB Output is correct
5 Correct 13 ms 27772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 886 ms 50404 KB Output is correct
2 Correct 800 ms 49964 KB Output is correct
3 Correct 401 ms 35052 KB Output is correct
4 Correct 102 ms 33244 KB Output is correct
5 Correct 326 ms 38068 KB Output is correct
6 Correct 72 ms 32708 KB Output is correct
7 Correct 16 ms 27988 KB Output is correct
8 Correct 13 ms 27860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 28388 KB Output is correct
2 Correct 19 ms 28368 KB Output is correct
3 Correct 17 ms 28132 KB Output is correct
4 Correct 19 ms 28392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2710 ms 99728 KB Output is correct
2 Correct 2447 ms 99380 KB Output is correct
3 Correct 1305 ms 47380 KB Output is correct
4 Correct 1905 ms 66424 KB Output is correct
5 Correct 422 ms 48908 KB Output is correct
6 Correct 209 ms 36032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6094 ms 165460 KB Time limit exceeded
2 Halted 0 ms 0 KB -