#include <bits/stdc++.h>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 2e5 + 7;
const int Mod = 998244353;
const int szBL = 916;
const ll INF = 1e18;
const int BASE = 137;
int n, K, m;
vector<pair<int,ll>> ke[N];
ll dp[N][(1 << 5) + 7]; ///suppose i is root
void solution () {
cin >> n >> K >> m;
memset(dp, 0x3f, sizeof dp);
rep (i, 1, K) {
int x;
cin >> x;
dp[x][(1 << i - 1)] = 0;
}
rep (i, 1, m) {
ll u, v, c;
cin >> u >> v >> c;
ke[u].push_back({v, c});
ke[v].push_back({u, c});
}
rep (msk, 1, (1 << K) - 1) {
if (__builtin_popcount(msk) > 1) {
rep (i, 1, n) {
rep (j, 0, K - 1) if (bit (msk, j) ) {
int nmsk = msk ^ (1 << j);
dp[i][msk] = min(dp[i][msk], dp[i][nmsk] + dp[i][(1 << j)]);
}
}
}
static priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> pq;
rep (i, 1, n) {
pq.push({dp[i][msk], i});
}
while (!pq.empty()) {
int u = pq.top().se;
pq.pop();
iter (&id, ke[u]) {
int v = id.fs, w = id.se;
if (dp[v][msk] > dp[u][msk] + w) {
dp[v][msk] = dp[u][msk] + w;
pq.push({dp[v][msk], v});
}
}
}
// rep (i, 1, n) cout << msk<<" "<<i<<" "<<dp[i][msk]<<"\n";
}
ll res = INF;
rep (i, 1, n) {
res = min(res, dp[i][(1 << K) - 1]);
}
cout << res <<"\n";
}
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// file ("fbuy");
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
7
1 2 4
2 3 12
3 4 2
4 5 48
5 6 2
6 7 123
*/
Compilation message
cities.cpp: In function 'void solution()':
cities.cpp:37:23: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
37 | dp[x][(1 << i - 1)] = 0;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
66136 KB |
Output is correct |
2 |
Correct |
9 ms |
66212 KB |
Output is correct |
3 |
Correct |
9 ms |
66156 KB |
Output is correct |
4 |
Correct |
9 ms |
66140 KB |
Output is correct |
5 |
Correct |
9 ms |
66136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
409 ms |
85500 KB |
Output is correct |
2 |
Correct |
433 ms |
85616 KB |
Output is correct |
3 |
Correct |
247 ms |
75720 KB |
Output is correct |
4 |
Correct |
63 ms |
78672 KB |
Output is correct |
5 |
Correct |
222 ms |
85196 KB |
Output is correct |
6 |
Correct |
54 ms |
78672 KB |
Output is correct |
7 |
Correct |
12 ms |
66392 KB |
Output is correct |
8 |
Correct |
10 ms |
66396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
66372 KB |
Output is correct |
2 |
Correct |
14 ms |
66396 KB |
Output is correct |
3 |
Correct |
13 ms |
66336 KB |
Output is correct |
4 |
Correct |
12 ms |
66416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
863 ms |
85260 KB |
Output is correct |
2 |
Correct |
791 ms |
84280 KB |
Output is correct |
3 |
Correct |
528 ms |
75720 KB |
Output is correct |
4 |
Correct |
435 ms |
83144 KB |
Output is correct |
5 |
Correct |
148 ms |
80052 KB |
Output is correct |
6 |
Correct |
74 ms |
80888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1619 ms |
85448 KB |
Output is correct |
2 |
Correct |
1621 ms |
85444 KB |
Output is correct |
3 |
Correct |
1491 ms |
85948 KB |
Output is correct |
4 |
Correct |
977 ms |
75716 KB |
Output is correct |
5 |
Correct |
865 ms |
83108 KB |
Output is correct |
6 |
Correct |
235 ms |
80048 KB |
Output is correct |
7 |
Correct |
94 ms |
80980 KB |
Output is correct |