标签:
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 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
Output
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
题目大意:有F种食物,D种饮料,N头奶牛,只能吃某种食物和饮料(而且只能吃特定的一份),一种食物被一头牛吃了之后,其余牛就不能吃了。
第一行有N,F,D三个整数
接着2-N+1行代表第i头牛,前面两个整数是Fi与Di(食物与饮料的种类数量),接着是食物的种类与饮料的种类
要求输出最多分配能够满足的牛的数量
错误思路:建立超级源点和超级汇点。让源点指向食物,食物指向牛,牛再指向饮料,最后让饮料指向汇点,中间所有边权为1。求最大流。
食物指向牛,牛再指向饮料。因为这样不能保证一头牛可能得到多种食物和多种饮料
正确思路:建立超级源点和超级汇点,把每头牛拆分成左,右牛。让源点 --> 食物 --> 左牛 -- > 右牛 --> 饮料 --> 汇点,所有边权为1。求最大流。
源点:0
食物:2 n + 1 -- 2 n + f
左牛:1 -- n
右牛:n + 1 -- 2 n
饮料:2n + f + 1 -- 2n + f + d
汇点:2n + f + d + 1
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <queue> #define maxn 1000 #define maxm 200000 + 2000 #define INF 0x3f3f3f3f using namespace std; int dist[maxn], vis[maxn]; int cur[maxn], head[maxn], cnt; int N, F, D; int sect;//超级汇点 struct node { int u, v, cap, flow, next; }; node edge[maxm]; void init(){ cnt = 0; memset(head, -1, sizeof(head)); } void add(int u, int v, int w){ edge[cnt] = {u, v, w, 0, head[u]}; //正建边 head[u] = cnt++; edge[cnt] = {v, u, 0, 0, head[v]}; //反建边 head[v] = cnt++; } void getmap(){ int Fans, Dans; for(int i = 1; i <= N; ++i){ scanf("%d%d", &Fans, &Dans); while(Fans--){ int num; scanf("%d", &num); add(2 * N + num, i, 1);//食物和左牛连接 } while(Dans--){ int num; scanf("%d", &num); add(N + i, 2 * N + F + num, 1);//右牛和饮料连接 } add(i, i + N, 1);//左牛和右牛连接 } sect = 2 * N + F + D + 1; for(int i = 1; i <= F; ++i) add(0, 2 * N + i, 1);//源点和食物连接 for(int i = 1; i <= D; ++i) add(2 * N + F + i, 2 * N + F + D + 1, 1);//饮料和超级汇点连接 } bool BFS(int st, int ed){ queue<int>q; memset(dist, -1, sizeof(dist)); memset(vis, 0 ,sizeof(vis)); q.push(st); dist[st] = 0; vis[st] = 1; while(!q.empty()){ int u = q.front(); q.pop(); for(int i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(!vis[E.v] && E.cap > E.flow){ vis[E.v] = 1; dist[E.v] = dist[u] + 1; if(E.v == ed) return true; q.push(E.v); } } } return false; } int DFS(int x, int ed, int a){ if(x == ed || a == 0) return a; int flow = 0, f; for(int &i = cur[x]; i != -1; i = edge[i].next){ node &E = edge[i]; if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){ E.flow += f; edge[i ^ 1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow; } int maxflow(int st, int ed){ int sumflow = 0; while(BFS(st, ed)){ memcpy(cur, head, sizeof(head)); sumflow += DFS(st, ed, INF); } return sumflow; } int main (){ while(scanf("%d%d%d", &N, &F, &D) != EOF){ init(); getmap(); printf("%d\n", maxflow(0, sect)); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/hpuhjh/article/details/47274799