답안 #147324

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
147324 2019-08-28T20:23:54 Z cerberus Cities (BOI16_cities) C++17
74 / 100
6000 ms 67568 KB
/* cerberus97 - Hanit Banga */

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>

using namespace std;

#define pb push_back
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const int N = 1e5 + 10, K = 5, S = (1 << K) + 10;
const ll inf = 1e15 + 42;

ll dp[S][N];

struct state_t {
	int mask, u;
	bool operator<(const state_t &o) const {
		return dp[mask][u] > dp[o.mask][o.u];
	}
};

int imp[K];
vector<pii> g[N];
vector<int> supermasks[S];
bool vis[S][N];

int main() {
	fast_cin();
	int n, k, m;
	cin >> n >> k >> m;
	for (int i = 0; i < k; ++i) {
		cin >> imp[i];
	}
	for (int i = 0; i < m; ++i) {
		int u, v, w;
		cin >> u >> v >> w;
		g[u].pb({v, w});
		g[v].pb({u, w});
	}
	int tot = (1 << k), all = tot - 1;
	for (int mask = 1; mask < tot; ++mask) {
		for (int u = 1; u <= n; ++u) {
			dp[mask][u] = inf;
		}
	}
	for (int mask = 0; mask < tot; ++mask) {
		if (__builtin_popcount(mask) <= 1) {
			for (int smask = mask + 1; smask < tot; ++smask) {
				if ((smask & mask) == mask) {
					supermasks[mask].pb(smask);
				}
			}
		} else {
			for (int i = 0; i < k; ++i) {
				if (!(mask & (1 << i))) {
					supermasks[mask].pb(mask | (1 << i));
				}
			}
		}
	}
	priority_queue<state_t> q;
	for (int i = 0; i < k; ++i) {
		dp[1 << i][imp[i]] = 0;
		q.push({1 << i, imp[i]});
	}
	while (!q.empty()) {
		auto cur = q.top();
		q.pop();
		int mask = cur.mask, u = cur.u;
		if (vis[mask][u]) {
			continue;
		}
		vis[mask][u] = true;
		if (mask == all) {
			cout << dp[mask][u] << endl;
			return 0;
		}
		for (auto &e : g[u]) {
			int v = e.first, w = e.second;
			ll cand = dp[mask][u] + w;
			if (cand < dp[mask][v]) {
				dp[mask][v] = cand;
				q.push({mask, v});
			}
		}
		for (auto &nmask : supermasks[mask]) {
			int extra = nmask ^ mask;
			ll cand = dp[mask][u] + dp[extra][u];
			if (cand < dp[nmask][u]) {
				dp[nmask][u] = cand;
				q.push({nmask, u});
			}
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2808 KB Output is correct
4 Correct 4 ms 2808 KB Output is correct
5 Correct 4 ms 3064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 838 ms 23508 KB Output is correct
2 Correct 792 ms 18784 KB Output is correct
3 Correct 118 ms 12276 KB Output is correct
4 Correct 84 ms 7672 KB Output is correct
5 Correct 272 ms 12660 KB Output is correct
6 Correct 76 ms 7288 KB Output is correct
7 Correct 7 ms 2936 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 3320 KB Output is correct
2 Correct 11 ms 3320 KB Output is correct
3 Correct 7 ms 3064 KB Output is correct
4 Correct 8 ms 3104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4687 ms 38300 KB Output is correct
2 Correct 4287 ms 37464 KB Output is correct
3 Correct 1307 ms 23164 KB Output is correct
4 Correct 1857 ms 23136 KB Output is correct
5 Correct 270 ms 10868 KB Output is correct
6 Correct 100 ms 9464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6008 ms 67568 KB Time limit exceeded
2 Halted 0 ms 0 KB -