#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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
27604 KB |
Output is correct |
2 |
Correct |
11 ms |
27604 KB |
Output is correct |
3 |
Correct |
11 ms |
27640 KB |
Output is correct |
4 |
Correct |
11 ms |
27604 KB |
Output is correct |
5 |
Correct |
12 ms |
27604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
423 ms |
38140 KB |
Output is correct |
2 |
Correct |
417 ms |
37748 KB |
Output is correct |
3 |
Correct |
241 ms |
32952 KB |
Output is correct |
4 |
Correct |
70 ms |
32428 KB |
Output is correct |
5 |
Correct |
222 ms |
38080 KB |
Output is correct |
6 |
Correct |
61 ms |
32284 KB |
Output is correct |
7 |
Correct |
15 ms |
27732 KB |
Output is correct |
8 |
Correct |
13 ms |
27732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
27732 KB |
Output is correct |
2 |
Correct |
16 ms |
27848 KB |
Output is correct |
3 |
Correct |
15 ms |
27768 KB |
Output is correct |
4 |
Correct |
14 ms |
27816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
815 ms |
38140 KB |
Output is correct |
2 |
Correct |
839 ms |
37796 KB |
Output is correct |
3 |
Correct |
555 ms |
32968 KB |
Output is correct |
4 |
Correct |
459 ms |
35660 KB |
Output is correct |
5 |
Correct |
129 ms |
33028 KB |
Output is correct |
6 |
Correct |
70 ms |
34044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1728 ms |
38104 KB |
Output is correct |
2 |
Correct |
1795 ms |
42376 KB |
Output is correct |
3 |
Correct |
1906 ms |
42032 KB |
Output is correct |
4 |
Correct |
1250 ms |
35152 KB |
Output is correct |
5 |
Correct |
971 ms |
39736 KB |
Output is correct |
6 |
Correct |
244 ms |
36848 KB |
Output is correct |
7 |
Correct |
94 ms |
37396 KB |
Output is correct |