#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, int>
#define pii pair <int, int>
#define ull unsigned long long
// #define int long long
// #define int unsigned long long
const ll inf = 1e18 + 7;
const ll weirdMod = 998244353;
vector<vector<pii>> G;
void solve() {
int n, k, m;
cin >> n >> k >> m; G.resize(n + 1);
ll dp[n + 1][1 << k]; int f[k];
for (int i = 1; i <= n; i++)
for (int j = 0; j < (1 << k); j++) dp[i][j] = inf;
for (int i = 0; i < k; i++) {
cin >> f[i]; dp[f[i]][1 << i] = 0;
} for (int i = 1; i <= m; i++) {
int x, y, z;
cin >> x >> y >> z;
G[x].pb({y, z}); G[y].pb({x, z});
} for (int msk = 1; msk < (1 << k); msk++) {
for (int msk1 = 0; msk1 < msk; msk1++) {
if ((msk & msk1) != msk1) continue;
int msk2 = (msk ^ msk1);
for (int i = 1; i <= n; i++)
dp[i][msk] = min(dp[i][msk], dp[i][msk1] + dp[i][msk2]);
} priority_queue<pll, vector<pll>, greater<pll>> q;
for (int i = 1; i <= n; i++)
q.push({dp[i][msk], i});
while (q.size()) {
auto [w, u] = q.top(); q.pop();
if (dp[u][msk] != w) continue;
for (auto [v, wgt] : G[u]) {
if (dp[v][msk] > dp[u][msk] + wgt)
q.push({dp[v][msk] = dp[u][msk] + wgt, v});
}
}
} ll mn = inf;
for (int i = 1; i <= n; i++) {
mn = min(mn, dp[i][(1 << k) - 1]);
} cout << mn;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
23040 KB |
Output is correct |
2 |
Correct |
303 ms |
21700 KB |
Output is correct |
3 |
Correct |
192 ms |
15812 KB |
Output is correct |
4 |
Correct |
37 ms |
5200 KB |
Output is correct |
5 |
Correct |
153 ms |
19816 KB |
Output is correct |
6 |
Correct |
32 ms |
5116 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
604 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
763 ms |
27992 KB |
Output is correct |
2 |
Correct |
737 ms |
27988 KB |
Output is correct |
3 |
Correct |
472 ms |
22064 KB |
Output is correct |
4 |
Correct |
346 ms |
17720 KB |
Output is correct |
5 |
Correct |
102 ms |
8200 KB |
Output is correct |
6 |
Correct |
42 ms |
6924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1767 ms |
42312 KB |
Output is correct |
2 |
Correct |
1744 ms |
41816 KB |
Output is correct |
3 |
Correct |
1720 ms |
41348 KB |
Output is correct |
4 |
Correct |
1194 ms |
35136 KB |
Output is correct |
5 |
Correct |
800 ms |
23616 KB |
Output is correct |
6 |
Correct |
177 ms |
9524 KB |
Output is correct |
7 |
Correct |
51 ms |
7180 KB |
Output is correct |