Poj 3281 dining network stream

Source: Internet
Author: User

Nheaded ox, d kind of beverage, F kind of food, every day the ox eats one kind of food one kind of beverage, both food and drink have only one. Ask the maximum number of cows.

Diagram: as each ox only needs one drink and one food, each Ox needs to be split into two points, with a capacity of 1. From the start point to the side with a capacity of 1 for all foods, and from the beverage to the sink point, the side with a capacity of 1. The ox connects with food and drinks.

Just take a closer look at the graph ..

# Include <cstdio>
# Include <iostream>
# Include <cstring>
# Include <algorithm>
# Define n 5005
# Define M 100005 // This should be large enough ..
# Define INF 999999999
Using namespace STD;

Int S, T, num, adj [N], DIS [N], Q [N];
Struct edge
{
Int V, W, pre;
} E [m];

Int min (INT g, int H)
{
If (G> H)
Return h;
Else
Return g;
}
Void insert (int u, int V, int W) // The Insert above can be written as follows ..
{
E [num]. V = V;
E [num]. W = W;
E [num]. Pre = adj [u];
Adj [u] = num ++;
E [num]. V = u;
E [num]. W = 0;
E [num]. Pre = adj [v];
Adj [v] = num ++;

}
Int BFS ()
{
Int I, X, V, tail = 0, head = 0;
Memset (DIS, 0, sizeof (DIS ));
Dis [s] = 1;
Q [tail ++] = s;
While (Head <tail)
{
X = Q [head ++];
For (I = adj [X]; I! =-1; I = E [I]. PRE)
If (E [I]. W & dis [V = E [I]. V] = 0)
{
Dis [v] = dis [x] + 1;
If (V = T)
Return 1;
Q [tail ++] = V;
}
}
Return 0;
}
Int DFS (int s, int limit)
{
If (S = T)
Return limit;
Int I, V, TMP, cost = 0;
For (I = adj [s]; I! =-1; I = E [I]. PRE)
If (E [I]. W & dis [s] = dis [V = E [I]. V]-1)
{
TMP = DFS (v, min (Limit-cost, E [I]. W ));
If (TMP> 0)
{
E [I]. W-= TMP;
E [I ^ 1]. W + = TMP;
Cost + = TMP;
If (Limit = cost)
Break;
}
Else dis [v] =-1;
}
Return cost;
}
Int dinic ()
{
Int ans = 0;
While (BFS ())
Ans + = DFS (S, INF );
Return ans;
}
Int main ()
{
Int num, F, D;
Int I, j, FJ, DJ, F, D;
While (scanf ("% d", & num, & F, & D )! = EOF)
{
Memset (adj,-1, sizeof (adj ));
Num = 0;
For (I = 1; I <= f; I ++)
Insert (0, I, 1 );
For (I = F + 2 * num + 1; I <= F + 2 * num + D; I ++)
Insert (I, F + D + 2 * num + 1, 1 );
For (I = F + 1; I <= F + num; I ++)
Insert (I, I + num, 1 );
For (I = 1; I <= num; I ++)
{
Cin> F> D;
For (j = 1; j <= f; j ++)
{
Cin> FJ;
Insert (FJ, I + F, 1 );
}
For (j = 1; j <= D; j ++)
{
Cin> DJ;
Insert (F + num + I, F + 2 * num + DJ, 1 );
}
}
S = 0;
T = F + D + 2 * num + 1;
Printf ("% d \ n", dinic ());
}
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.