标签:printf span for foo scanf bfs ble lan ==
对牛进行拆点
源点——食物——牛——饮料——汇点连边
边的流量都为1,以保证每种食物、牛以及饮料对答案的贡献为1。
#include<complex> #include<cstdio> using namespace std; const int INF=0x3f3f3f3f; const int N=407,M=1e4+7; struct node{ int v,f,nxt; }e[M*5]; int n,f,d,Enum=1,s,t; int front[N],cur[N],deep[N]; int q[N]; int qread() { int x=0; char ch=getchar(); while(ch<‘0‘ || ch>‘9‘)ch=getchar(); while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x; } void Insert(int u,int v,int w) { e[++Enum].v=v;e[Enum].f=w;e[Enum].nxt=front[u];front[u]=Enum; e[++Enum].v=u;e[Enum].nxt=front[v];front[v]=Enum; } bool bfs() { for(int i=0;i<=t;i++) { deep[i]=0; cur[i]=front[i]; } int head=1,tail=0,u,v; deep[s]=1;q[++tail]=s; while(head<=tail) { u=q[head++]; for(int i=front[u];i;i=e[i].nxt) { v=e[i].v; if(!deep[v] && e[i].f) { deep[v]=deep[u]+1; if(v==t)return 1; q[++tail]=v; } } } return 0; } int dfs(int x,int cur_flow) { if(x==t)return cur_flow; int rest=cur_flow,v; for(int &i=cur[x];i;i=e[i].nxt) { v=e[i].v; if(deep[v]==deep[x]+1 && e[i].f && rest) { int new_flow=dfs(v,min(e[i].f,rest)); e[i].f-=new_flow; e[i^1].f+=new_flow; rest-=new_flow; if(!rest)return cur_flow; } } deep[x]=0; return cur_flow-rest; } void Dinic() { int res=0; while(bfs()) res+=dfs(s,INF); printf("%d\n",res); } int main() { scanf("%d%d%d",&n,&f,&d); s=0;t=n+n+f+d+1; for(int i=1;i<=f;i++) Insert(s,i,1); for(int i=1;i<=d;i++) Insert(f+n+n+i,t,1); int food,drink; for(int i=1;i<=n;i++) { Insert(f+i,f+n+i,1); food=qread();drink=qread(); for(int j=1;j<=food;j++) Insert(qread(),f+i,1); for(int j=1;j<=drink;j++) Insert(f+n+i,f+n+n+qread(),1); } Dinic(); return 0; }
标签:printf span for foo scanf bfs ble lan ==
原文地址:https://www.cnblogs.com/LeTri/p/9029409.html