poj 3281 Dining (最大网络流)

题目链接:

  http://poj.org/problem?id=3281

题目大意:

  有n头牛,f种食物,d种饮料,第i头牛喜欢fi种食物和di种饮料,每种食物或者饮料被一头牛选中后,就不能被其他的牛选了,问最多能满足多少头牛的要求?

解题思路:

  最大匹配问题,关键在于如何建图,可以虚构出来一个源点,一个汇点,一共需要f+d+2*n+2个点即可,建图为:源点—>食物—>牛—>牛—>饮料—> 汇点。把牛作为点拆开建图是为了让一头牛只对应一种饮料和一种食物,避免出现对应多种饮料或者多种食物的情况。

代码:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <algorithm>
  5 #include <iostream>
  6 #include <cmath>
  7 #include <queue>
  8 using namespace std;
  9
 10 #define maxn 0x3f3f3f3f
 11 #define N 410
 12 int map[N][N], Layer[N], s, e;
 13 bool visit[N];
 14 bool CountLayer();
 15 int Dinic ();
 16
 17 int main ()
 18 {
 19     int n, f, d, x, y, m;
 20     while (scanf ("%d %d %d", &n, &f, &d) != EOF)
 21     {
 22         s = 0, e = f + d + n + n + 1;
 23         memset (map, 0, sizeof(map));
 24
 25         for (int i=1; i<=f; i++)
 26             map[0][i] = 1;//食物和源点连线
 27
 28         for (int i=1; i<=d; i++)
 29             map[i+f+2*n][e] = 1;//饮料和汇点链接
 30
 31         for (int i=1; i<=n; i++)
 32             map[i+f][i+f+n] = 1;//对应的牛和牛链接
 33
 34         for (int i=1; i<=n; i++)
 35         {//建立牛和食物及饮料的关系
 36             int num = f + i;
 37             scanf ("%d %d", &x, &y);
 38             while (x --)
 39             {
 40                 scanf ("%d", &m);
 41                 map[m][num] = 1;
 42             }
 43             while (y --)
 44             {
 45                 scanf ("%d", &m);
 46                 map[num + n][m+f+2*n] = 1;
 47             }
 48         }
 49         printf ("%d\n", Dinic());
 50     }
 51     return 0;
 52 }
 53
 54 bool CountLayer()
 55 {
 56     deque <int> Q;
 57     memset (Layer, 0, sizeof(Layer));
 58     Layer[0] = 1;
 59     Q.push_back(0);
 60     while (!Q.empty())
 61     {
 62         int nd = Q.front();
 63         Q.pop_front();
 64         for (int i=s; i<=e; i++)
 65         {
 66             if (map[nd][i]>0 && !Layer[i])
 67             {
 68                 Layer[i] = Layer[nd] + 1;
 69                 if (i == e)
 70                     return true;
 71                 else
 72                     Q.push_back(i);
 73             }
 74         }
 75     }
 76     return false;
 77 }
 78
 79 int Dinic ()//Dinic模板
 80 {
 81     int maxnflow = 0, i;
 82     while (CountLayer())
 83     {
 84         deque<int>Q;
 85         memset (visit, 0, sizeof(visit));
 86         visit[0] = 1;
 87         Q.push_back(0);
 88         while (!Q.empty())
 89         {
 90             int nd = Q.back();
 91             if (nd != e)
 92             {
 93                 for (i=0; i<=e; i++)
 94                 {
 95                     if (map[nd][i]>0 && Layer[i] == Layer[nd]+1 && !visit[i])
 96                     {
 97                         visit[i] = 1;
 98                         Q.push_back(i);
 99                         break;
100                     }
101                 }
102                 if (i > e)
103                     Q.pop_back();
104             }
105             else
106             {
107                 int minflow = maxn;
108                 int mv;
109                 for (i=1; i<Q.size(); i++)
110                 {
111                     int ns = Q[i-1];
112                     int ne = Q[i];
113                     if (map[ns][ne] < minflow)
114                     {
115                         minflow = map[ns][ne];
116                         mv = ns;
117                     }
118                 }
119                 maxnflow += minflow;
120                 for (i=1; i<Q.size(); i++)
121                 {
122                     int ns = Q[i-1];
123                     int ne = Q[i];
124                     map[ns][ne] -= minflow;
125                     map[ne][ns] += minflow;
126                 }
127                 while (!Q.empty() && Q.back() != mv)
128                     Q.pop_back();
129             }
130         }
131     }
132     return maxnflow;
133 }
时间: 2024-08-01 11:16:11

