题解:P13714 淘汰(Hard ver.)
一、前言
这是本蒟蒻第一次抢写 LG 月赛的题解。
本题作为 C 题确实有点难度,而且十分巧妙地用到了 SOS DP(又称高位前缀,子集 DP)难度大约为紫。
视频讲解有些快,本蒟蒻措手不及,故理解后写下本篇题解。
二、题意&暴力
题意说有一个数
首先我们考虑完成
代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N (1<<16)
int n,k,x,y,a[N+10],b[N+10],c[N+10],d[N+10],dp[N+10],vis[N+10];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t; cin>>t;
while (t--){
cin>>n>>k>>x>>y;
for (int i=1; i<=n; i++){
cin>>a[i];
}
for (int i=1; i<=n; i++){
cin>>b[i];
}
for (int i=1; i<=n; i++){
cin>>c[i];
}
for (int i=1; i<=n; i++){
cin>>d[i];
}
k=(1<<k);
for (int i=0; i<=k; i++) dp[i]=(int)1e18,vis[i]=0;
dp[x]=0;
priority_queue <pair<int,int>,vector <pair<int,int>>,greater<pair<int,int>> > q; q.push({0,x});
while (q.size()){
int tmp=q.top().second; q.pop();
if (vis[tmp]) continue; vis[tmp]=1;
for (int i=1; i<=n; i++){
if (dp[(tmp&a[i])]>dp[tmp]+c[i]){
dp[(tmp&a[i])]=dp[tmp]+c[i];
q.push({dp[(tmp&a[i])],(tmp&a[i])});
}
if (dp[(tmp|b[i])]>dp[tmp]+d[i]){
dp[(tmp|b[i])]=dp[tmp]+d[i];
q.push({dp[(tmp|b[i])],(tmp|b[i])});
}
}
}
cout<<(vis[y]?dp[y]:-1)<<"\n";
}
}
顺带说一句,如果你暴力
三、正解
正解的话,我们观察到
首先做初步转换,我们先把题目中