#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int, int> pii;
#define fi first
#define se second
void maxi (int &x, int y) {x = max (x, y);}
void mini (int &x, int y) {x = min (x, y);}
const ll maxN = 2e5 + 5, maxLog = 20, mod = 1e9 + 7, inf64 = 1e18, maxK = 5;
ll n, k, m, a [maxK], f [maxK] [1 << maxK], f2 [maxN] [1 << maxK], f3 [1 << maxK];
vector <pll> adj [maxN];
priority_queue <pll, vector <pll>, greater <pll>> q;
vector <ll> dij [maxK];
vector <ll> getdijk (ll x) {
vector <ll> ans (n + 1, inf64);
ans [x] = 0;
q. push ({ans [x], x});
while (!q. empty ()) {
ll d = q. top (). fi;
ll pos = q. top (). se;
q. pop ();
if (d != ans [pos]) continue;
for (pll i : adj [pos]) {
if (ans [i. fi] > ans [pos] + i. se) {
ans [i. fi] = ans [pos] + i. se;
q. push ({ans [i. fi], i. fi});
}
}
}
return ans;
}
signed main () {
ios::sync_with_stdio (0);
cin. tie (0);
cout. tie (0);
#define task "untitled1"
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n >> k >> m;
for (int i = 0; i < k; i ++) cin >> a [i];
for (int i = 1, u, v, w; i <= m; i ++) {
cin >> u >> v >> w;
adj [u]. push_back ({v, w});
adj [v]. push_back ({u, w});
}
for (int i = 0; i < k; i ++) {
dij [i] = getdijk (a [i]);
}
memset (f, 0x3f, sizeof f);
for (int i = 0; i < k; i ++) f [i] [1 << i] = 0;
for (int i = 0; i < (1 << k); i ++) {
for (int on = 0; on < k; on ++) if ((i >> on) & 1) {
for (int off = 0; off < k; off ++) if (((~i) >> on) & 1) {
mini (f [off] [i | (1 << off)],
f [on] [i] + dij [on] [a [off]]);
}
}
}
ll ans = inf64;
for (int i = 0; i < k; i ++) mini (ans, f [i] [(1 << k) - 1]);
memset (f3, 0x3f, sizeof f3);
for (int i = 0; i < (1 << k); i ++) {
for (int j = 1; j <= n; j ++) {
for (int bit = 0; bit < k; bit ++) if ((i >> bit) & 1) {
f2 [j] [i] += dij [bit] [j];
}
mini (f3 [i], f2 [j] [i]);
}
}
for (int i = 0; i < (1 << k); i ++){
for (int j = 0; j < k; j ++) if ((i >> j) & 1){
ll br = (1 << k) - 1 - i;
br += (1 << j);
mini (ans, f3 [i] + f [j] [br]);
mini (ans, f3 [i] + f3 [br]);
}
}
cout << ans;
}
Compilation message (stderr)
cities.cpp: In function 'int main()':
cities.cpp:45:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:46:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |