#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll K = 5;
const ll N = 1e5 + 50;
const ll inf = 1e18;
ll n, m, k, dist[(1 << K)][N];
bool mark[N];
vector<int> vec;
vector<pii> adj[N];
void dijk(int id){
fill(mark, mark + N, 0);
priority_queue<pll, vector<pll>, greater<pll>> pq;
for(int i = 1; i <= n; i++) if(dist[id][i] < inf) pq.push({dist[id][i], i});
while(!pq.empty()){
int v = pq.top().S;
pq.pop();
if(mark[v]) continue;
mark[v] = true;
for(auto [u, w]: adj[v]){
if(dist[id][v] + w < dist[id][u]){
dist[id][u] = dist[id][v] + w;
pq.push({dist[id][u], u});
}
}
}
}
int main(){
fast_io;
cin >> n >> k >> m;
int u, v, w;
for(int i = 1; i <= k; i++){
cin >> v; vec.pb(v);
}
for(int i = 1; i <= m; i++){
cin >> u >> v >> w;
adj[u].pb(Mp(v, w));
adj[v].pb(Mp(u, w));
}
for(int i = 0; i < (1 << k); i++) fill(dist[i], dist[i] + N, inf);
for(int i = 0; i < k; i++) dist[(1 << i)][vec[i]] = 0;
for(int mask = 1; mask < (1 << k); mask++){
for(int i = 1; i <= n; i++){
for(int sub = mask; sub; sub = (sub-1) & mask){
dist[mask][i] = min(dist[mask][i], dist[sub][i] + dist[mask ^ sub][i]);
}
}
dijk(mask);
}
cout << dist[(1 << k)-1][vec[0]];
}
Compilation message
cities.cpp: In function 'void dijk(int)':
cities.cpp:48:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | for(auto [u, w]: adj[v]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
7 ms |
27740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
334 ms |
28112 KB |
Output is correct |
2 |
Correct |
371 ms |
28572 KB |
Output is correct |
3 |
Correct |
173 ms |
19724 KB |
Output is correct |
4 |
Correct |
45 ms |
18952 KB |
Output is correct |
5 |
Correct |
171 ms |
22176 KB |
Output is correct |
6 |
Correct |
42 ms |
14884 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16988 KB |
Output is correct |
2 |
Correct |
7 ms |
17052 KB |
Output is correct |
3 |
Correct |
5 ms |
16988 KB |
Output is correct |
4 |
Correct |
5 ms |
17044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
738 ms |
35352 KB |
Output is correct |
2 |
Correct |
706 ms |
35424 KB |
Output is correct |
3 |
Correct |
448 ms |
25912 KB |
Output is correct |
4 |
Correct |
400 ms |
30540 KB |
Output is correct |
5 |
Correct |
126 ms |
26464 KB |
Output is correct |
6 |
Correct |
50 ms |
26776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1556 ms |
44808 KB |
Output is correct |
2 |
Correct |
1479 ms |
45316 KB |
Output is correct |
3 |
Correct |
1380 ms |
45036 KB |
Output is correct |
4 |
Correct |
892 ms |
36828 KB |
Output is correct |
5 |
Correct |
756 ms |
41272 KB |
Output is correct |
6 |
Correct |
177 ms |
37500 KB |
Output is correct |
7 |
Correct |
65 ms |
37712 KB |
Output is correct |