P1402 酒店之王
P1402 酒店之王
把人放中间拆点即可
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<cmath> #include<ctime> #include<set> #include<map> #include<stack> #include<cstring> #define inf 2147483647 #define ls rt<<1 #define rs rt<<1|1 #define lson ls,nl,mid,l,r #define rson rs,mid+1,nr,l,r #define N 10010 #define For(i,a,b) for(int i=a;i<=b;i++) #define p(a) putchar(a) #define g() getchar() using namespace std; int n,m,x,y,v,k,maxflow,tot; int head[N],deep[N],cur[N]; struct node{ int n; int v; int next; }e[N]; queue<int>q; void in(int &x){ int y=1; char c=g();x=0; while(c<'0'||c>'9'){ if(c=='-')y=-1; c=g(); } while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=g(); } x*=y; } void o(int x){ if(x<0){ p('-'); x=-x; } if(x>9)o(x/10); p(x%10+'0'); } void push(int x,int y,int v){ e[tot].n=y; e[tot].v=v; e[tot].next=head[x]; head[x]=tot++; } bool bfs(int s,int t){ memset(deep,-1,sizeof(deep)); For(i,0,2*n+m+k+10) cur[i]=head[i]; deep[s]=0; q.push(s); while(!q.empty()){ int now=q.front(); q.pop(); for(int i=head[now];i!=-1;i=e[i].next) if(deep[e[i].n]==-1 && e[i].v>0){ deep[e[i].n]=deep[now]+1; q.push(e[i].n); } } return deep[t]!=-1; } int dfs(int now,int t,int lim){ if(now==t||lim==0) return lim; int flow=0,f; for(int i=cur[now];i!=-1;i=e[i].next){ cur[now]=i; if(deep[e[i].n]==deep[now]+1 && (f=dfs(e[i].n,t, min(lim,e[i].v) ))){ flow+=f; lim-=f; e[i].v-=f; e[i^1].v+=f; if(!lim) break; } } return flow; } void dinic(int s,int t){ while(bfs(s,t)){ maxflow+=dfs(s,t,inf); } } int main(){ in(n);in(m);in(k); memset(head,-1,sizeof(head)); For(i,1,n) For(j,1,m){ in(v); if(v){ push(2*n+j,i,1); push(i,2*n+j,0); } } For(i,1,n){ push(i,n+i,1); push(n+i,i,0); } For(j,1,m){ push(0,2*n+j,1); push(2*n+j,0,0); } For(i,1,n) For(j,1,k){ in(v); if(v){ push(n+i,2*n+m+j,1); push(2*n+m+j,n+i,0); } } For(j,1,k){ push(2*n+m+j,2*n+m+k+10,1); push(2*n+m+k+10,2*n+m+j,0); } dinic(0,2*n+m+k+10); o(maxflow); return 0; }