#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long ll;
typedef pair<ll,int> ii;
const int N = 1e5;
const int K = 5;
const int MX = (1<<K);
const ll INF = 1e18;
int n, m, k;
int s[K+2];
ll dp[N+2][MX+2];
vector<ii> adj[N+2];
void djikstra(int mask) {
priority_queue<ii,vector<ii>,greater<ii>> pq;
for (int i = 0; i < n; i++) pq.push({dp[i][mask],i});
while (!pq.empty()) {
ii cur = pq.top(); pq.pop();
if (dp[cur.se][mask] != cur.fi) continue;
for (auto i : adj[cur.se]) {
ii nx = {cur.fi+i.se,i.fi};
if (dp[nx.se][mask] > nx.fi) {
dp[nx.se][mask] = nx.fi;
pq.push(nx);
}
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k >> m;
for (int i = 0; i < k; i++) {
cin >> s[i]; s[i]--;
}
for (int i = 0; i < m; i++) {
int u, v, w; cin >> u >> v >> w;
u--, v--;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < (1<<k); j++) {
dp[i][j] = INF;
}
}
for (int mask = 1; mask < (1<<k); mask++) {
if (__builtin_popcount(mask) == 1) {
for (int i = 0; i < k; i++) {
if ((mask>>i)&1) dp[s[i]][mask] = 0;
}
}
for (int i = 0; i < k; i++) {
if ((mask>>i)&1) {
for (int j = 0; j < n; j++) {
dp[j][mask] = min(dp[j][mask],dp[j][1<<i]+dp[j][mask^(1<<i)]);
}
}
}
djikstra(mask);
}
ll ans = INF;
for (int i = 0; i < n; i++) {
ans = min(ans,dp[i][(1<<k)-1]);
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
887 ms |
50520 KB |
Output is correct |
2 |
Correct |
876 ms |
50112 KB |
Output is correct |
3 |
Correct |
550 ms |
40368 KB |
Output is correct |
4 |
Correct |
86 ms |
15448 KB |
Output is correct |
5 |
Correct |
460 ms |
50400 KB |
Output is correct |
6 |
Correct |
79 ms |
15504 KB |
Output is correct |
7 |
Correct |
5 ms |
3180 KB |
Output is correct |
8 |
Correct |
4 ms |
3308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
3180 KB |
Output is correct |
2 |
Correct |
8 ms |
3180 KB |
Output is correct |
3 |
Correct |
6 ms |
3052 KB |
Output is correct |
4 |
Correct |
5 ms |
3052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1739 ms |
50516 KB |
Output is correct |
2 |
Correct |
1732 ms |
50340 KB |
Output is correct |
3 |
Correct |
1132 ms |
40588 KB |
Output is correct |
4 |
Correct |
961 ms |
34388 KB |
Output is correct |
5 |
Correct |
216 ms |
19756 KB |
Output is correct |
6 |
Correct |
91 ms |
17748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3458 ms |
50700 KB |
Output is correct |
2 |
Correct |
3447 ms |
50760 KB |
Output is correct |
3 |
Correct |
3380 ms |
50056 KB |
Output is correct |
4 |
Correct |
2311 ms |
40700 KB |
Output is correct |
5 |
Correct |
1845 ms |
34784 KB |
Output is correct |
6 |
Correct |
344 ms |
19864 KB |
Output is correct |
7 |
Correct |
109 ms |
17916 KB |
Output is correct |