Poj 3281 dining (maximum flow creation & ISAP & Split points)

Source: Internet
Author: User

Question link: http://poj.org/problem? Id = 3281


Try to build a graph !!!


Question:There are n cows,There are f kinds of food and D kinds of drinks, each type of food or drink can only be enjoyed by one ox, and each ox can only enjoy one kind of food and one kind of drink.

Row 10-row n + 1. I like a kind of food, B kind of drinks, andList of food types and drinksThe list of material types.

Q: How many cows can enjoy their happiness at the same time?Huan food and drinks.->Maximum stream.


The difficulty isGraph creation:


Ideas: Generally, it is represented by a set on the left.Source pointAndSupplyConnected, represented by a set on the rightRequirementAndSettlement point.

However, in this question, cattle are still a group as demand, but there are two collections of food and drinks, so one is split into two cows:

Ox Ontology (OX/entry point), Cattle separation (NIU '/out point)

Figure creation method: source point-> food, food-> ox, ox-> ox, ox-> drinks, drinks-> sink point.




Id 3281. Accepted 240 K 0 ms C ++

Add! = EOF is 16 Ms. It's strange... 0 Ms if not added


# Include <iostream> # include <cstdlib> # include <cstdio> # include <cstring> # include <queue> # include <algorithm> const int n = 210; const int maxn = 2000; const int maxm = 80000; # define min int_min # define Max int_max # define ll long # define max (A, B) (A> B )? (A) :( B) # define min (A, B) (A> B )? (B) :( A) using namespace STD; int head [maxn], sum, bnum; int dis [maxn]; int num [maxn]; int cur [maxn]; int pre [maxn]; struct node {int V, cap; int next;} edge [maxm]; void add (int u, int V, int cap) {edge [bnum]. V = V; edge [bnum]. CAP = CAP; edge [bnum]. next = head [u]; head [u] = bnum ++; edge [bnum]. V = u; edge [bnum]. CAP = 0; edge [bnum]. next = head [v]; head [v] = bnum ++;} void BFS (INT source, int sink) {queue <int> q; while (Q. empty () = False) Q. pop (); memset (Num, 0, sizeof (Num); memset (DIS,-1, sizeof (DIS); q. push (sink); DIS [sink] = 0; num [0] = 1; while (! Q. Empty () {int u = Q. Front (); q. Pop (); For (INT I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. v; If (DIS [v] =-1) {dis [v] = dis [u] + 1; num [dis [v] ++; q. push (v) ;}}} int ISAP (INT source, int sink, int N) {memcpy (cur, Head, sizeof (cur); int flow = 0, U = pre [Source] = source; BFS (source, sink); While (DIS [Source] <n) {If (u = sink) {int df = max, pos; For (INT I = source; I! = Sink; I = edge [cur [I]. v) {If (DF> edge [cur [I]. CAP) {df = edge [cur [I]. CAP; Pos = I ;}for (INT I = source; I! = Sink; I = edge [cur [I]. v) {edge [cur [I]. cap-= DF; edge [cur [I] ^ 1]. cap + = DF;} flow + = DF; u = Pos;} int st; For (ST = cur [u]; st! =-1; ST = edge [st]. next) {If (DIS [edge [st]. v] + 1 = dis [u] & edge [st]. CAP) {break ;}} if (st! =-1) {cur [u] = sT; Pre [edge [st]. v] = u; u = edge [st]. v;} else {If (-- num [dis [u]) = 0) break; int mind = N; For (INT id = head [u]; ID! =-1; id = edge [ID]. Next) {If (mind> dis [edge [ID]. V] & edge [ID]. Cap! = 0) {cur [u] = ID; mind = dis [edge [ID]. v] ;}} dis [u] = mind + 1; num [dis [u] ++; If (u! = Source) u = pre [u] ;}} return flow ;}void initt () {memset (Head,-1, sizeof (head); bnum = 0 ;} int main () {int N, F, D; int A, B, C; while (scanf ("% d", & N, & F, & D )! = EOF) {initt (); int source = 0, sink = 2 * n + F + D + 1; for (INT I = 1; I <= F; I ++) // create a source point to all foods {Add (source, I, 1) ;}for (INT I = 1; I <= N; I ++) {scanf ("% d", & A, & B); For (Int J = 1; j <= A; j ++) {scanf ("% d", & C); add (C, F + I, 1); // food to Ox} For (Int J = 1; j <= B; j ++) {scanf ("% d", & C); add (F + I + N, 2 * n + F + C, 1 ); // Niu 'to beverage} Add (F + I, F + I + N, 1); // Niu zhiniu'} For (INT I = 1; I <= D; I ++) {Add (2 * n + F + I, sink, 1); // food to sink} int ans = ISAP (source, sink, sink + 1); printf ("% d \ n", ANS);} return 0 ;}


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.