BZOJ1433: [ZJOI2009]假期的宿舍

1433: [ZJOI2009]假期的宿舍

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 3134  Solved: 1324
[Submit][Status][Discuss]

Description

Input

Output

Sample Input

1
3
1 1 0
0 1 0
0 1 1
1 0 0
1 0 0

Sample Output

ˆ ˆ

HINT

对于30% 的数据满足1 ≤ n ≤ 12。
对于100% 的数据满足1 ≤ n ≤ 50,1 ≤ T ≤ 20。

#include<bits/stdc++.h>
#define RG register
#define il inline
#define db double
#define LL long long
#define N 100000
#define S 0
#define Inf (1<<30)
using namespace std;
struct ed{int nxt,to,c;}e[N];
int head[N],tot,dep[N];
void link(int u,int v,int c){e[tot].nxt=head[u];e[tot].to=v;e[tot].c=c;head[u]=tot++;}
void LINK(int u,int v,int c){link(u,v,c),link(v,u,0);}
int n,BZ[N],cnt,out[N];
void clear(){
  tot=0;memset(head,-1,sizeof(head));
  cnt=0;
}
bool BFS(int s,int t){
  memset(dep,0,sizeof(dep));
  dep[s]=1;queue<int>que;while(!que.empty())que.pop();que.push(s);
  while(!que.empty()){
    int u=que.front();
    que.pop();
    for(int i=head[u];i!=-1;i=e[i].nxt)
      if(!dep[e[i].to]&&e[i].c){
    int v=e[i].to;
    dep[v]=dep[u]+1;
    if(v==t)return true;
    que.push(v);
      }
  }return false;
}
int dinic(int s,int t,int T){
  if(s==t)return T;int tag(0);
  for(int i=head[s];i!=-1;i=e[i].nxt)if(dep[e[i].to]==dep[s]+1&&e[i].c){
      int v=e[i].to;
      int d=dinic(v,t,min(T-tag,e[i].c));
      e[i].c-=d,e[i^1].c+=d,tag+=d;
      if(tag==T)break;
    }if(!tag)dep[s]=0;
  return tag;
}
int maxflow(int s,int t){
  int flow(0);
  while(BFS(s,t))flow+=dinic(s,t,Inf);
  return flow;
}
int main(){
  int T;scanf("%d",&T);
  while(T--){
    clear();
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
      scanf("%d",&BZ[i]);
      LINK(0,i,1);
    }
    for(int i=1;i<=n;++i){
      scanf("%d",&out[i]);
      if(BZ[i])LINK(i+n,2*n+1,1);
      if(BZ[i]&&out[i])cnt++;
    }
    for(int i=1;i<=n;++i){
      if(BZ[i]&&out[i])
      for(int j=1;j<=n;++j){
    int kk;scanf("%d",&kk);
    }
      else {
    LINK(i,i+n,1);
    for(int j=1;j<=n;++j){
      int kk;scanf("%d",&kk);
      if(kk)LINK(i,j+n,1);
    }
      }
    }
    if(maxflow(S,2*n+1)+cnt==n)cout<<"^_^\n";
    else cout<<"T_T\n";
  }
  return 0;
}

 

思路{首先用增广路的方法发现是有问题的,然后发现,人床对应,就是一个裸的二分图}

 

posted @ 2017-09-04 23:55  QYP_2002  阅读(122)  评论(0编辑  收藏  举报