Food HDU - 4292
关键在于建图,食物放左边,饮料放右边,人员放中间。
需要注意的是,人员要拆点,保证一人只能吃一份。
以后都不用dinic了,都用ISAP
#include<iostream> #include<algorithm> using namespace std; const int max_n = 900; const int max_m = max_n*max_n; const int inf=0x3f3f3f3f; struct edge { int to,cap,next; }E[max_m<<2]; int n; int head[max_n],cnt=1,gap[max_n],last[max_n],d[max_n],que[max_n],ql,qr; void add(int u,int to,int cap) { E[++cnt].to=to;E[cnt].cap=cap; E[cnt].next=head[u];head[u]=cnt; E[++cnt].to=u;E[cnt].cap=0; E[cnt].next=head[to];head[to]=cnt; }int aug(int x,int s,int t,int mi) { if (x==t) return mi; int flow=0; for (int &i=last[x],v=E[i].to;i;i=E[i].next,v=E[i].to) if (d[x]==d[v]+1) { int tmp=aug(v,s,t,min(mi,E[i].cap)); flow+=tmp,mi-=tmp,E[i].cap-=tmp,E[i^1].cap+=tmp; if (!mi) return flow; } if (!(--gap[d[x]])) d[s]=n+1; ++gap[++d[x]],last[x]=head[x]; return flow; } int maxflow(int s,int t) { fill(gap,gap+n+10,0);fill(d,d+n+10,0); ++gap[d[t]=1]; for (int i=1;i<=n;++i) last[i]=head[i]; que[ql=qr=1]=t; while (ql<=qr) { int x=que[ql++]; for (int i=head[x],v=E[i].to;i;i=E[i].next,v=E[i].to) if (!d[v]) ++gap[d[v]=d[x]+1],que[++qr]=v; } int ret=aug(s,s,t,inf); while (d[s]<=n) ret+=aug(s,s,t,inf); return ret; } int N,F,D; int main(){ while(~scanf("%d %d %d",&N,&F,&D)){ int s = N+F+D+N+1;int t = s+1; n = t; fill(head,head+n+5,0);cnt=1; for (int i=1;i<=F;++i){ int cap;scanf("%d",&cap); add(s,i,cap); } for (int i=1;i<=D;++i){ int cap;scanf("%d",&cap); add(i+F+N,t,cap); } for (int i=1;i<=N;++i)add(i+F,i+F+D+N,1); for (int i=1;i<=N;++i){ char c[400]; scanf("%s",c+1); for (int j=1;j<=F;++j) if (c[j]=='Y')add(j,i+F,1); } for (int i=1;i<=N;++i){ char c[400]; scanf("%s",c+1); for (int j=1;j<=D;++j) if (c[j]=='Y')add(i+F+D+N,j+N+F,1); } printf("%d\n",maxflow(s,t)); } }
kuangbin题单刷题详解(网络流) 文章被收录于专栏
题单:https://vjudge.net/article/371