POJ3281 Dining —— 最大流 + 拆点

题目链接:https://vjudge.net/problem/POJ-3281

 

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20017   Accepted: 8901

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: NF, 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

Hint

One way to satisfy three cows is: 
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.

Source

 

 

题意:

有N头牛, F个食物, D个饮料。每头牛只吃或者喝自己喜欢的食物或饮料,问:怎样分配,使得尽量多的牛能够获得一个食物或一个饮料?

 

题解:

题目要求就是要食物、饮料与牛进行匹配,但是又不能用匹配算法,因为有两种匹配。因而可以利用网络流:食物放左边,牛放中间,饮料放右边,然后最左边加个超级源点,最右边加个超级汇点。意思就是要:先将食物与牛进行匹配,然后再用匹配后的牛与饮料进行匹配。

1.超级源点与食物相连,且边的容量为1,表明每种食物只提供一份。

2.将每头牛拆成两点,左边与食物相连,边的容量为1,表明最多只能提供一份食物。右边与饮料相连,边的容量为1,表明最多只能提供一份饮料。然后内部相连,边的容量为1, 表明有“1头牛”。

3.每个饮料与超级汇点相连,且边的容量为1,表明每种饮料只提供一份。

4.建图完毕,求最大流即可。

 

 

代码如下:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <vector>
  6 #include <cmath>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 using namespace std;
 13 typedef long long LL;
 14 const int INF = 2e9;
 15 const LL LNF = 9e18;
 16 const int mod = 1e9+7;
 17 const int MAXN = 5e2+10;
 18 
 19 int maze[MAXN][MAXN];
 20 int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
 21 int flow[MAXN][MAXN];
 22 
 23 int sap(int start, int end, int nodenum)
 24 {
 25     memset(cur, 0, sizeof(cur));
 26     memset(dis, 0, sizeof(dis));
 27     memset(gap, 0, sizeof(gap));
 28     memset(flow, 0, sizeof(flow));
 29     int u = pre[start] = start, maxflow = 0, aug = INF;
 30     gap[0] = nodenum;
 31 
 32     while(dis[start]<nodenum)
 33     {
 34         loop:
 35         for(int v = cur[u]; v<nodenum; v++)
 36         if(maze[u][v]-flow[u][v]>0 && dis[u] == dis[v]+1)
 37         {
 38             aug = min(aug, maze[u][v]-flow[u][v]);
 39             pre[v] = u;
 40             u = cur[u] = v;
 41             if(v==end)
 42             {
 43                 maxflow += aug;
 44                 for(u = pre[u]; v!=start; v = u, u = pre[u])
 45                 {
 46                     flow[u][v] += aug;
 47                     flow[v][u] -= aug;
 48                 }
 49                 aug = INF;
 50             }
 51             goto loop;
 52         }
 53 
 54         int mindis = nodenum-1;
 55         for(int v = 0; v<nodenum; v++)
 56             if(maze[u][v]-flow[u][v]>0 && mindis>dis[v])
 57             {
 58                 cur[u] = v;
 59                 mindis = dis[v];
 60             }
 61         if((--gap[dis[u]])==0) break;
 62         gap[dis[u]=mindis+1]++;
 63         u = pre[u];
 64     }
 65     return maxflow;
 66 }
 67 
 68 int main()
 69 {
 70     int N, F, D;
 71     while(scanf("%d%d%d",&N,&F,&D)!=EOF)
 72     {
 73         memset(maze, 0, sizeof(maze));
 74         for(int i = 0; i<N; i++)
 75         {
 76             int f, d, v;
 77             scanf("%d%d", &f,&d);
 78             while(f--)
 79             {
 80                 scanf("%d", &v);
 81                 v--;
 82                 maze[v][F+D+i] = 1; //food --> cow
 83             }
 84             while(d--)
 85             {
 86                 scanf("%d", &v);
 87                 v--;
 88                 maze[F+D+N+i][F+v] = 1; //cow' --> drink
 89             }
 90             maze[F+D+i][F+D+N+i] = 1;   // cow --> cow'
 91         }
 92 
 93         int start = F+D+2*N, end = F+D+2*N+1;
 94         for(int i = 0; i<F; i++) maze[start][i] = 1;    //超级源点 --> food
 95         for(int i = F; i<F+D; i++) maze[i][end] = 1;    //drink --> 超级汇点
 96 
 97         int ans = sap(start, end, F+D+2*N+2);
 98         printf("%d\n", ans);
 99     }
100 }
View Code

 

