This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define ll long long
#define ii pair<int, int>
#define tp tuple<ll, int, int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
const int N = 2e5 + 5,
mod = 1e9 + 7;
const ll INF = 2e16;
// code
int n, k, m, a[6];
ll dp[N][33];
vector<ii> ad[N];
priority_queue<tp, vector<tp>, greater<>> pq;
int id(int i){
for(int j = 0; j < k; j++) if(a[j] == i) return j;
return -1;
}
bool check(int msk1, int msk2){
for(int i = 0; i < (1 << k); i++) if((msk1 >> i & 1) && (msk2 >> i & 1)) return false;
return true;
}
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
#define task "test"
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 = 0; i < m; i++){
int a, b, c; cin >> a >> b >> c;
ad[a].push_back({b, c});
ad[b].push_back({a, c});
}
for(int i = 1; i <= n; i++)
for(int j = 1; j < (1 << k); j++) dp[i][j] = INF;
for(int i = 0; i < k; i++){
dp[a[i]][1 << i] = 0;
pq.push({0, 1 << i, a[i]});
}
while(pq.size()){
auto [d, msk, u] = pq.top(); pq.pop();
if(d != dp[u][msk]) continue;
// cerr << u << " " << d << " " << msk << endl;
for(auto [v, c] : ad[u]){
int bit = id(v);
if(bit != -1 && !(msk >> bit & 1))
if(d + c < dp[v][msk|(1 << bit)])
pq.push({dp[v][msk|(1 << bit)] = d + c, msk|(1 << bit), v});
for(int _msk = 0; _msk < (1 << k); _msk++)
if(check(msk, _msk) && d + c + dp[v][_msk] < dp[v][msk|_msk])
pq.push({dp[v][msk|_msk] = d + c + dp[v][_msk], msk|_msk, v});
}
}
ll res = INF;
for(int i = 1; i <= n; i++){
res = min(res, dp[i][(1 << k)-1]);
// cerr << dp[i][(1 << k)-1] << endl;
}
cout << res << endl;
return 0;
}
Compilation message (stderr)
cities.cpp: In function 'int32_t main()':
cities.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen(task ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | 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... |