#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
assert(l <= r);
return uniform_int_distribution<ll> (l, r)(rng);
}
const int N = 1e5 + 3;
const int K = 5;
int n, k, m, c[K];
vector<pair<int, ll>> g[N];
ll f[N][1 << K];
bool vis[N][1 << K];
bool mini(ll &x, ll y){
if(x <= y)
return 0;
x = y;
return 1;
}
void solve(){
cin >> n >> k >> m;
for(int i = 0; i < k; ++i)
cin >> c[i];
for(int i = 1; i <= m; ++i){
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
memset(f, 40, sizeof(f));
priority_queue<pair<ll, pii>> pq;
for(int i = 0; i < k; ++i)
pq.push(make_pair(f[c[i]][1 << i] = 0, make_pair(c[i], 1 << i)));
while(!pq.empty()){
int u = pq.top().S.F;
int mask = pq.top().S.S;
pq.pop();
if(vis[u][mask])
continue;
vis[u][mask] = 1;
for(auto [v, w] : g[u]){
int nmask = mask;
for(int i = 0; i < k; ++i)
if(c[i] == v)
nmask |= (1 << i);
if(mini(f[v][nmask], f[u][mask] + w))
pq.push(make_pair(-f[v][nmask], make_pair(v, mask)));
int rev = ((1 << k) - 1) ^ mask, smask = rev;
for(int smask = rev; smask; smask = (smask - 1) & rev){
if(mini(f[v][mask | smask], f[u][mask] + f[v][smask] + w))
pq.push(make_pair(-f[v][mask | smask], make_pair(v, mask | smask)));
}
}
}
ll res = LLONG_MAX;
for(int i = 1; i <= n; ++i){
debug(i, f[i][(1 << k) - 1]);
res = min(res, f[i][(1 << k) - 1]);
}
cout << res;
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(0);
#define task "troll"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
// cin >> test;
for(int i = 1; i <= test; ++i){
// cout << "Case #" << i << ": ";
solve();
}
#ifdef LOCAL
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
#endif
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
cities.cpp: In function 'int32_t main()':
cities.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | 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... |