#include <bits/stdc++.h>
using namespace std;
//types
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define pw2(x) (1LL << (x))
#define getBit(x, y) ((x) & (1LL << (y)))
template<class X, class Y>
bool cmax(X &a, const Y &b) {
return a < b ? a = b, 1 : 0;
}
template<class X, class Y>
bool cmin(X &a, const Y &b) {
return a > b ? a = b, 1 : 0;
}
//lowercase 31, all 53
//(P/Q) % M = (P % M) * (Q^(M-2) % M)
//-------------------------------------------------------
const ld PI = 3.14159265359;
const ll N = 1e5+5, mod = 1e9+7;
int n, k, m;
vector<int> imp;
vector<pii> adj[N];
ll f[N][1 << 5];
using T = tuple<ll, int, int>;
priority_queue<T, vector<T>, greater<T>> pq;
ll dijkstra() {
memset(f, 0x3f, sizeof(f));
for (int i = 0; i < k; i++) {
f[imp[i]][1 << i] = 0;
pq.push({0, imp[i], 1 << i});
}
for (int i = 1; i <= n; i++)
f[i][0] = 0;
while (!pq.empty()) {
ll d; int u, mask;
tie(d, u, mask) = pq.top(); pq.pop();
if (d != f[u][mask]) continue;
for (auto e : adj[u]) {
int v, w; tie(v, w) = e;
for (int mask2 = 0; mask2 < (1 << k); mask2++) {
int newMask = mask | mask2;
if (cmin(f[v][newMask], f[u][mask] + f[v][mask2] + w)) {
pq.push({f[v][newMask], v, newMask});
}
}
}
}
ll ans = 1e16;
for (int i = 1; i <= n; i++) {
cmin(ans, f[i][(1 << k) - 1]);
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
cin >> n >> k >> m;
for (int i = 1; i <= k; i++) {
int tmp; cin >> tmp;
imp.push_back(tmp);
}
for (int i = 1; i <= m; i++) {
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
cout << dijkstra();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
27604 KB |
Output is correct |
2 |
Correct |
12 ms |
27696 KB |
Output is correct |
3 |
Correct |
12 ms |
27764 KB |
Output is correct |
4 |
Correct |
11 ms |
27604 KB |
Output is correct |
5 |
Correct |
11 ms |
27732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
906 ms |
54632 KB |
Output is correct |
2 |
Correct |
833 ms |
54292 KB |
Output is correct |
3 |
Correct |
413 ms |
37144 KB |
Output is correct |
4 |
Correct |
106 ms |
36752 KB |
Output is correct |
5 |
Correct |
334 ms |
42304 KB |
Output is correct |
6 |
Correct |
77 ms |
36192 KB |
Output is correct |
7 |
Correct |
15 ms |
28024 KB |
Output is correct |
8 |
Correct |
14 ms |
27892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
28416 KB |
Output is correct |
2 |
Correct |
19 ms |
28460 KB |
Output is correct |
3 |
Correct |
16 ms |
28056 KB |
Output is correct |
4 |
Correct |
18 ms |
28420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2785 ms |
104184 KB |
Output is correct |
2 |
Correct |
2571 ms |
103596 KB |
Output is correct |
3 |
Correct |
1364 ms |
49484 KB |
Output is correct |
4 |
Correct |
1986 ms |
70608 KB |
Output is correct |
5 |
Correct |
497 ms |
52664 KB |
Output is correct |
6 |
Correct |
224 ms |
39488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6017 ms |
169644 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |