제출 #240306

#제출 시각아이디문제언어결과실행 시간메모리
240306alishahali1382Cities (BOI16_cities)C++14
100 / 100
1204 ms44904 KiB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=200010, K=4;

ll n, m, k, u, v, x, y, t, a, b, ans, root;
ll dp[1<<K][MAXN], V[K];
bool mark[MAXN];
vector<pii> G[MAXN];
priority_queue<pll, vector<pll>, greater<pll>> pq;

inline void upd(ll &x, ll y){ x=min(x, y);}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	memset(dp, 63, sizeof(dp));
	cin>>n>>k>>m>>root;
	k--;
	for (int i=0; i<k; i++) cin>>V[i];
	while (m--){
		cin>>u>>v>>x;
		G[u].pb({v, x});
		G[v].pb({u, x});
	}
	for (int v=1; v<=n; v++) dp[0][v]=0;
	for (int mask=0; mask<(1<<k); mask++){
		for (int v=1; v<=n; v++){
			for (int sub=(mask-1)&mask; sub; sub=(sub-1)&mask)
				dp[mask][v]=min(dp[mask][v], dp[sub][v] + dp[mask^sub][v]);
			mark[v]=0;
			pq.push({dp[mask][v], v});
		}
		while (pq.size()){
			int v=pq.top().second;
			pq.pop();
			if (mark[v]) continue ;
			mark[v]=1;
			for (pii p:G[v]){
				int u=p.first, w=p.second;
				if (dp[mask][v]+w<dp[mask][u]){
					dp[mask][u]=dp[mask][v]+w;
					pq.push({dp[mask][u], u});
				}
			}
		}
		for (int v=1; v<=n; v++)
			for (int i=0; i<k; i++)
				if (V[i]==v) upd(dp[mask|(1<<i)][v], dp[mask][v]);
	}
	ans=dp[(1<<k)-1][root];
	cout<<ans<<'\n';
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...