POJ 3281 Dining(网络流-拆点)
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.
Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.
Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.
Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).
Input
Lines 2.. N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fiintegers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.
Output
Sample Input
4 3 3 2 2 1 2 3 1 2 2 2 3 1 2 2 2 1 3 1 2 2 1 1 3 3
Sample Output
3
Hint
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<cstdlib> 6 #include<algorithm> 7 #include<queue> 8 #include<deque> 9 #include<stack> 10 #include<set> 11 #include<vector> 12 #include<map> 13 using namespace std; 14 typedef long long LL; 15 typedef pair<int,int> PII; 16 #define PI acos(-1) 17 #define EPS 1e-8 18 const int INF=0x3f3f3f3f; 19 const int maxn = 520; 20 int n,m,k,s,t,x,y,z; 21 struct Edge { 22 int from, to, cap, flow; 23 }; 24 vector<Edge> edges; 25 vector<int> G[maxn]; 26 bool vis[maxn]; 27 int d[maxn], cur[maxn]; 28 29 void Init() 30 { 31 memset(d,0,sizeof d); 32 for(int i=0;i<=n;i++) G[i].clear(); 33 } 34 35 void addedge(int from, int to, int cap) 36 { 37 edges.push_back((Edge){from, to, cap, 0}); 38 edges.push_back((Edge){to, from, 0, 0}); 39 int m = edges.size(); 40 G[from].push_back(m-2); G[to].push_back(m-1); 41 } 42 43 bool bfs() 44 { 45 memset(vis,0,sizeof vis); 46 queue<int> q; 47 q.push(s); 48 d[s] = 0; vis[s] = 1; 49 while (!q.empty()) 50 { 51 int x = q.front(); q.pop(); 52 for(int i = 0; i < G[x].size(); ++i) 53 { 54 Edge &e = edges[G[x][i]]; 55 if (!vis[e.to] && e.cap > e.flow) 56 { 57 vis[e.to] = 1; 58 d[e.to] = d[x] + 1; 59 q.push(e.to); 60 } 61 } 62 } 63 return vis[t]; 64 } 65 66 int dfs(int x,int a) 67 { 68 if(x == t || a == 0) return a; 69 int flow = 0, f; 70 for(int &i = cur[x]; i < G[x].size(); ++i) 71 { 72 Edge &e = edges[G[x][i]]; 73 if (d[e.to] == d[x] + 1 && (f=dfs(e.to, min(a, e.cap-e.flow))) > 0) 74 { 75 e.flow += f; 76 edges[G[x][i]^1].flow -= f; 77 flow += f; a -= f; 78 if (a == 0) break; 79 } 80 } 81 return flow; 82 } 83 84 int Maxflow(int s, int t) 85 { 86 int flow = 0; 87 while (bfs()) 88 { 89 memset(cur,0,sizeof cur); 90 flow += dfs(s, INF); 91 } 92 return flow; 93 } 94 95 int main() 96 { 97 while(~scanf("%d%d%d",&n,&m,&k)) 98 { 99 Init(); 100 s=0,t=2*n+m+k+1; 101 for(int i=1;i<=m;i++) addedge(s,i,1); 102 for(int i=1;i<=n;i++) addedge(m+i,n+m+i,1); 103 for(int i=2*n+m+1;i<=2*n+m+k;i++) addedge(i,t,1); 104 for(int i=1;i<=n;i++) 105 { 106 scanf("%d%d",&x,&y); 107 for(int j=1;j<=x;j++) scanf("%d",&z),addedge(z,m+i,1); 108 for(int j=1;j<=y;j++) scanf("%d",&z),addedge(m+n+i,z+m+2*n,1); 109 } 110 printf("%d\n",Maxflow(s,t)); 111 } 112 return 0; 113 }