【USACO 2.1】Healthy Holsteins
/* TASK: holstein LANG: C++ URL: http://train.usaco.org/usacoprob2?a=SgkbOSkonr2&S=holstein SOLVE: con[i][j]为食物i含有维生素j的量,ned[i]为需要的维生素i的量 bfs,用二进制保存状态 */ #include<cstdio> #define N 30 int v,g,ned[N],con[N][N]; int now[N]; int l,r,q[40000]; bool vis[40000]; bool ck(int s){ //check whether state s is satisfied for(int i=1;i<=v;i++){ int tol=0; for(int j=0;j<g;j++) if((1<<j)&s) tol+=con[j][i]; if(tol<ned[i])return 0; } return 1; } void bfs(){ while(l<=r){ int k=q[l++]; if(ck(k)){ int num=0; for(int i=0;i<g;i++) if((1<<i)&k) num++; printf("%d",num); for(int i=0;i<g;i++) if((1<<i)&k) printf(" %d",i+1); puts(""); return; } for(int i=0;i<g;i++) if(!vis[k|(1<<i)]){ q[++r]=k|(1<<i); vis[k|(1<<i)]=1; } } } int main(){ freopen("holstein.in","r",stdin); freopen("holstein.out","w",stdout); scanf("%d",&v); for(int i=1;i<=v;i++) scanf("%d",&ned[i]); scanf("%d",&g); for(int i=0;i<g;i++) for(int j=1;j<=v;j++) scanf("%d",&con[i][j]); bfs(); }
┆凉┆暖┆降┆等┆幸┆我┆我┆里┆将┆ ┆可┆有┆谦┆戮┆那┆ ┆大┆始┆ ┆然┆
┆薄┆一┆临┆你┆的┆还┆没┆ ┆来┆ ┆是┆来┆逊┆没┆些┆ ┆雁┆终┆ ┆而┆
┆ ┆暖┆ ┆如┆地┆站┆有┆ ┆也┆ ┆我┆ ┆的┆有┆精┆ ┆也┆没┆ ┆你┆
┆ ┆这┆ ┆试┆方┆在┆逃┆ ┆会┆ ┆在┆ ┆清┆来┆准┆ ┆没┆有┆ ┆没┆
┆ ┆生┆ ┆探┆ ┆最┆避┆ ┆在┆ ┆这┆ ┆晨┆ ┆的┆ ┆有┆来┆ ┆有┆
┆ ┆之┆ ┆般┆ ┆不┆ ┆ ┆这┆ ┆里┆ ┆没┆ ┆杀┆ ┆来┆ ┆ ┆来┆