#include<bits/stdc++.h>
#define f first
#define s second
#define pii pair<int,int>
#define int long long
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7, inf = 1e16; // !
int t,n,m,dp[N][35],k;
priority_queue<pii, vector<pii>, greater<pii> > q;
vector<pii> V[N];
main() {
cin >> n >> k >> m;
for(int i = 1; i <= n; i++) for(int j = 1; j < (1 << k); j++) dp[i][j] = inf;
for(int i = 1; i <= k; i++) {
int b;
cin >> b;
dp[b][1 << (i - 1)] = 0;
}
for(int i = 1; i <= m; i++) {
int u,v,w;
cin >> u >> v >> w;
V[u].push_back({v,w});
V[v].push_back({u,w});
}
for(int j = 1; j < (1 << k); j++) {
vector<int> sub;
for(int m = j; ; m = (m - 1) & j) {
sub.push_back(m);
if(!m) break;
}
for(int i = 0; i < sub.size(); i++) {
int a = sub[i];
for(int u = 1; u <= n; u++) {
for(int id = 0; id < V[u].size(); id++) {
dp[u][j] = min(dp[u][j], dp[V[u][id].f][sub[i]] + V[u][id].s + dp[u][sub[i] ^ j]);
}
}
}
for(int i = 1; i <= n; i++) q.push({dp[i][j], i});
while(q.size()) {
int d = q.top().f;
int u = q.top().s;
q.pop();
if(d > dp[u][j]) continue;
for(int i = 0; i < V[u].size(); i++) {
if(dp[V[u][i].f][j] > dp[u][j] + V[u][i].s) {
dp[V[u][i].f][j] = dp[u][j] + V[u][i].s;
q.push({dp[V[u][i].f][j], V[u][i].f});
}
}
}
}
int ans = inf;
for(int i = 1; i <= n; i++) {
ans = min(ans, dp[i][(1 << k) - 1]);
}
cout << ans;
}
Compilation message
cities.cpp:11:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
11 | main() {
| ^~~~
cities.cpp: In function 'int main()':
cities.cpp:33:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i = 0; i < sub.size(); i++) {
| ~~^~~~~~~~~~~~
cities.cpp:36:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(int id = 0; id < V[u].size(); id++) {
| ~~~^~~~~~~~~~~~~
cities.cpp:34:8: warning: unused variable 'a' [-Wunused-variable]
34 | int a = sub[i];
| ^
cities.cpp:47:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1097 ms |
46872 KB |
Output is correct |
2 |
Correct |
1066 ms |
45996 KB |
Output is correct |
3 |
Correct |
656 ms |
39764 KB |
Output is correct |
4 |
Correct |
194 ms |
14164 KB |
Output is correct |
5 |
Correct |
547 ms |
46892 KB |
Output is correct |
6 |
Correct |
181 ms |
14148 KB |
Output is correct |
7 |
Correct |
6 ms |
5324 KB |
Output is correct |
8 |
Correct |
5 ms |
5324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5436 KB |
Output is correct |
2 |
Correct |
9 ms |
5328 KB |
Output is correct |
3 |
Correct |
6 ms |
5324 KB |
Output is correct |
4 |
Correct |
7 ms |
5284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2509 ms |
46960 KB |
Output is correct |
2 |
Correct |
2222 ms |
45964 KB |
Output is correct |
3 |
Correct |
1509 ms |
39736 KB |
Output is correct |
4 |
Correct |
1577 ms |
31304 KB |
Output is correct |
5 |
Correct |
460 ms |
17372 KB |
Output is correct |
6 |
Correct |
249 ms |
16324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5560 ms |
47028 KB |
Output is correct |
2 |
Correct |
5678 ms |
46900 KB |
Output is correct |
3 |
Correct |
5144 ms |
45968 KB |
Output is correct |
4 |
Correct |
3458 ms |
39684 KB |
Output is correct |
5 |
Correct |
3540 ms |
35520 KB |
Output is correct |
6 |
Correct |
888 ms |
21660 KB |
Output is correct |
7 |
Correct |
469 ms |
19752 KB |
Output is correct |