#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;
template<typename T>
istream &operator>>(istream &is, vector<T> &a) {
for (auto &i : a) {
is >> i;
}
return is;
}
int32_t main() {
#ifdef LOCAL
freopen("/tmp/input.txt", "r", stdin);
#else
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int n, k, m;
cin >> n >> k >> m;
vector<int> a(k);
cin >> a;
for (auto &i : a) {
--i;
}
vector<vector<pair<int, int>>> g(n);
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});
}
vector<vector<ll>> dp(1 << k, vector<ll>(n, infll));
using pii = pair<ll, pair<int, int>>;
set<pii> pq;
for (int i = 0; i < k; ++i) {
dp[1 << i][a[i]] = 0;
pq.insert({dp[1 << i][a[i]], {1 << i, a[i]}});
}
while (!pq.empty()) {
auto [mask, v] = pq.begin()->second;
pq.erase(pq.begin());
for (auto [u, w] : g[v]) {
if (dp[mask][u] > dp[mask][v] + w) {
pq.erase({dp[mask][u], {mask, u}});
dp[mask][u] = dp[mask][v] + w;
pq.insert({dp[mask][u], {mask, u}});
}
for (int upmask = mask;; upmask = (upmask + 1) | mask) {
if (dp[upmask][u] > dp[mask][v] + dp[upmask ^ mask][u] + w) {
pq.erase({dp[upmask][u], {upmask, u}});
dp[upmask][u] = dp[mask][v] + dp[upmask ^ mask][u] + w;
pq.insert({dp[upmask][u], {upmask, u}});
}
if (upmask == (1 << k) - 1) {
break;
}
}
}
}
ll ans = infll;
for (int i = 0; i < n; ++i) {
ans = min(ans, dp[(1 << k) - 1][i]);
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
396 KB |
Output is correct |
3 |
Correct |
1 ms |
508 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2089 ms |
41536 KB |
Output is correct |
2 |
Correct |
1693 ms |
37212 KB |
Output is correct |
3 |
Correct |
399 ms |
14728 KB |
Output is correct |
4 |
Correct |
70 ms |
4944 KB |
Output is correct |
5 |
Correct |
397 ms |
20592 KB |
Output is correct |
6 |
Correct |
48 ms |
4856 KB |
Output is correct |
7 |
Correct |
5 ms |
864 KB |
Output is correct |
8 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
1104 KB |
Output is correct |
2 |
Correct |
13 ms |
1104 KB |
Output is correct |
3 |
Correct |
6 ms |
848 KB |
Output is correct |
4 |
Correct |
9 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6039 ms |
90236 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6054 ms |
174052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |