poj 3281 Dining (Dinic)

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22572   Accepted: 10015

Description

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

Line 1: Three space-separated integers: N, F, and D
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 Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.

Output

Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes

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

 

C/C++:

 1 #include <map>
 2 #include <queue>
 3 #include <cmath>
 4 #include <vector>
 5 #include <string>
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <climits>
 9 #include <iostream>
10 #include <algorithm>
11 #define INF 0x3f3f3f3f
12 using namespace std;
13 const int my_max = 410;
14 
15 int my_map[my_max][my_max], my_cow, my_food, my_drink, my_source,
16     my_sink, n, my_dis[my_max];
17 
18 int my_dfs(int step, int ans)
19 {
20     if (step == my_sink) return ans;
21 
22     int temp = ans;
23     for (int i = 0; i < n && temp; ++ i)
24     {
25         if (my_dis[i] + 1 == my_dis[step] && my_map[step][i])
26         {
27             int t = my_dfs(i, min(ans, my_map[step][i]));
28             ans -= t;
29             my_map[step][i] -= t;
30             my_map[i][step] += t;
31         }
32     }
33     return temp - ans;
34 }
35 
36 bool my_bfs()
37 {
38     memset(my_dis, -1, sizeof(my_dis));
39     my_dis[my_sink] = 0;
40     queue <int> Q;
41     Q.push(my_sink);
42     while (!Q.empty())
43     {
44         int my_temp = Q.front();
45         for (int i = 0; i < n; ++ i)
46         {
47             if (my_dis[i] == -1 && my_map[i][my_temp])
48             {
49                 my_dis[i] = my_dis[my_temp] + 1;
50                 Q.push(i);
51             }
52         }
53         if (my_temp == my_source) return true;
54         Q.pop();
55     }
56     return false;
57 }
58 
59 int my_dinic()
60 {
61     int my_max_flow = 0;
62     while (my_bfs())
63         my_max_flow += my_dfs(my_source, INF);
64 
65     return my_max_flow;
66 }
67 
68 int main()
69 {
70     while (~scanf("%d%d%d", &my_cow, &my_food, &my_drink))
71     {
72         my_source = 0, my_sink = my_food + 2*my_cow + my_drink;
73         for (int i = 1; i <= my_food; ++ i) my_map[my_source][i] = 1;
74         for (int i = 1; i <= my_cow; ++ i) my_map[my_food + i][my_food + my_cow + i] = 1;
75         for (int i = 1; i <= my_drink; ++ i) my_map[my_food + 2*my_cow + i][my_sink] = 1;
76 
77         for (int i = 1; i <= my_cow; ++ i)
78         {
79             int my_food_num, my_drink_num;
80             scanf("%d%d", &my_food_num, &my_drink_num);
81             while (my_food_num --)
82             {
83                 int my_temp;
84                 scanf("%d", &my_temp);
85                 my_map[my_temp][my_food + i] = 1;
86             }
87             while (my_drink_num --)
88             {
89                 int my_temp;
90                 scanf("%d", &my_temp);
91                 my_map[my_food + my_cow + i][my_food + 2 * my_cow + my_temp] = 1;
92             }
93         }
94 
95         n = my_sink + 1;
96         printf("%d\n", my_dinic());
97     }
98     return 0;
99 }

help

posted @ 2018-08-15 20:12  GetcharZp  阅读(137)  评论(0编辑  收藏  举报