没有cur优化的sap:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int mod = 1e9+7;
17 const int MAXN = 5e2+10;
18 
19 int maze[MAXN][MAXN];
20 int gap[MAXN], dis[MAXN], pre[MAXN];
21 int flow[MAXN][MAXN];
22 
23 int sap(int start, int end, int nodenum)
24 {
25     memset(dis, 0, sizeof(dis));
26     memset(gap, 0, sizeof(gap));
27     memset(flow, 0, sizeof(flow));
28     int u = pre[start] = start, maxflow = 0, aug = INF;
29     gap[0] = nodenum;
30 
31     while(dis[start]<nodenum)
32     {
33         loop:
34         for(int v = 0; v<nodenum; v++)
35         if(maze[u][v]-flow[u][v]>0 && dis[u] == dis[v]+1)
36         {
37             aug = min(aug, maze[u][v]-flow[u][v]);
38             pre[v] = u;
39             u = v;
40             if(v==end)
41             {
42                 maxflow += aug;
43                 for(u = pre[u]; v!=start; v = u, u = pre[u])
44                 {
45                     flow[u][v] += aug;
46                     flow[v][u] -= aug;
47                 }
48                 aug = INF;
49             }
50             goto loop;
51         }
52 
53         int mindis = nodenum-1;
54         for(int v = 0; v<nodenum; v++)
55             if(maze[u][v]-flow[u][v]>0)
56                 mindis = min(mindis, dis[v]);
57 
58         if((--gap[dis[u]])==0) break;
59         gap[dis[u]=mindis+1]++;
60         u = pre[u];
61     }
62     return maxflow;
63 }
64 
65 int main()
66 {
67     int N, F, D;
68     while(scanf("%d%d%d",&N,&F,&D)!=EOF)
69     {
70         memset(maze, 0, sizeof(maze));
71         for(int i = 0; i<N; i++)
72         {
73             int f, d, v;
74             scanf("%d%d", &f,&d);
75             while(f--)
76             {
77                 scanf("%d", &v);
78                 v--;
79                 maze[v][F+D+i] = 1; //food --> cow
80             }
81             while(d--)
82             {
83                 scanf("%d", &v);
84                 v--;
85                 maze[F+D+N+i][F+v] = 1; //cow' --> drink
86             }
87             maze[F+D+i][F+D+N+i] = 1;   // cow --> cow'
88         }
89 
90         int start = F+D+2*N, end = F+D+2*N+1;
91         for(int i = 0; i<F; i++) maze[start][i] = 1;    //超级源点 --> food
92         for(int i = F; i<F+D; i++) maze[i][end] = 1;    //drink --> 超级汇点
93 
94         int ans = sap(start, end, F+D+2*N+2);
95         printf("%d\n", ans);
96     }
97 }
View Code

 

没有cur和gap优化的sap:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int mod = 1e9+7;
17 const int MAXN = 5e2+10;
18 
19 int maze[MAXN][MAXN];
20 int dis[MAXN], pre[MAXN];
21 int flow[MAXN][MAXN];
22 
23 int sap(int start, int end, int nodenum)
24 {
25     memset(dis, 0, sizeof(dis));
26     memset(flow, 0, sizeof(flow));
27     int u = pre[start] = start, maxflow = 0, aug = INF;
28 
29     while(dis[start]<nodenum)
30     {
31         loop:
32         for(int v = 0; v<nodenum; v++)
33         if(maze[u][v]-flow[u][v]>0 && dis[u] == dis[v]+1)
34         {
35             aug = min(aug, maze[u][v]-flow[u][v]);
36             pre[v] = u;
37             u = v;
38             if(v==end)
39             {
40                 maxflow += aug;
41                 for(u = pre[u]; v!=start; v = u, u = pre[u])
42                 {
43                     flow[u][v] += aug;
44                     flow[v][u] -= aug;
45                 }
46                 aug = INF;
47             }
48             goto loop;
49         }
50 
51         int mindis = nodenum-1;
52         for(int v = 0; v<nodenum; v++)
53             if(maze[u][v]-flow[u][v]>0)
54                 mindis = min(mindis, dis[v]);
55 
56         dis[u]=mindis+1;
57         u = pre[u];
58     }
59     return maxflow;
60 }
61 
62 int main()
63 {
64     int N, F, D;
65     while(scanf("%d%d%d",&N,&F,&D)!=EOF)
66     {
67         memset(maze, 0, sizeof(maze));
68         for(int i = 0; i<N; i++)
69         {
70             int f, d, v;
71             scanf("%d%d", &f,&d);
72             while(f--)
73             {
74                 scanf("%d", &v);
75                 v--;
76                 maze[v][F+D+i] = 1; //food --> cow
77             }
78             while(d--)
79             {
80                 scanf("%d", &v);
81                 v--;
82                 maze[F+D+N+i][F+v] = 1; //cow' --> drink
83             }
84             maze[F+D+i][F+D+N+i] = 1;   // cow --> cow'
85         }
86 
87         int start = F+D+2*N, end = F+D+2*N+1;
88         for(int i = 0; i<F; i++) maze[start][i] = 1;    //超级源点 --> food
89         for(int i = F; i<F+D; i++) maze[i][end] = 1;    //drink --> 超级汇点
90 
91         int ans = sap(start, end, F+D+2*N+2);
92         printf("%d\n", ans);
93     }
94 }
View Code

 

posted on 2017-12-19 10:55  h_z_cong  阅读(288)  评论(0编辑  收藏  举报

导航