#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=1e5+5;
ll n, m, k, x, dp[33][nx], u, v, w, res=LLONG_MAX;
vector<pair<ll, ll>> d[nx];
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>k>>m;
for (int i=1; i<(1<<k); i++) for (int j=1; j<=n; j++) dp[i][j]=1e18;
for (int i=0; i<k; i++) cin>>x, dp[(1<<i)][x]=0;
for (int i=0; i<m; i++) cin>>u>>v>>w, d[u].push_back({v, w}), d[v].push_back({u, w});
for (int i=1; i<(1<<k); i++)
{
for (int j=1; j<=n; j++) for (int k=i; k>0; k=(k-1)&i) dp[i][j]=min(dp[i][j], dp[i^k][j]+dp[k][j]);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
for (int j=1; j<=n; j++) pq.push({dp[i][j], j});
while (!pq.empty())
{
auto [w, u]=pq.top();
pq.pop();
for (auto [v, nw]:d[u]) if (w+nw<dp[i][v]) dp[i][v]=w+nw, pq.push({w+nw, v});
}
}
for (int i=1; i<=n; i++) res=min(res, dp[(1<<k)-1][i]);
cout<<res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
544 ms |
32388 KB |
Output is correct |
2 |
Correct |
423 ms |
31536 KB |
Output is correct |
3 |
Correct |
274 ms |
22000 KB |
Output is correct |
4 |
Correct |
69 ms |
21332 KB |
Output is correct |
5 |
Correct |
231 ms |
28692 KB |
Output is correct |
6 |
Correct |
49 ms |
19228 KB |
Output is correct |
7 |
Correct |
4 ms |
9048 KB |
Output is correct |
8 |
Correct |
3 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15192 KB |
Output is correct |
2 |
Correct |
6 ms |
15196 KB |
Output is correct |
3 |
Correct |
5 ms |
14940 KB |
Output is correct |
4 |
Correct |
5 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
986 ms |
38448 KB |
Output is correct |
2 |
Correct |
951 ms |
37912 KB |
Output is correct |
3 |
Correct |
602 ms |
28308 KB |
Output is correct |
4 |
Correct |
547 ms |
33392 KB |
Output is correct |
5 |
Correct |
136 ms |
29264 KB |
Output is correct |
6 |
Correct |
61 ms |
29652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2033 ms |
48944 KB |
Output is correct |
2 |
Correct |
2035 ms |
49872 KB |
Output is correct |
3 |
Correct |
1795 ms |
49628 KB |
Output is correct |
4 |
Correct |
1123 ms |
39000 KB |
Output is correct |
5 |
Correct |
925 ms |
45768 KB |
Output is correct |
6 |
Correct |
209 ms |
41728 KB |
Output is correct |
7 |
Correct |
80 ms |
42284 KB |
Output is correct |