#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 time |
Memory |
Grader output |
1 |
Correct |
20 ms |
30080 KB |
Output is correct |
2 |
Correct |
20 ms |
30080 KB |
Output is correct |
3 |
Correct |
22 ms |
30080 KB |
Output is correct |
4 |
Correct |
22 ms |
30080 KB |
Output is correct |
5 |
Correct |
20 ms |
30080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
368 ms |
40684 KB |
Output is correct |
2 |
Correct |
358 ms |
40288 KB |
Output is correct |
3 |
Correct |
223 ms |
35436 KB |
Output is correct |
4 |
Correct |
94 ms |
38136 KB |
Output is correct |
5 |
Correct |
283 ms |
44904 KB |
Output is correct |
6 |
Correct |
91 ms |
38136 KB |
Output is correct |
7 |
Correct |
26 ms |
30208 KB |
Output is correct |
8 |
Correct |
23 ms |
30200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
30208 KB |
Output is correct |
2 |
Correct |
24 ms |
30208 KB |
Output is correct |
3 |
Correct |
23 ms |
30208 KB |
Output is correct |
4 |
Correct |
27 ms |
30208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
885 ms |
40684 KB |
Output is correct |
2 |
Correct |
707 ms |
40296 KB |
Output is correct |
3 |
Correct |
421 ms |
35560 KB |
Output is correct |
4 |
Correct |
395 ms |
42224 KB |
Output is correct |
5 |
Correct |
186 ms |
39848 KB |
Output is correct |
6 |
Correct |
100 ms |
39928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1204 ms |
40788 KB |
Output is correct |
2 |
Correct |
1176 ms |
40680 KB |
Output is correct |
3 |
Correct |
1161 ms |
40164 KB |
Output is correct |
4 |
Correct |
810 ms |
35432 KB |
Output is correct |
5 |
Correct |
673 ms |
42220 KB |
Output is correct |
6 |
Correct |
195 ms |
39792 KB |
Output is correct |
7 |
Correct |
104 ms |
39800 KB |
Output is correct |