#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
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
74576 KB |
Output is correct |
2 |
Correct |
7 ms |
74580 KB |
Output is correct |
3 |
Correct |
8 ms |
74472 KB |
Output is correct |
4 |
Correct |
7 ms |
74576 KB |
Output is correct |
5 |
Correct |
7 ms |
74576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
291 ms |
105044 KB |
Output is correct |
2 |
Correct |
289 ms |
104252 KB |
Output is correct |
3 |
Correct |
156 ms |
94688 KB |
Output is correct |
4 |
Correct |
50 ms |
85452 KB |
Output is correct |
5 |
Correct |
157 ms |
103780 KB |
Output is correct |
6 |
Correct |
48 ms |
86856 KB |
Output is correct |
7 |
Correct |
10 ms |
74576 KB |
Output is correct |
8 |
Correct |
9 ms |
74576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
74576 KB |
Output is correct |
2 |
Correct |
11 ms |
74576 KB |
Output is correct |
3 |
Correct |
10 ms |
74576 KB |
Output is correct |
4 |
Correct |
10 ms |
74576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
589 ms |
104640 KB |
Output is correct |
2 |
Correct |
578 ms |
102956 KB |
Output is correct |
3 |
Correct |
361 ms |
95420 KB |
Output is correct |
4 |
Correct |
343 ms |
102228 KB |
Output is correct |
5 |
Correct |
109 ms |
84140 KB |
Output is correct |
6 |
Correct |
59 ms |
89092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1177 ms |
101568 KB |
Output is correct |
2 |
Correct |
1193 ms |
105072 KB |
Output is correct |
3 |
Correct |
1147 ms |
104108 KB |
Output is correct |
4 |
Correct |
748 ms |
95428 KB |
Output is correct |
5 |
Correct |
645 ms |
102076 KB |
Output is correct |
6 |
Correct |
173 ms |
87572 KB |
Output is correct |
7 |
Correct |
63 ms |
88904 KB |
Output is correct |