#include <bits/stdc++.h>
/*
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target ("avx2")
*/
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define F first
#define S second
#define pb push_back
#define FIO freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout)
#define md(a) ((a%mod+mod)%mod)
#define all(a) a.begin(), a.end()
#define MP make_pair
#define lc (id<<1)
#define rc (lc|1)
#define mid (l+r)/2
#define kill(a) cout << a << "\n", exit(0)
#define SZ(a) (ll)a.size()
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef long long ll;
typedef long double ld;
typedef vector<vector<ll>> matrix;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll const maxn=2e5+10, mod=1e9+7, INF=1e18, LOG=20, sq=65;
ll poww(ll a, ll b, ll mod) {
if (b == 0) return 1;
return 1 * poww(1 * a * a % mod, b / 2, mod) * ((b % 2 == 1) ? a : 1) % mod;
}
ll n, k, m, root, dp[(1ll<<5)][maxn], A[6];
priority_queue<pll> pq;
vector<pll> g[maxn];
int main()
{
// ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>k>>m>>root;
k--;
for(ll i=0;i<k;i++) cin>>A[i];
for(ll i=1;i<=m;i++)
{
ll v, u, w;
cin>>v>>u>>w;
g[v].pb({u, w});
g[u].pb({v, w});
}
for(ll msk=1;msk<(1ll<<k);msk++) for(ll i=1;i<=n;i++) dp[msk][i]=INF;
for(ll msk=0;msk<(1ll<<k);msk++)
{
for(ll v=1;v<=n;v++)
{
for(ll sub=(msk-1)&msk;sub;sub=(sub-1)&msk)
dp[msk][v]=min(dp[msk][v], dp[sub][v]+dp[msk^sub][v]);
pq.push({-dp[msk][v], v});
}
while(SZ(pq))
{
auto [d, v]=pq.top();
pq.pop();
d=-d;
if(dp[msk][v]!=d) continue;
for(auto [u, w]:g[v])
{
if(dp[msk][v]+w<dp[msk][u]){
dp[msk][u]=dp[msk][v]+w;
pq.push({-dp[msk][u], u});
}
}
}
for(ll i=0;i<k;i++) dp[msk|(1ll<<i)][A[i]]=min(dp[msk|(1ll<<i)][A[i]], dp[msk][A[i]]);
}
cout<<dp[(1ll<<k)-1][root]<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |