POJ 3281 Dining (Max stream)

Source: Internet
Author: User
POJ 3281 Dining

Question link

N cows, each of which has some favorite foods and drinks, each of which has only one, q: How many cows can match? Every Ox can eat his favorite food and drinks?

Idea: The largest stream, modeling source point to each food connected to an edge, capacity is 1, each beverage linked to a edge capacity is 1, and then because each ox has CAPACITY 1, so we need to split the ox, and then feed it into the ox, and feed the ox out, and run the maximum stream.

Code:

# Include <cstdio> # include <cstring> # include <queue> # include <algorithm> using namespace std; const int MAXNODE = 505; const int MAXEDGE = 100005; typedef int Type; const Type INF = 0x3f3f3f; struct Edge {int u, v; Type cap, flow; Edge () {} Edge (int u, int v, Type cap, Type flow) {this-> u = u; this-> v = v; this-> cap = cap; this-> flow = flow ;}}; struct Dinic {int n, m, s, t; Edge edges [MAXEDGE]; int first [MAXNODE ]; Int next [MAXEDGE]; bool vis [MAXNODE]; Type d [MAXNODE]; int cur [MAXNODE]; vector <int> cut; void init (int n) {this-> n = n; memset (first,-1, sizeof (first); m = 0;} void add_Edge (int u, int v, Type cap) {edges [m] = Edge (u, v, cap, 0); next [m] = first [u]; first [u] = m ++; edges [m] = Edge (v, u, 0, 0); next [m] = first [v]; first [v] = m ++;} bool bfs () {memset (vis, false, sizeof (vis); queue <int> Q; Q. push (s); d [s] = 0; v Is [s] = true; while (! Q. empty () {int u = Q. front (); Q. pop (); for (int I = first [u]; I! =-1; I = next [I]) {Edge & e = edges [I]; if (! Vis [e. v] & e. cap> e. flow) {vis [e. v] = true; d [e. v] = d [u] + 1; Q. push (e. v) ;}}return vis [t];} Type dfs (int u, Type a) {if (u = t | a = 0) return; type flow = 0, f; for (int & I = cur [u]; I! =-1; I = next [I]) {Edge & e = edges [I]; if (d [u] + 1 = d [e. v] & (f = dfs (e. v, min (a, e. cap-e. flow)> 0) {e. flow + = f; edges [I ^ 1]. flow-= f; flow + = f; a-= f; if (a = 0) break;} return flow;} Type Maxflow (int s, int t) {this-> s = s; this-> t = t; Type flow = 0; while (bfs () {for (int I = 0; I <n; I ++) cur [I] = first [I]; flow + = dfs (s, INF);} return flow;} void MinCut () {cut. clear (); for (int I = 0; I < M; I + = 2) {if (vis [edges [I]. u] &! Vis [edges [I]. v]) cut. push_back (I) ;}} gao; int f, d, n; int main () {while (~ Scanf ("% d", & n, & f, & d) {int s = 0, t = f + d + n * 2 + 1; gao. init (t + 1); for (int I = 1; I <= f; I ++) gao. add_Edge (s, I, 1); for (int I = 1; I <= d; I ++) gao. add_Edge (f + I, t, 1); for (int I = 1; I <= n; I ++) {gao. add_Edge (I + f + d, n + f + d + I, 1); int fn, dn; scanf ("% d", & fn, & dn ); int v; while (fn --) {scanf ("% d", & v); gao. add_Edge (v, I + f + d, 1) ;}while (dn --) {scanf ("% d", & v); gao. add_Edge (n + f + d + I, f + v, 1) ;}} printf ("% d \ n", gao. maxflow (s, t);} return 0 ;}


POJ 3281 Dining (Max stream)

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.