#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("VNOICUP.INP");
ofstream fout("VNOICUP.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
using u128 = __uint128_t;
//const int x[4] = {1, -1, 0, 0};
//const int y[4] = {0, 0, 1, -1};
const ll mod = 998244353;
const int mxn = 1e5 + 5, mxq = 2e5 + 5, sq = 400, mxv = 2e7 + 5;
//const int base = (1 << 18);
const ll inf = 1e17, neg = -69420;
int n, k, m;
int to[mxn + 1];
vt<pll>adj[mxn + 1];
ll dp[mxn + 1][(1 << 5)];
void input(){
cin >> n >> k >> m;
for(int i = 1; i <= n; i++)to[i] = -1;
for(int i = 0; i < k; i++){
int x; cin >> x; to[x] = i;
}
for(int i = 0; i < m; i++){
int u, v, w; cin >> u >> v >> w;
adj[u].pb({v, w}); adj[v].pb({u, w});
}
}
/*
4 3 6
1 3 4
1 2 4
1 3 9
1 4 6
2 3 2
2 4 5
3 4 8
*/
void process(){
for(int i = 1; i <= n; i++){
for(int j = 0; j < (1 << k); j++){
dp[i][j] = inf;
}
}
for(int i = 1; i <= n; i++){
if(to[i] != -1){
dp[i][(1 << to[i])] = 0;
}
dp[i][0] = 0;
}
for(int i = 1; i < (1 << k); i++){
priority_queue<pll, vt<pll>, greater<pll>>pq;
for(int j = 1; j <= n; j++){
for(int l = 0;l < i; l++){
if((i | l) == i)dp[j][i] = min(dp[j][i], dp[j][i ^ l] + dp[j][l]);
}
if(dp[j][i] != inf){
pq.push(make_pair(dp[j][i], j));
}
}
while(!pq.empty()){
auto [dd, u] = pq.top(); pq.pop();
if(dp[u][i] != dd)continue;
for(auto [v, w]: adj[u]){
if(dp[v][i] > dp[u][i] + w){
dp[v][i] = dp[u][i] + w;
pq.push({dp[v][i], v});
}
}
}
}
ll ans = inf;
for(int i = 1; i <= n; i++){
ans = min(ans, dp[i][(1 << k) - 1]);
}
cout << ans;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
input();
process();
//solve();
return(0);
}
Compilation message
cities.cpp: In function 'void process()':
cities.cpp:75:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
75 | auto [dd, u] = pq.top(); pq.pop();
| ^
cities.cpp:77:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
77 | for(auto [v, w]: adj[u]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2664 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
602 ms |
49080 KB |
Output is correct |
2 |
Correct |
533 ms |
48520 KB |
Output is correct |
3 |
Correct |
338 ms |
39208 KB |
Output is correct |
4 |
Correct |
48 ms |
15416 KB |
Output is correct |
5 |
Correct |
262 ms |
46912 KB |
Output is correct |
6 |
Correct |
44 ms |
15464 KB |
Output is correct |
7 |
Correct |
4 ms |
3028 KB |
Output is correct |
8 |
Correct |
3 ms |
3084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3028 KB |
Output is correct |
2 |
Correct |
5 ms |
3028 KB |
Output is correct |
3 |
Correct |
4 ms |
2960 KB |
Output is correct |
4 |
Correct |
4 ms |
2976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1225 ms |
49064 KB |
Output is correct |
2 |
Correct |
1186 ms |
48516 KB |
Output is correct |
3 |
Correct |
767 ms |
39084 KB |
Output is correct |
4 |
Correct |
606 ms |
33592 KB |
Output is correct |
5 |
Correct |
130 ms |
18888 KB |
Output is correct |
6 |
Correct |
67 ms |
17540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2494 ms |
49068 KB |
Output is correct |
2 |
Correct |
2468 ms |
49284 KB |
Output is correct |
3 |
Correct |
2403 ms |
48492 KB |
Output is correct |
4 |
Correct |
1656 ms |
39212 KB |
Output is correct |
5 |
Correct |
1134 ms |
33688 KB |
Output is correct |
6 |
Correct |
207 ms |
19064 KB |
Output is correct |
7 |
Correct |
62 ms |
17352 KB |
Output is correct |