This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |