제출 #659387

#제출 시각아이디문제언어결과실행 시간메모리
659387thanh913Cities (BOI16_cities)C++14
100 / 100
1906 ms42376 KiB
#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[1 << 5][N];

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

void dijkstra(ll f[N]) {
	for (int i = 1; i <= n; i++) {
		if (f[i] < 1e16) pq.push({f[i], i});
	}

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

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	memset(f, inf, sizeof(f));
	memset(f[0], 0, sizeof(f[0]));
	cin >> n >> k >> m;
	for (int i = 1; i <= k; i++) {
		int tmp; cin >> tmp;
		imp.push_back(tmp);
		f[1 << (i - 1)][tmp] = 0;
	}
	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});
	}
	
	for (int mask = 1; mask < (1 << k); mask++) {
		for (int pos = 0; pos < k; pos++) {
			if (!((mask >> pos) & 1)) continue;	
			for (int i = 1; i <= n; i++) {
				cmin(f[mask][i], f[mask ^ (1 << pos)][i] + f[(1 << pos)][i]);
			}
		}

		dijkstra(f[mask]);
	}
	cout << *min_element(f[(1 << k) - 1] + 1, f[(1 << k) - 1] + n + 1);
}
#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...