# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1007703 |
2024-06-25T10:58:30 Z |
otarius |
Cities (BOI16_cities) |
C++17 |
|
1802 ms |
45868 KB |
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, int>
#define pii pair <int, int>
#define ull unsigned long long
// #define int long long
// #define int unsigned long long
const ll inf = 1e18 + 7;
const ll weirdMod = 998244353;
vector<vector<pii>> G;
void solve() {
int n, k, m;
cin >> n >> k >> m; G.resize(n + 1);
ll dp[n + 1][1 << k]; int f[k];
for (int i = 1; i <= n; i++)
for (int j = 0; j < (1 << k); j++) dp[i][j] = inf;
for (int i = 0; i < k; i++) {
cin >> f[i]; dp[f[i]][1 << i] = 0;
} for (int i = 1; i <= m; i++) {
int x, y, z;
cin >> x >> y >> z;
G[x].pb({y, z}); G[y].pb({x, z});
} for (int msk = 1; msk < (1 << k); msk++) {
for (int j = 0; j < k; j++) {
if (!(msk & (1 << j))) continue;
int msk2 = (msk ^ (1 << j));
for (int i = 1; i <= n; i++)
dp[i][msk] = min(dp[i][msk], dp[i][1 << j] + dp[i][msk2]);
} priority_queue<pll, vector<pll>, greater<pll>> q;
for (int i = 1; i <= n; i++)
q.push({dp[i][msk], i});
while (q.size()) {
auto [w, u] = q.top(); q.pop();
if (dp[u][msk] != w) continue;
for (auto [v, wgt] : G[u]) {
if (dp[v][msk] > dp[u][msk] + wgt)
q.push({dp[v][msk] = dp[u][msk] + wgt, v});
}
}
} ll mn = inf;
for (int i = 1; i <= n; i++) {
mn = min(mn, dp[i][(1 << k) - 1]);
} cout << mn;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
359 ms |
27216 KB |
Output is correct |
2 |
Correct |
333 ms |
26760 KB |
Output is correct |
3 |
Correct |
215 ms |
17932 KB |
Output is correct |
4 |
Correct |
39 ms |
8652 KB |
Output is correct |
5 |
Correct |
172 ms |
23468 KB |
Output is correct |
6 |
Correct |
34 ms |
8528 KB |
Output is correct |
7 |
Correct |
3 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
600 KB |
Output is correct |
4 |
Correct |
3 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
775 ms |
33324 KB |
Output is correct |
2 |
Correct |
735 ms |
32260 KB |
Output is correct |
3 |
Correct |
521 ms |
24200 KB |
Output is correct |
4 |
Correct |
381 ms |
21448 KB |
Output is correct |
5 |
Correct |
110 ms |
12076 KB |
Output is correct |
6 |
Correct |
46 ms |
10420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1794 ms |
45716 KB |
Output is correct |
2 |
Correct |
1765 ms |
45160 KB |
Output is correct |
3 |
Correct |
1802 ms |
45868 KB |
Output is correct |
4 |
Correct |
1129 ms |
36728 KB |
Output is correct |
5 |
Correct |
745 ms |
27628 KB |
Output is correct |
6 |
Correct |
170 ms |
13212 KB |
Output is correct |
7 |
Correct |
47 ms |
10532 KB |
Output is correct |