#include<bits/stdc++.h>
#define f first
#define s second
#define pii pair<int,int>
#define int long long
#define Pii pair<int,pii>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7, inf = 1e16; // !
int t,n,m,dp[N][35],k;
vector<pii> V[N];
priority_queue<Pii, vector<Pii>, greater<Pii> > q;
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;
q.push({0, {b, 1 << (i - 1)}});
}
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});
}
while(q.size()) {
int d = q.top().f;
int u = q.top().s.f;
int t = q.top().s.s;
q.pop();
if(d > dp[u][t]) continue;
int x = ((1 << k) - 1) ^ t;
for(int i = x; ; i = (i - 1) & x) {
for(int j = 0; j < V[u].size(); j++) {
if(dp[V[u][j].f][t ^ i] > V[u][j].s + d + dp[V[u][j].f][i]) {
dp[V[u][j].f][t ^ i] = V[u][j].s + d + dp[V[u][j].f][i];
q.push({V[u][j].s + d + dp[V[u][j].f][i], {V[u][j].f, t ^ i}});
}
}
if(i == 0) break;
}
}
int ans = inf;
for(int i = 1; i <= n; i++) {
ans = min(ans, dp[i][(1 << k) - 1]);
}
cout << ans;
}
Compilation message
cities.cpp:12:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
12 | main() {
| ^~~~
cities.cpp: In function 'int main()':
cities.cpp:37: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]
37 | for(int j = 0; j < V[u].size(); j++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1096 ms |
67584 KB |
Output is correct |
2 |
Correct |
1050 ms |
71020 KB |
Output is correct |
3 |
Correct |
524 ms |
45952 KB |
Output is correct |
4 |
Correct |
187 ms |
19136 KB |
Output is correct |
5 |
Correct |
511 ms |
53408 KB |
Output is correct |
6 |
Correct |
179 ms |
18508 KB |
Output is correct |
7 |
Correct |
7 ms |
5692 KB |
Output is correct |
8 |
Correct |
5 ms |
5504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
6216 KB |
Output is correct |
2 |
Correct |
12 ms |
6220 KB |
Output is correct |
3 |
Correct |
8 ms |
5776 KB |
Output is correct |
4 |
Correct |
10 ms |
6088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3148 ms |
141360 KB |
Output is correct |
2 |
Correct |
3054 ms |
140708 KB |
Output is correct |
3 |
Correct |
1578 ms |
62344 KB |
Output is correct |
4 |
Correct |
2001 ms |
82736 KB |
Output is correct |
5 |
Correct |
551 ms |
45136 KB |
Output is correct |
6 |
Correct |
260 ms |
22892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
6039 ms |
239804 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |