#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FOS(i, r, l) for (int i = (r); i >= (l); --i)
#define FRN(i, n) for (int i = 0; i < (n); ++i)
#define FSN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : (x))
#define WHILE while
template<typename T> T gcd(T a, T b) { WHILE(b) { a = a % b; swap(a, b); } return a; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
GOAT
----------------------------------------------------------------
*/
struct Edge {
int u, v, w;
Edge() {}
Edge(int u, int v, int w) : u(u), v(v), w(w) {}
};
const ll oo = 1e18;
const int M = 2e5 + 5;
const int N = 1e5 + 5;
const int K = 10;
int n, k, m, a[K];
ll d[N][1 << 5], rs = oo;
vpi adj[N];
priority_queue<pll, vpll, greater<pll> > pq;
void input() {
cin >> n >> k >> m;
FOR(i, 1, k) cin >> a[i];
FOR(i, 1, m) {
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
}
void solve() {
FOR(j, 1, (1 << k) - 1) {
int bc = __builtin_popcount(j), pos = __builtin_ctz(j & -j) + 1;
FOR(i, 1, n) {
d[i][j] = oo;
if (bc == 1 && i == a[pos]) d[i][j] = 0;
if (bc > 1) {
for (int z = (j - 1) & j; z > 0; z = (z - 1) & j)
EACH(k, adj[i]) {
int v = k.st, w = k.nd;
d[i][j] = min(d[i][j], d[v][z] + d[i][j ^ z] + w);
}
}
}
FOR(i, 1, n) pq.push({d[i][j], i});
WHILE(!pq.empty()) {
int u = pq.top().nd;
ll du = pq.top().st;
pq.pop();
if (du > d[u][j]) continue;
EACH(z, adj[u]) {
int v = z.st, w = z.nd;
if (d[v][j] > du + w) {
d[v][j] = du + w;
pq.push({d[v][j], v});
}
}
}
}
FOR(i, 1, n) rs = min(rs, d[i][(1 << k) - 1]);
cout << rs;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
input();
solve();
return 0;
}
/*
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
682 ms |
42416 KB |
Output is correct |
2 |
Correct |
696 ms |
41904 KB |
Output is correct |
3 |
Correct |
447 ms |
35188 KB |
Output is correct |
4 |
Correct |
65 ms |
11128 KB |
Output is correct |
5 |
Correct |
335 ms |
42372 KB |
Output is correct |
6 |
Correct |
56 ms |
11052 KB |
Output is correct |
7 |
Correct |
4 ms |
3028 KB |
Output is correct |
8 |
Correct |
3 ms |
3064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3096 KB |
Output is correct |
2 |
Correct |
6 ms |
3028 KB |
Output is correct |
3 |
Correct |
5 ms |
2900 KB |
Output is correct |
4 |
Correct |
5 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1438 ms |
42420 KB |
Output is correct |
2 |
Correct |
1434 ms |
42036 KB |
Output is correct |
3 |
Correct |
999 ms |
35120 KB |
Output is correct |
4 |
Correct |
836 ms |
27328 KB |
Output is correct |
5 |
Correct |
203 ms |
14796 KB |
Output is correct |
6 |
Correct |
98 ms |
12688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3009 ms |
42436 KB |
Output is correct |
2 |
Correct |
3128 ms |
42356 KB |
Output is correct |
3 |
Correct |
3094 ms |
42096 KB |
Output is correct |
4 |
Correct |
2163 ms |
35152 KB |
Output is correct |
5 |
Correct |
1687 ms |
27328 KB |
Output is correct |
6 |
Correct |
382 ms |
14832 KB |
Output is correct |
7 |
Correct |
206 ms |
12620 KB |
Output is correct |