Description
Farmer John made good food for the cows, but the cows were picky. each ox only likes to eat some food and drinks, but does not eat anything else. although he may not be able to feed all the cows, he still wants as many cows as possible to eat their favorite food and drinks. farmer John made F (1 <= F <= 100) kinds of food and prepared D (1 <= d <= 100) kinds of drinks. his n (1 <= n <= 100) cows determine whether they are willing to eat certain foods and drink certain drinks. farmer John wants to give each Ox a kind of food and a drink so that as many cows as possible can get their favorite food and drinks. each piece of food and beverage can only be used by one ox. for example, if food 2 is eaten by a cow, no other cow can eat food. input
* First row: Number Three: N, f, and D
* 2nd .. n + 1 rows: each row starts with f_ I and d_ I, which are the number of foods that can be eaten by the I-th ox and the number of drinks that can be consumed. the f_ I integer below is the food number that can be eaten by the I-head ox, And the d_ I integer below is the beverage number that the I-head ox can drink. output
* Row 1: an integer that can be used to feed a maximum of cattle.
Question:
Maximum stream.
Split the ox into two vertices: OX 1 and OX 2.
S connects to each type of beverage. Each type of beverage connects to one edge of the favorite ox, one edge to the ox, and two edge to the favorite food, each type of food is directed to a t edge.
The traffic is 1.
Find the maximum stream of S-T.
Code:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>//by zrt//problem:using namespace std;typedef long long LL;const int inf(0x3f3f3f3f);const double eps(1e-9);int n,f;int H[405],tot,S,T,P[160005],flow[160005],X[160005];inline void add(int x,int y,int z){ P[++tot]=y;X[tot]=H[x];H[x]=tot;flow[tot]=z;}int d[405];queue<int> q;bool bfs(){ memset(d,0,sizeof d); d[S]=1; while(!q.empty()) q.pop(); q.push(S); int x; while(!q.empty()){ x=q.front();q.pop(); if(x==T) return 1; for(int i=H[x];i;i=X[i]){ if(flow[i]>0&&!d[P[i]]){ d[P[i]]=d[x]+1; q.push(P[i]); } } } return 0;}int dfs(int x,int a){ if(x==T||a==0) return a; int f=a,tmp; for(int i=H[x];i;i=X[i]){ if(d[P[i]]==d[x]+1&&flow[i]>0){ tmp=dfs(P[i],min(a,flow[i])); a-=tmp; flow[i]-=tmp; flow[i^1]+=tmp; if(!a) break; } } if(f==a) d[x]=-1; return f-a;}int Dinic(){ int f=0; while(bfs()){ f+=dfs(S,inf); } return f;}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif S=403,T=404; tot=1; int d; scanf("%d%d%d",&n,&f,&d); //n 1..n n+1..n+n //f 2*n+1..2*n+f //d 2*n+f+1..2*n+f+d for(int i=1;i<=n;i++){ add(i,i+n,1); add(i+n,i,0); } for(int i=1;i<=f;i++){ add(S,i+2*n,1); add(i+2*n,S,0); } for(int i=1;i<=d;i++){ add(2*n+f+i,T,1); add(T,2*n+f+i,0); } for(int i=1;i<=n;i++){ int fi,di; scanf("%d%d",&fi,&di); int x; for(int j=1;j<=fi;j++){ scanf("%d",&x); add(x+2*n,i,1); add(i,x+2*n,0); } for(int j=1;j<=di;j++){ scanf("%d",&x); add(i+n,x+2*n+f,1); add(x+2*n+f,i+n,0); } } printf("%d\n",Dinic()); return 0;}
Bzoj 1711: [usaco open] dingin for dinner