bzoj 4057(状压dp)

传送门
题解:设f[i](bool)表示能否出现状态i,二进制i的1表示破产,0表示幸存

#include
using namespace std;
int n,a[22][22],ans[22],cnt;
bool f[1050000];
int main() {
//  freopen("bzoj 4057.in","r",stdin);
    int kase;
    scanf("%d",&kase);
    while (kase--) {
        scanf("%d",&n);
        for (int i=0;ifor (int j=0;jscanf("%d",&a[i][j]);
        memset(f,false,sizeof(f));
        f[0]=1,cnt=0;
        for (int i=0;i<(1<if (f[i])
            for (int j=0;jif (!(i&(1<1<int temp=0;
                    for (int k=0;kif (!(i&(1<if (temp<0) f[i|(1<true;
                }
        int tot=(1<1,d=0;
        for (int i=0;iif (f[tot^(1<true;
        if (!d) {puts("0");continue;}
        sort(ans+1,ans+cnt+1);
        for  (int i=1;i<=cnt;++i) printf("%d%c",ans[i]+1,(i^cnt)?' ':'\n');
    }
    return 0;
}

你可能感兴趣的:(dp)