POJ 3281 Dining Maximum Flow

Source: Internet
Author: User

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

Cows is such finicky eaters. Each cow have a preference for certain foods and drinks, and she'll consume no others.

Farmer John had cooked fabulous meals for him cows, but he forgot to check his menu against their preferences. Although he might not being 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) Typ Es of drinks. Each of him N (1≤ n ≤100) cows has decided whether she's 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 are only being consumed by one cow (i.e., once food type 2 are assigned to a cow, no other cow can be assig Ned Food type 2).

Title Description: There are F food and D drink (1<=f<=100,1<=d<=100), n cattle (1<=n<=100), each cow has its own favorite food and drinks. Now every cow can eat only one kind of food and drink a favorite drink, a food and drink can only be provided to a cow. Ask at most how many cows can eat food to drink.

Algorithm Analysis: We know that there is more than one supply of resources, the demand side only cattle this group, so we can put cattle this group in the middle, on both sides is the supply of resources, the formation of source-food-cow-drink-meeting point of the forward side. For each cow I this node is split into I and i+n two points, the new source point from and sinks to, the source point to each food edge, the weight is 1 (because each food can only be supplied to a cow), each beverage to the meeting point of the edge, the weight of 1. Next is the food-cow-drink relationship: to the cow like Food J Lian Bian J->i, like the drink K even edge i+n->k, the weight of the value is 1.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <queue>8 #defineINF 0x7fffffff9 using namespacestd;Ten Const intmaxn= ++Ten; One  A intn,f,d; - structnode - { the     intU,flow; -     intNext; -}edge[maxn*5]; - inthead[maxn*5],edgenum; + int  from, to; - intd[maxn*5]; +  A voidAddintUintVintflow) at { -Edge[edgenum].u=v; edge[edgenum].flow=flow; -edge[edgenum].next=Head[u]; -head[u]=edgenum++; -  -Edge[edgenum].u=u; edge[edgenum].flow=0; inedge[edgenum].next=Head[v]; -head[v]=edgenum++; to } +  - intBFS () the { *memset (D,0,sizeof(d)); $d[ from]=1;Panax Notoginsengqueue<int>Q; -Q.push ( from); the      while(!q.empty ()) +     { A         intu=Q.front (); Q.pop (); the          for(intI=head[u]; i!=-1; i=edge[i].next) +         { -             intv=edge[i].u; $             if(!d[v] && edge[i].flow>0) $             { -d[v]=d[u]+1; - Q.push (v); the                 if(v==to)return 1; -             }Wuyi         } the     } -     return 0; Wu } -  About intDfsintUintflow) $ { -     if(U==to | | flow==0)returnflow; -     intcap=flow; -      for(intI=head[u]; i!=-1; i=edge[i].next) A     { +         intv=edge[i].u; the         if(d[v]==d[u]+1&& edge[i].flow>0) -         { $             intx=Dfs (V,min (cap,edge[i].flow)); theEdge[i].flow-=x; theedge[i^1].flow + =x; theCap-=x; the             if(cap==0)returnflow; -         } in     } the     returnflow-cap; the } About  the intdinic () the { the     intsum=0; +      while(BFS ()) sum + = DFS ( from, INF); -     returnsum; the }Bayi  the intMain () the { -      while(SCANF ("%d%d%d", &n,&f,&d)! =EOF) -     { thememset (head,-1,sizeof(head)); theedgenum=0; the          from=2*n+f+d+1; theto= from+1; -         intF,d,a; the          for(intI=2*n+1; i<=2*n+f; i++) theAdd fromI1); the          for(intI=2*n+f+1; i<=2*n+f+d; i++)94Add (I,to,1); the          for(intI=1; i<=n; i++.) the         { thescanf"%d%d",&f,&d);98              while(f--) About             { -scanf"%d",&a);101Add2*n+a,i,1);102             }103              while(d--)104             { thescanf"%d",&a);106Add (I+n,2*n+f+a,1);107             }108Add (I,i+n,1);109         } theprintf"%d\n", Dinic ());111     } the     return 0;113}

POJ 3281 Dining Maximum Flow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.