POJ 3281 Dining (拆点)【最大流】

<题目链接>

题目大意:

有N头牛,F种食物,D种饮料,每一头牛都有自己喜欢的食物和饮料,且每一种食物和饮料都只有一份,让你分配这些食物和饮料,问最多能使多少头牛同时获得自己喜欢的食物和饮料。

解题分析:

开始还以为是一道匹配问题,后面才知道这是用网络流求解。

首先我们要明确,如果按照源点——>食物——>牛——>饮料——>汇点这样建图,是不符合题目条件的。因为题目要求每头牛只能吃一份食物和饮料,而这样建图,如果一头牛对应多个食物和饮料,那这样那头牛是可以吃多份食物和饮料的,所以我们需要对牛进行拆点,并且拆除的两点之间用容量为1边相连,这样跑最大流的时候就能够限制每头牛最多只能吃一份食物和饮料了。下面是Dinic的模板。

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 #include <algorithm>
  5 using namespace std;
  6
  7 #define INF 0x3f3f3f3f
  8 const int maxn = 500;
  9 const int maxm = 1e5;
 10
 11 int depth[maxn], vis[maxn],cur[maxn], head[maxn];
 12 int N, F, D;
 13 int sect,cnt;    //sect为汇点
 14 struct Edge {
 15     int v, cap, flow, next;
 16 }edge[maxm];
 17
 18 void init(){
 19     cnt = 0;
 20     memset(head, -1, sizeof(head));
 21 }
 22
 23 void add(int u, int v, int w){     //建立双向边
 24     edge[cnt].v = v, edge[cnt].cap = w, edge[cnt].flow = 0,edge[cnt].next = head[u];
 25     head[u] = cnt++;
 26     edge[cnt].v = u, edge[cnt].cap = 0, edge[cnt].flow = 0,edge[cnt].next = head[v];
 27     head[v] = cnt++;
 28 }
 29
 30 void getmap(){
 31     int tmp1, tmp2;
 32     for(int i = 1; i <= N; ++i){
 33         scanf("%d%d", &tmp1, &tmp2);
 34         while(tmp1--){
 35             int num;
 36             scanf("%d", &num);
 37             add(2 * N + num, i, 1);//食物和左牛连接
 38         }
 39         while(tmp2--){
 40             int num;
 41             scanf("%d", &num);
 42             add(N + i, 2 * N + F + num, 1);//右牛和饮料连接
 43         }
 44         add(i, i + N, 1);//左牛和右牛连接
 45     }
 46     sect = 2 * N + F + D + 1;
 47     for(int i = 1; i <= F; ++i)
 48         add(0, 2 * N + i, 1);//源点和食物连接
 49     for(int i = 1; i <= D; ++i)
 50         add(2 * N + F + i, 2 * N + F + D + 1, 1);//饮料和超级汇点连接
 51 }
 52
 53 bool BFS(int st, int ed){    //构建分层图,并且判断增广路径是否存在
 54     queue<int>q;
 55     memset(depth, -1, sizeof(depth));    //将所有点分层,初始化深度为-1
 56     memset(vis, 0 ,sizeof(vis));
 57     q.push(st);
 58     depth[st] = 0;   //源点深度为0
 59     vis[st] = 1;
 60     while(!q.empty()){
 61         int u = q.front();
 62         q.pop();
 63         for(int i = head[u]; i != -1; i = edge[i].next){
 64             Edge &E = edge[i];
 65             if(!vis[E.v] && E.cap > E.flow){
 66                 vis[E.v] = 1;
 67                 depth[E.v] = depth[u] + 1;    //下一个点是当前点的深度+1
 68                 if(E.v == ed) return true;    //找到汇点则直接返回
 69                 q.push(E.v);
 70             }
 71         }
 72     }
 73     return false;    //没有找到通向汇点的增广路径
 74 }
 75
 76 int DFS(int x, int ed, int val){
 77     if(x == ed || val == 0)
 78         return val;
 79     int flow  = 0, f;
 80     for(int &i = cur[x]; i != -1; i = edge[i].next){   //每次从当前弧开始找
 81         Edge E=edge[i];
 82         if(depth[E.v] == depth[x] + 1 && (f = DFS(E.v, ed, min(val, E.cap - E.flow))) > 0){
 83             edge[i].flow += f;
 84             edge[i ^ 1].flow -= f;
 85             flow += f;
 86             val -= f;
 87             if(val == 0) break;
 88         }
 89     }
 90     return flow;
 91 }
 92
 93 int Dinic(int st, int ed){
 94     int sumflow = 0;  //最大流
 95     while(BFS(st, ed)){     //判断是否存在增广路
 96         memcpy(cur, head, sizeof(head));    //当前弧优化
 97         sumflow += DFS(st, ed, INF);
 98     }
 99     return sumflow;
100 }
101
102 int main (){
103     while(scanf("%d%d%d", &N, &F, &D) != EOF){
104         init();
105         getmap();    //建图
106         printf("%d\n", Dinic(0, sect));
107     }
108     return 0;
109 }

2018-11-23

原文地址:https://www.cnblogs.com/00isok/p/10008683.html

时间: 2024-08-01 11:16:01

POJ 3281 Dining (拆点)【最大流】的相关文章

POJ 3281【拆点 &amp;&amp; 最大流经典建图】

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

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 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(网络最大流)

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

POJ 3281 网络流 拆点保证本身只匹配一对食物和饮料

如何建图? 最开始的问题就是,怎么表示一只牛有了食物和饮料呢? 后来发现可以先将食物与牛匹配,牛再去和饮料匹配,实际上这就构成了三个层次. 起点到食物层边的容量是1,食物层到奶牛层容量是1,奶牛层到饮料层容量是1,饮料层到终点容量是1. 但是后来发现有一组hack数据: 2 3 3 3 3 1 2 3 1 2 3 3 3 1 2 3 1 2 3 我们发现一头奶牛居然吃了多个套餐,所以要解决这个只需要将自己与自己建立一条容量是1的边就行了. #include <cstdio> #include

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(网络流拆点)

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