标签:strong lap img namespace ring nts names ble gif
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25452 | Accepted: 11183 |
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
题解:建图方法,将牛拆分为两个点,之间建边流量为1。 源点->食物->牛1->牛2->饮料->汇点
ps:下面的dinic一开始没有反向边建0在poj上wa了。。。
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <cstring> #include <string> #include <set> #include <map> #include <stack> #include <queue> using namespace std; #define LL long long #define add addedge #define infw in #define ofw out #define inf INF #define mp edge const int MAXN = 1e5 + 10; const int INF = 0x3f3f3f3f; const LL mod = 998244353; int N,F,D; struct Edge{ int u,v,cap,next; }edge[MAXN]; int pre[MAXN]; int dis[MAXN],cur[MAXN]; int cnt; int sp,tp; void init(){ cnt = 0; memset(pre,-1, sizeof(pre)); } void addedge(int u,int v,int w){ edge[cnt].u = u; edge[cnt].v = v; edge[cnt].cap = w; edge[cnt].next = pre[u]; pre[u] = cnt++; edge[cnt].u = v; edge[cnt].v = u; edge[cnt].cap = 0; edge[cnt].next = pre[v]; pre[v] = cnt ++; } bool bfs(){ memset(dis,-1,sizeof dis); queue<int>que; while(!que.empty()) que.pop(); que.push(sp); dis[sp] = 0; int u,v; while(!que.empty()){ u = que.front(); que.pop(); for(int i = pre[u]; i != -1; i = edge[i].next){ v = edge[i].v; if(dis[v] == -1 && edge[i].cap > 0){ dis[v] = dis[u] + 1; que.push(v); if(v == tp) break; } } } return dis[tp] != -1; } int dfs(int u,int cap){ if(u == tp || cap == 0) return cap; int res = 0,f; for(int i = cur[u]; i != -1 ; i = edge[i].next){ int v = edge[i].v; if(dis[v] == dis[u] + 1 && (f = dfs(v,min(cap - res,edge[i].cap))) > 0){ edge[i].cap -= f; edge[i ^ 1].cap += f; res += f; if(res == cap) return cap; } } if(!res) dis[u] = -1; return res; } int dinic(){ int ans = 0; while(bfs()){ for(int i = sp ; i <= tp ;i++) cur[i] = pre[i]; ans += dfs(sp,INF); } return ans; } int main() { sp = 0; tp = 500; init(); while(~scanf("%d %d %d",&N,&F,&D)) { int ff[MAXN], dd[MAXN]; for (int i = 1; i <= F; i++){ addedge(sp, i + 100, 1); addedge(i + 100,sp,0); } for (int i = 1; i <= D; i++) { addedge(i + 200, tp, 1); addedge(tp, i + 200, 0); } for (int i = 1; i <= N; i++) { addedge(i, i + 300, 1); addedge(i + 300,i,0); int nf, nd; scanf("%d %d", &nf, &nd); int tmp; for (int j = 0; j < nf; j++) { scanf("%d", &tmp); addedge(tmp + 100, i, 1); addedge(i,tmp + 100,0); } for (int j = 0; j < nd; j++) { scanf("%d", &tmp); addedge(i + 300, tmp + 200, 1); addedge(tmp + 200,i + 300,0); } } int ans = dinic(); printf("%d\n", ans); } }
标签:strong lap img namespace ring nts names ble gif
原文地址:https://www.cnblogs.com/smallhester/p/11252164.html