日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 3281】Dining(拆点建图,网络流最大流)

發(fā)布時(shí)間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 3281】Dining(拆点建图,网络流最大流) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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

Line 1: Three space-separated integers:?N,?F, and?D?
Lines 2..?N+1: Each line?i?starts with a two integers?Fi?and?Di, the number of dishes that cow?i?likes and the number of drinks that cow?i?likes. The next?Fiintegers denote the dishes that cow?i?will eat, and the?Di?integers following that denote the drinks that cow?i?will drink.

Output

Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes

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

One way to satisfy three cows is:?
Cow 1: no meal?
Cow 2: Food #2, Drink #2?
Cow 3: Food #1, Drink #1?
Cow 4: Food #3, Drink #3?
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.

題目大意:

有F種食物和D種飲料,每種食物或飲料只能供一頭牛享用,且每頭牛只享用一種食物和一種飲料。現(xiàn)在有N頭牛,每頭牛都有自己喜歡的食物種類(lèi)列表和飲料種類(lèi)列表,問(wèn)最多能使幾頭牛同時(shí)享用到自己喜歡的食物和飲料。(1 <= F <= 100, 1 <= D <= 100, 1 <= N <= 100)

解題報(bào)告:
以如下的順序依次建圖, 每條邊權(quán)值均為1
源點(diǎn) -> 食物 -> 牛 -> 牛 -> 飲料 -> 匯點(diǎn)

因?yàn)槊總€(gè)牛只能被選擇一次,所以需要將牛拆成兩個(gè)點(diǎn)并且連一條流量為1的邊。

AC代碼:

#include<cstring> #include<cstdio> #include<algorithm> #include<iostream> using namespace std; int n,F,D; int tot; struct Edge {int to,ne,w; } e[100005 * 2]; int head[10005]; int st,ed; int dis[10050],q[10005];//一共多少個(gè)點(diǎn)跑bfs,dis數(shù)組和q數(shù)組就開(kāi)多大。 void add(int u,int v,int w) {e[++tot].to=v;e[tot].w=w;e[tot].ne=head[u];head[u]=tot; } bool bfs(int st,int ed) {memset(dis,-1,sizeof(dis));int front=0,tail=0;q[tail++]=st;dis[st]=0;while(front<tail) {int cur = q[front];if(cur == ed) return 1;front++;for(int i = head[cur]; i!=-1; i = e[i].ne) {if(e[i].w&&dis[e[i].to]<0) {q[tail++]=e[i].to;dis[e[i].to]=dis[cur]+1;}}}if(dis[ed]==-1) return 0;return 1; } int dfs(int cur,int limit) {//limit為源點(diǎn)到這個(gè)點(diǎn)的路徑上的最小邊權(quán) if(limit==0||cur==ed) return limit;int w,flow=0;for(int i = head[cur]; i!=-1; i = e[i].ne) { if(e[i].w&&dis[e[i].to]==dis[cur]+1) {w=dfs(e[i].to,min(limit,e[i].w));e[i].w-=w;e[i^1].w+=w;flow+=w;limit-=w;if(limit==0) break;}}if(!flow) dis[cur]=-1;return flow; } int dinic() {int ans = 0;while(bfs(st,ed)) ans+=dfs(st,0x7fffffff);return ans; } int main() {cin>>n>>F>>D;int N = F + D + 2*n + 2;st = F + D + 2*n + 1;ed = F + D + 2*n + 2;tot=1;for(int i = 1; i<=ed; i++) head[i] = -1;for(int i = 1; i<=F; i++) add(st,i,1),add(i,st,0);for(int i = 1; i<=D; i++) add(F+2*n+i,ed,1),add(ed,F+2*n+i,0);for(int i = F+1; i<=F+n;i++) add(i,i+n,1),add(i+n,i,0);for(int numx,numy,i = 1; i<=n; i++) {scanf("%d%d",&numx,&numy);for(int x,j = 1; j<=numx; j++) scanf("%d",&x),add(x,F+i,1),add(F+i,x,0);for(int x,j = 1; j<=numy; j++) scanf("%d",&x),add(F+i+n,F+2*n+x,1),add(F+2*n+x,F+i+n,0);}printf("%d\n",dinic());return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的【POJ - 3281】Dining(拆点建图,网络流最大流)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。