/* cerberus97 - Hanit Banga */
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int N = 1e5 + 10, K = 5, S = (1 << K) + 10;
const ll inf = 1e15 + 42;
struct state_t {
int mask, u;
ll val;
bool operator<(const state_t &o) const {
return val > o.val;
}
};
int imp[K];
vector<pii> g[N];
vector<int> supermasks[S];
ll dp[N][S];
int main() {
fast_cin();
int n, k, m;
cin >> n >> k >> m;
for (int i = 0; i < k; ++i) {
cin >> imp[i];
}
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
int tot = (1 << k), all = tot - 1;
for (int u = 1; u <= n; ++u) {
for (int mask = 1; mask < tot; ++mask) {
dp[u][mask] = inf;
}
}
for (int mask = 0; mask < tot; ++mask) {
for (int smask = mask; smask < tot; ++smask) {
if ((smask & mask) == mask) {
supermasks[mask].pb(smask);
}
}
}
priority_queue<state_t> q;
for (int i = 0; i < k; ++i) {
dp[imp[i]][1 << i] = 0;
q.push({1 << i, imp[i], 0});
}
while (!q.empty()) {
auto cur = q.top();
q.pop();
int mask = cur.mask, u = cur.u;
if (dp[u][mask] != cur.val) {
continue;
}
for (auto &nmask : supermasks[mask]) {
int extra = nmask ^ mask;
for (auto &e : g[u]) {
int v = e.first, w = e.second;
ll cand = dp[u][mask] + w + dp[v][extra];
if (cand < dp[v][nmask]) {
dp[v][nmask] = cand;
q.push({nmask, v, cand});
}
}
}
}
ll ans = inf;
for (int u = 1; u <= n; ++u) {
ans = min(ans, dp[u][all]);
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2644 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1127 ms |
62488 KB |
Output is correct |
2 |
Correct |
948 ms |
62208 KB |
Output is correct |
3 |
Correct |
407 ms |
45028 KB |
Output is correct |
4 |
Correct |
114 ms |
12028 KB |
Output is correct |
5 |
Correct |
418 ms |
50408 KB |
Output is correct |
6 |
Correct |
93 ms |
11520 KB |
Output is correct |
7 |
Correct |
8 ms |
3448 KB |
Output is correct |
8 |
Correct |
7 ms |
3192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
3832 KB |
Output is correct |
2 |
Correct |
14 ms |
3828 KB |
Output is correct |
3 |
Correct |
9 ms |
3320 KB |
Output is correct |
4 |
Correct |
12 ms |
3544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3097 ms |
111836 KB |
Output is correct |
2 |
Correct |
2819 ms |
111264 KB |
Output is correct |
3 |
Correct |
1511 ms |
57588 KB |
Output is correct |
4 |
Correct |
1898 ms |
62276 KB |
Output is correct |
5 |
Correct |
469 ms |
31072 KB |
Output is correct |
6 |
Correct |
191 ms |
14700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6106 ms |
177628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |