# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1028813 |
2024-07-20T09:04:43 Z |
phoenix |
Cities (BOI16_cities) |
C++17 |
|
1628 ms |
47164 KB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100100;
const int M = 200200;
const ll INF = 1e15;
int n, m, k;
vector<pair<int, ll>> g[N];
int special[5];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> m;
for (int i = 0; i < k; i++) {
cin >> special[i];
special[i]--;
}
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
ll dp[(1 << k)][n] = {};
for (int i = 1; i < (1 << k); i++)
for (int j = 0; j < n; j++)
dp[i][j] = INF;
for (int i = 0; i < k; i++)
dp[(1 << i)][special[i]] = 0;
for (int msk = 1; msk < (1 << k); msk++) {
for (int v = 0; v < n; v++)
for (int sub = msk; sub && sub > (msk ^ sub); sub = (msk & sub - 1))
dp[msk][v] = min(dp[msk][v], dp[sub][v] + dp[(msk ^ sub)][v]);
priority_queue<pair<ll, int>> pq;
for (int i = 0; i < n; i++)
pq.push({-dp[msk][i], i});
while (!pq.empty()) {
auto [d, x] = pq.top();
pq.pop();
if (dp[msk][x] != -d) continue;;
for (auto [to, w] : g[x]) {
if (dp[msk][to] > dp[msk][x] + w) {
dp[msk][to] = dp[msk][x] + w;
pq.push({-dp[msk][to], to});
}
}
}
}
cout << *min_element(dp[(1 << k) - 1], dp[(1 << k) - 1] + n);
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:43:76: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
43 | for (int sub = msk; sub && sub > (msk ^ sub); sub = (msk & sub - 1))
| ~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2720 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
410 ms |
28076 KB |
Output is correct |
2 |
Correct |
400 ms |
27608 KB |
Output is correct |
3 |
Correct |
233 ms |
19220 KB |
Output is correct |
4 |
Correct |
47 ms |
13968 KB |
Output is correct |
5 |
Correct |
212 ms |
24908 KB |
Output is correct |
6 |
Correct |
42 ms |
13816 KB |
Output is correct |
7 |
Correct |
4 ms |
2820 KB |
Output is correct |
8 |
Correct |
2 ms |
3044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3136 KB |
Output is correct |
2 |
Correct |
7 ms |
3080 KB |
Output is correct |
3 |
Correct |
5 ms |
3048 KB |
Output is correct |
4 |
Correct |
4 ms |
3068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
863 ms |
34500 KB |
Output is correct |
2 |
Correct |
834 ms |
33932 KB |
Output is correct |
3 |
Correct |
499 ms |
25472 KB |
Output is correct |
4 |
Correct |
445 ms |
25532 KB |
Output is correct |
5 |
Correct |
122 ms |
16400 KB |
Output is correct |
6 |
Correct |
64 ms |
16168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1616 ms |
47012 KB |
Output is correct |
2 |
Correct |
1628 ms |
47164 KB |
Output is correct |
3 |
Correct |
1566 ms |
46476 KB |
Output is correct |
4 |
Correct |
1129 ms |
37996 KB |
Output is correct |
5 |
Correct |
898 ms |
31816 KB |
Output is correct |
6 |
Correct |
181 ms |
17948 KB |
Output is correct |
7 |
Correct |
60 ms |
16536 KB |
Output is correct |