真的不知道为啥了求助大佬qwq
在写这道题的时候用异或写的滚动数组
结果诡异TLE on #42
改成用i与运算就过了,但是不知道为啥
TLE code:
#include<bits/stdc++.h>
using namespace std;
namespace IO{
#define ll long long
#define ischar(ch) (32<ch&&ch<127)
#define SIZE 1<<18
char ibuf[SIZE],*is=ibuf,*it=ibuf;
#define getchar() (is==it&&(it=(is=ibuf)+fread(ibuf,1,SIZE,stdin),is==it)?EOF:*is++)
template<typename Type>
inline void read(Type &a){
Type x=0;int y=0,ch=getchar();
while(!isdigit(ch)&&(ch^EOF))y|=(ch=='-'),ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
a=y?-x:x;
}
inline void read(char &a){
int ch=getchar();
while(!ischar(ch)&&(ch^EOF))ch=getchar();
a=ch;
}
inline void read(string &a){
a="";char ch;
while(!ischar(ch)&&(ch^EOF))ch=getchar();
while(ischar(ch))a+=ch,ch=getchar();
}
inline void read(short &a){
int ch=getchar();
while(!isdigit(ch)&&(ch^EOF))ch=getchar();
a=(ch^48);
}
template<typename Type>
inline void read(Type a[]){
Type *pos=a;
int ch=getchar();
while(!ischar(ch)&&(ch^EOF))ch=getchar();
while(ischar(ch))*pos++=ch,ch=getchar();
*pos=0;
}
inline void read(double &a){
int zh=0,w=0,ch=getchar();
double xi=0.0,x=1.0;
while (!isdigit(ch)&&(ch^EOF))w|=ch=='-',ch=getchar();
while(isdigit(ch))zh=(zh<<1)+(zh<<3)+(ch^48),ch=getchar();
if(ch^'.')return void(a=w?-zh:zh);
ch=getchar();
while(isdigit(ch))x*=10.0,xi+=(ch^48)*1.0/x,ch=getchar();
a=w?-zh-xi:zh+xi;
}
template<typename Type,typename ...Args>
inline void read(Type &a,Args &...as){read(a);read(as...);}
char obuf[SIZE],*op=obuf;
inline void putch(const char &c){
if(op-obuf==SIZE)fwrite(obuf,1,op-obuf,stdout),op=obuf;
*op++=c;
}
template<typename Type>
inline void write(Type x){
if(x<0)putch('-'),x=-x;
static int s[35],top;top=0;
do s[top++]=x%10,x/=10;
while(x);
while(top)putch(s[--top]+'0');
}
int precision;
inline void write(double x){
ll n=1,base=10,k=precision;
while(k)n=(k&1)?n*base:n,base*=base,k>>=1;
x=round(x*n)/n;
if(!x){
putch('0'),putch('.');
for(int i=1;i<=precision;++i)putch('0');
return;
}
if(x<0)putch('-'),x=-x;
ll y=(ll)round(x*n)%n;x=(ll)x;
write((ll)x);putch('.');
int s[35],top=0;
for(;top<precision;y/=10)s[++top]=y%10;
while(top)putch(s[top--]+'0');
}
inline void write(char ch){putch(ch);}
inline void write(const char* s){for(int i=0;i<strlen(s);++i)putch(s[i]);}
inline void write(string s){for(int i=0;i<s.size();++i)putch(s[i]);}
template<typename Type,typename ...Args>
inline void write(Type a,Args ...as){write(a);write(as...);}
inline void oclear(){fwrite(obuf,1,op-obuf,stdout);}
#undef SIZE
#undef ischar
}
using namespace IO;
#define N 110
#define M 20
const ll inf=0x3f3f3f3f3f3f3f3f;
struct node{
int x,k,p;
}a[N];
int n,m,b,x[N],k[N],cur;
ll f[2][1<<M],ans;
//f[i][j][st]
//前i个朋友j台显示器解决方案st最少要多少钱
//f[i][j][st]+max(0,k[i+1]-j)*d+x[i+1]->f[i+1][max(j,k[i+1])][st|a[i+1]]
//发现可以给显示器个数排序,这样保证递增
//f[i][st]+x[i+1]->f[i+1][st|a[i+1]]
#define cmin(a,b) (a=b<a?b:a)
signed main(){
read(n,m,b);
for(int i=1,c,v;i<=n;++i){
read(a[i].x,a[i].k,c);
while(c--)read(v),a[i].p|=1<<(v-1);
}
sort(a+1,a+n+1,[](node a,node b){return a.k<b.k;});
memset(f[cur],0x3f,sizeof f);
f[cur][0]=0;
f[cur][a[1].p]=a[1].x;
ans=f[cur][(1<<m)-1]+1ll*b*a[1].k;
for(int i=1;i<n;++i){
for(int st=0;st<1<<m;++st)f[cur^1][st]=inf;
for(int st=0;st<1<<m;++st)
if(f[cur][st]<inf)
cmin(f[cur^1][st],f[cur][st]),
cmin(f[cur^1][st|a[i+1].p],f[cur][st]+a[i+1].x);
cmin(ans,f[cur^1][(1<<m)-1]+1ll*a[i+1].k*b);
cur^=1;
}
write(ans<inf?ans:-1);
oclear();
return 0;
}
AC code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 1010
#define M 21
const ll inf=2e18;
struct node{
int x,k,p;
}a[N];
int n,m,b,x[N],k[N],cur;
ll f[2][1<<M],ans=inf;
#define cmin(a,b) (a=b<a?b:a)
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m>>b;
for(int i=1,c,v;i<=n;++i){
cin>>a[i].x>>a[i].k>>c;
while(c--)cin>>v,a[i].p|=1<<(v-1);
}
sort(a+1,a+n+1,[&](node a,node b){return a.k<b.k;});
for(int i=1;i<1<<m;i++)f[0][i]=inf;
for(int i=0;i<n;++i){
for(int st=0;st<1<<m;++st)f[i+1&1][st]=inf;
for(int st=0;st<1<<m;++st)
if(f[i&1][st]<inf)
cmin(f[i+1&1][st],f[i&1][st]),
cmin(f[i+1&1][st|a[i+1].p],f[i&1][st]+a[i+1].x);
cmin(ans,f[i+1&1][(1<<m)-1]+1ll*a[i+1].k*b);
}
cout<<(ans<inf?ans:-1);
return 0;
}