#pragma GCC optimize("O3")
#pragma GCC target("popcnt")
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define si short int
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pill pair<ll,ll>
#define f first
#define s second
#define pilc pair<ll,char>
#define all(a) (a).begin(),(a).end()
#define rep(s,e,step) for(int i = (s); i < (e) ; i += step)
#define vrep(s,e,step) for(int j = (s); j < (e) ; j += step)
#define ex exit(0)
#define triple pair<pill, ll>
#define pinode pair<node*, node*>
#define quadra pair<pill, pill>
#define ld double
#define pild pair<double,double>
using namespace std;
const ll N = 1e5 + 11;
const ll M = 32;
const ll mod = 998244353;
mt19937_64 bruh(chrono::steady_clock::now().time_since_epoch().count());
ll n, m, k;
ll dp[N][M];
bool was[N][M];
vector<pill> g[N];
vector<int> prec[M];
ll x[N];
int main() {
speed;
cin >> n >> k >> m;
for(int i = 0; i < k; i++)
cin >> x[i];
for(int i = 1; i < (1<<k); i++) {
for(int j = i + 1; j < (1<<k); j++)
if(!(i&j)) {
prec[i].pb(j), prec[j].pb(i);
}
}
for(int i = 1; i <= m; i++) {
ll a, b, c;
cin >> a >> b >> c;
g[a].pb(mp(b, c));
g[b].pb(mp(a, c));
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j < (1<<k); j++)
dp[i][j] = 1e17;
}
for(int i = 0; i < k; i++) {
dp[x[i]][1<<i] = 0;
}
for(int i = 0; i < (1<<k); i++) {
set<pair<ll, int>> q;
for(int j = 1; j <= n; j++)
q.insert(mp(dp[j][i], j));
while(q.size()) {
ll v = (*q.begin()).s;
q.erase(q.begin());
if(was[v][i])continue;
was[v][i] = 1;
for(auto u : g[v]) {
if(dp[u.f][i] > dp[v][i] + u.s) {
dp[u.f][i] = dp[v][i] + u.s;
q.insert(mp(dp[u.f][i], u.f));
}
}
}
for(int v = 1; v <= n; v++) {
for(auto u : g[v]) {
for(auto msk : prec[i]) {
if(dp[u.f][msk|i] > dp[u.f][msk] + u.s + dp[v][i]) {
dp[u.f][msk|i] = dp[u.f][msk] + u.s + dp[v][i];
}
}
}
}
}
ll ans = 1e17;
for(int i = 1; i <= n; i++)
ans = min(ans, dp[i][(1<<k) - 1]);
cout << ans;
}
/*
5 3 1
1 2 4 7 11
5 7 10
1 3 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1296 ms |
50752 KB |
Output is correct |
2 |
Correct |
1408 ms |
51600 KB |
Output is correct |
3 |
Correct |
638 ms |
42388 KB |
Output is correct |
4 |
Correct |
79 ms |
11756 KB |
Output is correct |
5 |
Correct |
571 ms |
50784 KB |
Output is correct |
6 |
Correct |
69 ms |
11876 KB |
Output is correct |
7 |
Correct |
6 ms |
3148 KB |
Output is correct |
8 |
Correct |
4 ms |
3148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
3148 KB |
Output is correct |
2 |
Correct |
12 ms |
3148 KB |
Output is correct |
3 |
Correct |
8 ms |
3020 KB |
Output is correct |
4 |
Correct |
7 ms |
2892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2836 ms |
50532 KB |
Output is correct |
2 |
Correct |
2827 ms |
55980 KB |
Output is correct |
3 |
Correct |
1360 ms |
44416 KB |
Output is correct |
4 |
Correct |
1591 ms |
37048 KB |
Output is correct |
5 |
Correct |
332 ms |
20320 KB |
Output is correct |
6 |
Correct |
103 ms |
17640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5769 ms |
50536 KB |
Output is correct |
2 |
Correct |
5699 ms |
54880 KB |
Output is correct |
3 |
Correct |
5910 ms |
55732 KB |
Output is correct |
4 |
Correct |
2954 ms |
44536 KB |
Output is correct |
5 |
Correct |
3301 ms |
37048 KB |
Output is correct |
6 |
Correct |
621 ms |
20440 KB |
Output is correct |
7 |
Correct |
182 ms |
17392 KB |
Output is correct |