POJ 3281:Dining(最大流)

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

题意:有n头牛,f种食物,d种饮料,每头牛有fnum种喜欢的食物,dnum种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种食物了,饮料也同理,问最多有多少头牛可以吃到它喜欢的饮料和食物。

思路:一开始还以为二分匹配可以做,当然如果只有食物或者饮料其中一种就可以做。难点在于建图。看了下书,因为要保证经过牛的流量是1(每种食物对应分配给一头牛,每种饮料对应分配给一头牛,避免一头牛吃多份),所以要把牛拆成两个点。形成这样的路径S->f->niu1->niu2->d->T。

转的图。

 2 #include <cstring>
 3 #include <vector>
 4 #include <queue>
 5 using namespace std;
 6 #define N 410
 7 #define INF 0x3f3f3f3f
 8 struct Edge {
 9     int u, v, cap;
10     Edge () {}
11     Edge (int u, int v, int cap) : u(u), v(v), cap(cap) {}
12 }edge[N*N];
13 vector<int> G[N];
14 int tot, S, T, dis[N], cur[N];
15
16 void AddEdge(int u, int v, int c) {
17     G[u].push_back(tot);
18     edge[tot++] = Edge(u, v, c);
19     G[v].push_back(tot);
20     edge[tot++] = Edge(v, u, 0); // 反向弧的流量是0
21 }
22
23 int BFS() {
24     queue<int> que;
25     que.push(S);
26     memset(dis, INF, sizeof(dis));
27     dis[S] = 0;
28     while(!que.empty()) {
29         int u = que.front(); que.pop();
30         for(int i = 0; i < G[u].size(); i++) {
31             Edge &e = edge[G[u][i]];
32             if(e.cap > 0 && dis[e.v] == INF) {
33                 dis[e.v] = dis[u] + 1;
34                 que.push(e.v);
35             }
36         }
37     }
38     return dis[T] < INF;
39 }
40
41 int DFS(int u, int maxflow) {
42     if(u == T) return maxflow;
43     for(int i = cur[u]; i < G[u].size(); i++) {
44         cur[u] = i;
45         Edge &e = edge[G[u][i]];
46         if(dis[e.v] == dis[u] + 1 && e.cap > 0) {
47             int flow = DFS(e.v, min(maxflow, e.cap));
48             if(flow) {
49                 e.cap -= flow;
50                 edge[G[u][i]^1].cap += flow;
51                 return flow;
52             }
53         }
54     }
55     return 0;
56 }
57
58 int Dinic() {
59     int ans = 0, flow;
60     while(BFS()) {
61 //        puts("BFS");
62         memset(cur, 0, sizeof(cur));
63         while(flow = DFS(S, INF)) ans += flow;
64     }
65     return ans;
66 }
67
68 int main() {
69     int n, f, d;
70     while(~scanf("%d%d%d", &n, &f, &d)) {
71         S = 0, T = 2 * n + f + d + 1, tot = 0;
72         for(int i = 0; i <= T; i++) G[i].clear();
73         for(int i = 1; i <= f; i++)
74             AddEdge(S, 2 * n + i, 1); // 源点到食物
75         for(int i = 1; i <= d; i++)
76             AddEdge(2 * n + f + i, T, 1); // 饮料到汇点
77         for(int i = 1; i <= n; i++) {
78             int fnum, dnum;
79             scanf("%d%d", &fnum, &dnum);
80             AddEdge(i, n + i, 1); // 每只牛拆点
81             for(int j = 1; j <= fnum; j++) {
82                 int v; scanf("%d", &v);
83                 AddEdge(2 * n + v, i, 1); // 食物到牛的第一个点
84             }
85             for(int j = 1; j <= dnum; j++) {
86                 int v; scanf("%d", &v);
87                 AddEdge(n + i, 2 * n + f + v, 1); // 牛的第二个点到饮料
88             }
89         }
90         int ans = Dinic();
91         printf("%d\n", ans);
92     }
93     return 0;
94 }
时间: 2024-10-25 03:42:08

POJ 3281:Dining(最大流)的相关文章

POJ 3281 Dining(最大流)

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

POJ 3281 Dining (最大流)

Dining Time Limit: 2000MS   Memory Limit: 65536K       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

POJ 3281 Dining 最大流 Dinic算法

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

【网络流#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

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(最大流建图 &amp;&amp; ISAP &amp;&amp; 拆点)

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

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 题意: 给出牛n,饮料d还有食物f的数量,每头牛给出喜欢的饮料和食物,最后求出能够满足的牛的数量 解法:源点到食物建边, 由食物到牛建边, 牛到饮料建边 ,饮料到汇点建边 ,求最大流.牛要拆点控制流量为1 全部是有向的边,而且权值全部为1 有2*n+f+d+2个顶点 0表示源点,2*n+f+d+1表示汇点 1到f为食物点,f+1到f+2*n为牛点,f+2*n+1到f+2*n+d为饮料点 代码: #include <stdio

POJ 3281 Dining(最大流dinic&amp;&amp;SAP)

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