BZOJ1433: [ZJOI2009]假期的宿舍

1433: [ZJOI2009]假期的宿舍

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1141  Solved: 491
[Submit][Status]

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。

题解:
左边是人,右边是床,dinic即可
注意逻辑关系即可
代码:
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #define inf 1000000000
12 #define maxn 100+100
13 #define maxm 2500+100
14 #define eps 1e-10
15 #define ll long long
16 using namespace std;
17 inline int read()
18 {
19     int x=0,f=1;char ch=getchar();
20     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
21     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
22     return x*f;
23 }
24 struct edge{int go,next,v;}e[2*maxm];
25 int n,m,s,t,tot=1,maxflow,head[maxn],q[maxn],h[maxn],cur[maxn];
26 bool a[maxn],b[maxn];
27 void ins(int x,int y,int z){e[++tot].go=y;e[tot].v=z;e[tot].next=head[x];head[x]=tot;}
28 void insert(int x,int y,int z){ins(x,y,z);ins(y,x,0);}
29 bool bfs()
30 {
31     for(int i=s;i<=t;i++)h[i]=-1;
32     int l=0,r=1;q[1]=s;h[s]=0;
33     while(l<r) 
34     {
35         int x=q[++l];
36         for(int i=head[x];i;i=e[i].next)
37          if(e[i].v&&h[e[i].go]==-1)
38          {
39              h[e[i].go]=h[x]+1;q[++r]=e[i].go;
40          }
41     }
42     return h[t]!=-1;
43 }
44 int dfs(int x,int f)
45 {
46     if(x==t) return f;
47     int tmp,used=0,i;
48     for(int i=head[x];i;i=e[i].next)
49      if(e[i].v&&h[e[i].go]==h[x]+1) 
50     {
51         tmp=dfs(e[i].go,min(e[i].v,f-used));
52         e[i].v-=tmp;if(e[i].v)cur[x]=i; 
53         e[i^1].v+=tmp;used+=tmp;
54         if(used==f)return f;         
55     }
56     if(!used) h[x]=-1;
57     return used; 
58 }
59 void dinic()
60 {
61     maxflow=0;
62     while(bfs())
63     {
64         for (int i=s;i<=t;i++)cur[i]=head[i];maxflow+=dfs(s,inf); 
65     } 
66 }
67 int main()
68 {
69     freopen("input.txt","r",stdin);
70     freopen("output.txt","w",stdout);
71     int cs=read();
72     while(cs--)
73     {    
74      memset(a,0,sizeof(a));
75      memset(b,0,sizeof(b));
76      memset(head,0,sizeof(head));tot=1;
77      n=read();int x;
78      for(int i=1;i<=n;i++)a[i]=read();
79      for(int i=1;i<=n;i++){x=read();if(x&&a[i])b[i]=1;}
80      //for(int i=1;i<=n;i++)cout<<i<<' '<<a[i]<<' '<<b[i]<<endl;
81      s=0;t=2*n+1;
82      for(int i=1;i<=n;i++)
83      {
84          if(!b[i]&&a[i])insert(i,i+n,1);
85         for(int j=1;j<=n;j++){x=read();if(!b[i]&&x&&a[j])insert(i,j+n,1);}
86      }
87      int ans=0;
88      for(int i=1;i<=n;i++)if(!b[i])insert(s,i,1),ans++;
89      for(int i=1;i<=n;i++)if(a[i])insert(i+n,t,1);
90      dinic();
91      //cout<<ans<<' '<<maxflow<<endl;
92      if(maxflow==ans)printf("%c%c%c\n",94,95,94);else printf("%c%c%c\n",84,95,84);
93     }
94     return 0;
95 }
View Code

 

posted @ 2014-08-23 14:00  ZYF-ZYF  Views(210)  Comments(0Edit  收藏  举报