bzoj1433: [ZJOI2009]假期的宿舍
1433: [ZJOI2009]假期的宿舍
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2315 Solved: 981
[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
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。
难度在题意?
1 #include<bits/stdc++.h> 2 #define inf 2147483647 3 #define N 1280 4 #define rep(i,l,r) for(int i=l;i<=r;i++) 5 using namespace std; 6 int T,head[N],cnt,ans,ccz,n,tot=1,mp[N][N],f[N],g[N],dis[N]; 7 struct node{ 8 int to,next,w; 9 }e[N*N<<1]; 10 inline bool bfs(){ 11 for(int i=0;i<=T;i++) dis[i]=-1; queue<int>q; q.push(0); dis[0]=0; 12 while(!q.empty()) { 13 int x=q.front(); q.pop(); 14 for(int k=head[x];k;k=e[k].next) 15 if(dis[e[k].to]<0 && e[k].w>0) { 16 dis[e[k].to]=dis[x]+1; q.push(e[k].to); 17 } 18 } 19 if(dis[T]>0) return 1;else return 0; 20 } 21 int find(int x,int low){ 22 if(x==T) return low; 23 int delta=low,now; 24 for(int k=head[x];k;k=e[k].next) 25 if(e[k].w>0 && dis[e[k].to]==dis[x]+1){ 26 now=find(e[k].to,min(e[k].w,delta)); 27 e[k].w-=now; e[k^1].w+=now; delta-=now; 28 if(!delta) return low; 29 } 30 dis[x]=-1; 31 return low-delta; 32 } 33 inline void ins(int u,int v,int w) { 34 e[++tot].to=v; e[tot].next=head[u]; head[u]=tot; e[tot].w=w; 35 } 36 inline void insert(int u,int v,int w) { 37 ins(u,v,w); ins(v,u,0); 38 } 39 int main () { 40 scanf("%d",&ccz); 41 while(ccz--) { 42 scanf("%d",&n); memset(head,0,sizeof(head)); tot=1; cnt=ans=0; T=n+n+1; 43 rep(i,1,n) scanf("%d",&f[i]); 44 rep(i,1,n) scanf("%d",&g[i]); 45 rep(i,1,n) rep(j,1,n) scanf("%d",&mp[i][j]),mp[i][j]+=(i==j); 46 rep(i,1,n) if(!f[i] || (f[i]&&!g[i])) rep(j,1,n) if(mp[i][j]&&f[j]) insert(i,j+n,1); 47 rep(i,1,n) if(!f[i] || (f[i]&&!g[i])) ++cnt,insert(0,i,1); 48 rep(i,1,n) insert(i+n,T,1); 49 while(bfs()) ans+=find(0,inf); 50 if(ans>=cnt) puts("^_^");else puts("T_T"); 51 } 52 }