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>
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define RRH(v) v.resize(unique(all(v)) - v.begin())
using namespace std;
const int N = 3e5+7;
const int M = 1e9+7;
const ll oo = 3e18;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long GetRandom(long long l, long long r) {
return uniform_int_distribution<long long> (l, r)(rng);
}
int n, k, m, a[N];
vector<pair<int, ll>> g[N];
ll dist[33][N];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
#define TASK ""
if (fopen (".inp", "r")) {
freopen (".inp", "r", stdin);
freopen (".out", "w", stdout);
}
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> k >> m;
For (i, 1, k) {
cin >> a[i];
}
For (i, 1, m) {
int u, v, w;
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
For (j, 0, 32) For (i, 0, n) dist[j][i] = oo;
For (i, 1, k) dist[(1 << (i - 1))][a[i]] = 0;
priority_queue <pair<ll, int>, vector<pair<ll,int>>, greater<pair<ll,int>>> q;
For (mask, 1, (1 << k) - 1) {
for (int submask = mask; submask > 0; submask = (submask - 1) & mask) {
For (i, 1, n) {
dist[mask][i] = min(dist[mask][i], dist[submask][i] + dist[mask ^ submask][i]);
}
}
// cout << mask << '\n';
for (int i = 1; i <= n; i++) {
if (dist[mask][i] < oo) {
q.push({dist[mask][i], i});
// cout << i << " ";
}
}
//cout << mask << '\n';
while (!q.empty()) {
ll d, u;
tie(d, u) = q.top();
q.pop();
if (dist[mask][u] != d) continue;
for (auto [v, w]: g[u]) {
if (dist[mask][v] > d + w) {
dist[mask][v] = d + w;
q.push({d + w, v});
}
}
}
}
ll ans = oo;
For (i, 1, n) ans = min(ans, dist[(1 << k) - 1][i]);
cout << ans << '\n';
cerr << "Time elapsed: " << TIME << " s.\n";
return 0;
}
Compilation message (stderr)
cities.cpp: In function 'int main()':
cities.cpp:78:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
78 | for (auto [v, w]: g[u]) {
| ^
cities.cpp:31:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen (".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~
cities.cpp:32:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen (".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
cities.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | 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... |