POJ-3281 : 最大流(Dinic算法 + ISAP算法)

时间:2020-11-27 20:10:56
Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6958   Accepted: 3178

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

此题建图是难点,方法为拆点 :源点->食物->牛->牛->水->汇点。

ISAP: 0ms
POJ-3281 : 最大流(Dinic算法 + ISAP算法)POJ-3281 : 最大流(Dinic算法 + ISAP算法)View Code
  1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 #define INF 0x7fffffff
6 #define MAX 405
7
8 using namespace std;
9
10 struct node
11 {
12 int to,val,next;
13 };
14
15 node point[MAX*MAX];
16 int head[MAX];
17 int dis[MAX];
18 int gap[MAX];
19 int N,F,D,index,source,sink,all_pt;
20
21 void addNode(int from,int to,int val)
22 {
23 point[index].to=to;
24 point[index].val=val;
25 point[index].next=head[from];
26 head[from]=index++;
27 point[index].to=from;
28 point[index].val=0;
29 point[index].next=head[to];
30 head[to]=index++;
31 return ;
32 }
33
34 void init()
35 {
36 scanf("%d%d%d",&N,&F,&D);
37 source = 0;
38 sink = 2*N+F+D+1;
39 all_pt = sink + 1;
40 index = 0;
41 memset(head,-1,sizeof(head));
42 for(int i=1;i<=F;i++)
43 addNode(0,i,1);
44 for(int i=1;i<=D;i++)
45 addNode(F+2*N+i,sink,1);
46 for(int i=1;i<=N;i++)
47 {
48 int tf,td;
49 scanf("%d%d",&tf,&td);
50 for(int j=1;j<=tf;j++)
51 {
52 int food;
53 scanf("%d",&food);
54 addNode(food,F+i,1);
55 }
56 addNode(F+i,F+N+i,1);
57 for(int j=1;j<=td;j++)
58 {
59 int drink;
60 scanf("%d",&drink);
61 addNode(i+N+F,F+2*N+drink,1);
62 }
63 }
64 return ;
65 }
66
67 int dfs(int cur,int cur_val)
68 {
69 if(cur == sink)
70 return cur_val;
71 int min_dis = all_pt-1,temp_val = cur_val;
72 for(int i=head[cur];i!=-1;i=point[i].next)
73 {
74 int cur_to = point[i].to,to_val = point[i].val;
75 if(to_val>0)
76 {
77 if(dis[cur_to]+1 == dis[cur])
78 {
79 int val = min(temp_val,point[i].val);
80 val = dfs(cur_to,val);
81 temp_val -= val;
82 point[i].val -= val;
83 point[i^1].val += val;
84 if(dis[source]>=all_pt)
85 return cur_val - temp_val;
86 if(temp_val == 0)
87 break;
88 }
89 if(dis[cur_to]<min_dis)
90 min_dis = dis[cur_to];
91 }
92 }
93 if(temp_val == cur_val)
94 {
95 --gap[dis[cur]];
96 if(gap[dis[cur]]==0)
97 dis[source] = all_pt;
98 dis[cur] = min_dis + 1;
99 ++ gap[dis[cur]];
100 }
101 return cur_val - temp_val;
102 }
103
104 int sap()
105 {
106 memset(gap,0,sizeof(gap));
107 memset(dis,0,sizeof(dis));
108 int ret = 0;
109 gap[0] = all_pt;
110 while(dis[source]<all_pt)
111 ret += dfs(source,INF);
112 return ret;
113 }
114
115 int main()
116 {
117 init();
118 int ans = sap();
119 printf("%d\n",ans);
120 return 0;
121 }


Dinic:  79ms

POJ-3281 : 最大流(Dinic算法 + ISAP算法)POJ-3281 : 最大流(Dinic算法 + ISAP算法)View Code
 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <queue>
5 #include <algorithm>
6 #define INF 0x7fffffff
7
8 using namespace std;
9
10 int eat[402][402];
11 int dis[402];
12 int food,drink,cow,point;
13
14 bool bfs()
15 {
16 queue<int> Q;
17 memset(dis,-1,sizeof(dis));
18 Q.push(0);
19 dis[0]=0;
20 while(!Q.empty())
21 {
22 int cur=Q.front();
23 Q.pop();
24 for(int i=0;i<=point;i++)
25 {
26 if(dis[i]==-1 && eat[cur][i])
27 {
28 dis[i]=dis[cur]+1;
29 Q.push(i);
30 }
31 }
32 }
33 return dis[point]!=-1?true:false;
34 }
35
36 int dfs(int cur,int cur_val)
37 {
38 if(cur == point)
39 return cur_val;
40 int temp_val=cur_val;
41 for(int i=0;i<=point;i++)
42 {
43 if(dis[i]==dis[cur]+1 && eat[cur][i])
44 {
45 int val=dfs(i,min(temp_val,eat[cur][i]));
46 temp_val -= val;
47 eat[cur][i] -= val;
48 eat[i][cur] += val;
49 }
50 }
51 return cur_val - temp_val;
52 }
53
54 int main()
55 {
56 while(~scanf("%d%d%d",&cow,&food,&drink))
57 {
58 point = 2*cow+food+drink+1;
59 memset(eat,0,sizeof(eat));
60 for(int i=1;i<=food;i++)
61 eat[0][i]=1;
62 for(int i=1;i<=drink;i++)
63 eat[food+2*cow+i][food+2*cow+drink+1]=1;
64 for(int i=1;i<=cow;i++)
65 {
66 int tf,td;
67 scanf("%d%d",&tf,&td);
68 for(int j=1;j<=tf;j++)
69 {
70 int f;
71 scanf("%d",&f);
72 eat[f][i+food]=1;
73 }
74 eat[i+food][i+food+cow]=1;
75 for(int j=1;j<=td;j++)
76 {
77 int d;
78 scanf("%d",&d);
79 eat[i+food+cow][d+food+2*cow]=1;
80 }
81 }
82 int ans=0;
83 while(bfs())
84 {
85 int temp;
86 while(temp=dfs(0,INF))
87 ans += temp;
88 }
89 printf("%d\n",ans);
90 }
91 return 0;
92 }