#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax =1e5 + 1;
const ll linf = 1e18 + 1;
ll d[nmax][32];
vector <vector <pll> > g(nmax);
int main(){
int n; cin >> n;
int k; cin >> k;
int m; cin >> m;
int imp[k + 1];
for(int i= 1; i <= n; i++)
for(int j = 0; j < 32; j++)
d[i][j] = linf;
for(int i = 1; i <= k; i++) {
cin >> imp[i];
}
for(int i= 1; i <= m; i++){
int u, v, len;
cin >> u >> v >> len;
g[u].epb(v, len);
g[v].epb(u, len);
}
for(int i = 0; i < k; i++){
d[imp[i + 1]][(1 << i)] = 0;
}
for(int mask = 1; mask < (1 << k); mask++){
for(int submask = 1; submask < mask; submask++){
if((mask | submask) != mask) continue;
for(int j = 1; j <= n; j++)
d[j][mask] = min(d[j][mask], d[j][submask] + d[j][mask ^ submask]);
}
priority_queue<pll> st;
for(int i = 1; i <= n; ++i){
if(d[i][mask] != linf)
st.push({-d[i][mask], i});
}
vector <bool> used(n + 1);
while(!st.empty()){
int v = st.top().s;
st.pop();
if(used[v]) continue;
used[v] = true;
for(auto x : g[v]){
int to = x.f;
ll len = x.s;
if(d[v][mask] + len < d[to][mask]){
d[to][mask] = d[v][mask] + len;
st.push({-d[to][mask], to});
}
}
}
}
cout << d[imp[1]][(1 << k) - 1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
759 ms |
44436 KB |
Output is correct |
2 |
Correct |
709 ms |
43924 KB |
Output is correct |
3 |
Correct |
436 ms |
36528 KB |
Output is correct |
4 |
Correct |
172 ms |
11920 KB |
Output is correct |
5 |
Correct |
504 ms |
42348 KB |
Output is correct |
6 |
Correct |
160 ms |
11908 KB |
Output is correct |
7 |
Correct |
5 ms |
3028 KB |
Output is correct |
8 |
Correct |
4 ms |
3020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
3028 KB |
Output is correct |
2 |
Correct |
7 ms |
3028 KB |
Output is correct |
3 |
Correct |
5 ms |
2900 KB |
Output is correct |
4 |
Correct |
5 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1354 ms |
44572 KB |
Output is correct |
2 |
Correct |
1300 ms |
43940 KB |
Output is correct |
3 |
Correct |
845 ms |
36680 KB |
Output is correct |
4 |
Correct |
788 ms |
29240 KB |
Output is correct |
5 |
Correct |
262 ms |
14968 KB |
Output is correct |
6 |
Correct |
206 ms |
14160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2664 ms |
44720 KB |
Output is correct |
2 |
Correct |
2716 ms |
48736 KB |
Output is correct |
3 |
Correct |
2619 ms |
48296 KB |
Output is correct |
4 |
Correct |
1917 ms |
38888 KB |
Output is correct |
5 |
Correct |
1468 ms |
33304 KB |
Output is correct |
6 |
Correct |
409 ms |
19000 KB |
Output is correct |
7 |
Correct |
183 ms |
17424 KB |
Output is correct |