poj 3281 Dining (最大网络流)的相关文章

POJ 3281 Dining(网络流拆点)

[题目链接] http://poj.org/problem?id=3281 [题目大意] 给出一些食物,一些饮料,每头牛只喜欢一些种类的食物和饮料, 但是每头牛最多只能得到一种饮料和食物,问可以最多满足几头牛的要求 即同时得到喜欢的饮料和食物 [题解] 建立一个源点连接食物,汇点连接饮料,中间连接牛, 为了防止同一头牛占用多个资源,所以我们对牛进行拆点,限流为1. [代码(Isap)] #include <cstdio> #include <cstring> using names

poj 3281 Dining(网络流+拆点)

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20052   Accepted: 8915 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 fabulo

POJ 3281 Dining(网络最大流)

http://poj.org/problem?id=3281 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9121   Accepted: 4199 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

poj 3281 Dining(最大流)

poj 3281 Dining 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 prefer

POJ 3281 Dining(最大流建图 &amp;&amp; ISAP &amp;&amp; 拆点)

题目链接:http://poj.org/problem?id=3281 努力练建图ing!!! 题意:有 N 头牛,有 F 种食物和 D 种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料. 第2行-第N+1行.是牛i 喜欢A种食物,B种饮料,及食物种类列表和饮料种类列表. 问最多能使几头牛同时享用到自己喜欢的食物和饮料.->最大流. 本题难点是建图: 思路:一般都是左边一个集合表示源点与供应相连,右边一个集合表示需求与汇点相连. 但是本题,牛作为需求仍然是一个群体,但是供

POJ 3281 Dining(最大流)

POJ 3281 Dining 题目链接 题意:n个牛,每个牛有一些喜欢的食物和饮料,每种食物饮料只有一个,问最大能匹配上多少只牛每个牛都能吃上喜欢的食物和喜欢的饮料 思路:最大流,建模源点到每个食物连一条边,容量为1,每个饮料向汇点连一条边容量为1,然后由于每个牛有容量1,所以把牛进行拆点,然后食物连向牛的入点,牛的出点连向食物,跑一下最大流即可 代码: #include <cstdio> #include <cstring> #include <queue> #in

POJ 3281 Dining (网络流最大流 拆点建图 Edmonds-Karp算法)

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10159   Accepted: 4676 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 fabulo

POJ 3281 Dining (网络流之最大流)

题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料.每头牛都有各自喜欢的食物和饮料, 而每种食物或饮料只能分配给一头牛.最多能有多少头牛可以同时得到喜欢的食物和饮料? 析:是一个经典网络流的题,建立一个超级源点,连向每种食物,建立一个超级汇点,连向每种饮料,然后把每头牛拆成两个点, 一个和食物连,一个和饮料连,最后跑一遍最大流即可. 代码如下: #pragma comment(linker, "/STACK:

【网络流#7】POJ 3281 Dining 最大流 - 《挑战程序设计竞赛》例题

不使用二分图匹配,使用最大流即可,设源点S与汇点T,S->食物->牛->牛->饮料->T,每条边流量为1,因为流过牛的最大流量是1,所以将牛拆成两个点. 前向星,Dinic,复杂度:O(V2E) 直接套用模板 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set