#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=998244353;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=100007,K=5;
ll DP[N][1<<K];
bool odw[N][1<<K];
vector<pair<int,ll>>G[N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,k,m,a,b,c;
cin>>n>>k>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<(1<<k);j++) DP[i][j]=infl;
}
for(int i=1;i<=k;i++)
{
cin>>a;
DP[a][(1<<(i-1))]=0;
}
for(int i=0;i<m;i++)
{
cin>>a>>b>>c;
G[a].pb({b,c});
G[b].pb({a,c});
}
for(int x=1;x<(1<<k);x++)
{
priority_queue<pair<ll,int>>Q;
for(int i=1;i<=n;i++) Q.push({-DP[i][x],i});
while(sz(Q)>0)
{
ll t=-Q.top().st;
int v=Q.top().nd;
Q.pop();
if(odw[v][x]) continue;
odw[v][x]=1;
for(auto u:G[v])
{
if(t+u.nd<DP[u.st][x])
{
DP[u.st][x]=t+u.nd;
Q.push({-DP[u.st][x],u.st});
}
}
}
for(int v=1;v<=n;v++)
{
for(auto u:G[v])
{
int d=(1<<k)-1-x;
for(int y=d;; y=(y-1)&d)
{
DP[u.st][x^y]=min(DP[u.st][x^y],DP[v][x]+u.nd+DP[u.st][y]);
if(y==0) break;
}
}
}
}
ll ans=infl;
for(int i=1;i<=n;i++) ans=min(ans,DP[i][(1<<k)-1]);
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2676 KB |
Output is correct |
4 |
Correct |
2 ms |
2684 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
861 ms |
48220 KB |
Output is correct |
2 |
Correct |
813 ms |
47528 KB |
Output is correct |
3 |
Correct |
509 ms |
39964 KB |
Output is correct |
4 |
Correct |
77 ms |
12276 KB |
Output is correct |
5 |
Correct |
447 ms |
47852 KB |
Output is correct |
6 |
Correct |
79 ms |
12208 KB |
Output is correct |
7 |
Correct |
5 ms |
3156 KB |
Output is correct |
8 |
Correct |
4 ms |
3128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
3176 KB |
Output is correct |
2 |
Correct |
8 ms |
3156 KB |
Output is correct |
3 |
Correct |
5 ms |
3080 KB |
Output is correct |
4 |
Correct |
5 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1789 ms |
48340 KB |
Output is correct |
2 |
Correct |
1716 ms |
47564 KB |
Output is correct |
3 |
Correct |
1132 ms |
40020 KB |
Output is correct |
4 |
Correct |
1000 ms |
31516 KB |
Output is correct |
5 |
Correct |
259 ms |
16296 KB |
Output is correct |
6 |
Correct |
123 ms |
14360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3742 ms |
48176 KB |
Output is correct |
2 |
Correct |
3678 ms |
51980 KB |
Output is correct |
3 |
Correct |
3565 ms |
51672 KB |
Output is correct |
4 |
Correct |
2451 ms |
41948 KB |
Output is correct |
5 |
Correct |
2322 ms |
35176 KB |
Output is correct |
6 |
Correct |
540 ms |
19924 KB |
Output is correct |
7 |
Correct |
249 ms |
17852 KB |
Output is correct |