Poj 3281 dinic template question

Source: Internet
Author: User

[Topic]
There are f kinds of food and D kinds of drinks, each type of food or beverage can only be enjoyed by one ox, and each ox can only enjoy one
One kind of food and one kind of drinks. Now there are n cows, each of which has a list of their favorite food types and drinks
A list of the expected types of food and drinks. (1 <= F <=
100, 1 <= d <= 100, 1 <= n <= 100)

[Modeling method]
The modeling method of this question is pioneering. In the past, a point set on the left usually represents supply and is connected to the source,
A point set on the right represents the requirement and is connected to the sink. Now it is different. There are two types of resources available, and the demand is still limited.
What should a group do? In fact, you only need to think carefully about the modeling principle of the largest stream, and the composition of this question is not
It is so hard to think about. The correctness of the maximum stream depends on the one-to-one comparison between each s-t stream and a practical solution.
Yes. Then, we need to use the s-t stream to "string" a cow and its favorite food and drinks.
There is no direct relationship between things and drinks. Naturally, I want to put the ox in the middle, with food and drinks on both sides,
Concatenates S and T to form a allocation scheme. Now the modeling method is obvious:
Things I act as a vertex and edge (S, I, 1). Each kind of beverage J acts as a vertex and edge (J, T, 1), and each ox K
Split into two vertices K' and k'' and link edge (K', k'', 1), (I, K', 1), (k'', J, 1), where I, J is a favorite food of ox K.
Or drinks. The result is obtained when the maximum stream is obtained.

# Include <stdio. h> # include <string. h> const int EDGE_NUM = 20001; // number of sides const int POINT_NUM = 501; // points struct edge {int v; // point int next; // the lower side of int value; // current edge traffic} edge [2 * EDGE_NUM]; // edge information, stored in the form of an adjacent table int p [POINT_NUM]; // p [I] records the id of the last edge starting with I, that is, the last edge starting with I is edge [p [I], edge [p [I]. next is the penultimate edge starting with I, and so on. int level [POINT_NUM]; // level [I] records the level of int que [POINT_NUM] of the I point. out [POINT_NUM]; // secondary array int edgeNumber; void init () {edgeNumber = 0; me Mset (p,-1, sizeof (p);} inline void addEdge (int from, int to, int value) // Add an edge, store {edge [edgeNumber] in the form of an adjacent table. v = to; edge [edgeNumber]. value = value; edge [edgeNumber]. next = p [from]; p [from] = edgeNumber ++;} int Dinic (int source, int sink, int n) {int I, maxFlow = 0; while (true) {int head, tail; for (I = 0; I <n; I ++) level [I] = 0; level [source] = 1; // The source point is the first layer head = 0; tail = 0; que [0] = source; // que here when the team uses while (head <= tail )/ /BFS the remaining graph, calculate each accessible vertex level {int cur = que [head ++]; for (I = p [cur]; I! =-1; I = edge [I]. next) {if (edge [I]. value> 0 & level [edge [I]. v] = 0) {level [edge [I]. v] = level [cur] + 1; que [++ tail] = edge [I]. v ;}}if (level [sink] = 0) break; // There is no augmented path for (I = 0; I <n; I ++) out [I] = p [I]; // out [I] dynamically records available edges int q =-1; // q indicates the number of points that have been searched, que stores path edge information while (true) // DFS remaining graph, search for augmented path {if (q <0) // The current path is empty {int cur = out [source]; for (; cur! =-1; cur = edge [cur]. next) // find the first edge {if (edge [cur]. value> 0 & out [edge [cur]. v]! =-1 & level [edge [cur]. v] = 2) // the first valid edge must meet the following conditions: 1. the traffic is greater than 0; 2. the endpoint has available edge 3: The endpoint level is 2 break;} if (cur =-1) break; // The second layer cannot be found, the remaining graph has not added the channel que [++ q] = cur; // Save the first edge id out [source] = edge [cur]. next;} int curnode = edge [que [q]. v; // the end of the current path if (curnode = sink) // find an augmented path {int thisflow = edge [que [0]. value; // thisflow is the int index of the current traffic increasing direction = 0; // id of the smallest traffic edge marked for (I = 1; I <= q; I ++) {if (thisflow> edge [que [I]. value) {thisflow = edge [que [I]. value; in Dex = I ;}} maxFlow + = thisflow; for (I = 0; I <= q; I ++) {edge [que [I]. value-= thisflow; edge [que [I] ^ 1]. value + = thisflow; // edge in the opposite direction} q = index-1; // when searching for the next augmented path, you can directly use the first q edge} else of the current path. // The sink point {int cur = out [curnode]; for (; cur! =-1; cur = edge [cur]. next) {if (edge [cur]. value> 0 & out [edge [cur]. v]! =-1 & level [edge [cur]. v] = level [curnode] + 1) break;} if (cur =-1) // There is no next path {out [curnode] =-1; // mark the reachable edge of the current vertex as 0 q --;} else {que [++ q] = cur; out [curnode] = edge [cur]. next; // The reachable edge in the next search from edge [cur]. next start finding }}}return maxFlow;} int main () {int Nn, Ff, Dd; while (scanf ("% d", & Nn, & Ff, & Dd )! = EOF) {init (); int foodstart = 1; int cow1 = Ff + 2; int cow2 = cow1 + Nn + 1; int drinkstart = cow2 + Nn + 1; int end = drinkstart + Dd + 1; int I; for (I = 0; I <Nn; I ++) // Add the ox edge {addEdge (cow1 + I, cow2 + I, 1); addEdge (cow2 + I, cow1 + I, 0);} for (I = 0; I <Ff; I ++) // Add the food edge {addEdge (0, foodstart + I, 1); addEdge (foodstart + I,) ;}for (I = 0; I <Dd; I ++) // Add beverage {addEdge (drinkstart + I, end, 1); addEdge (end, drinkstart + I, 0);} for (I = 0; I <Nn; I ++) {int f, d; scanf ("% d", & f, & d); int x; while (f --) {scanf ("% d", & x); x --; addEdge (foodstart + x, cow1 + I, 1); addEdge (cow1 + I, foodstart + x, 0);} while (d --) {scanf ("% d", & x); x --; addEdge (cow2 + I, drinkstart + x, 1 ); addEdge (drinkstart + x, cow2 + I, 0) ;}} printf ("% d \ n", Dinic (0, end, end + 1 ));} return 0 ;}

Attach dinic Template

struct edge  {       int v, next;       int val;   } net[ 100010 ];     int level[maxn], Qu[maxn], out[maxn],next[maxn];  class Dinic {   public:       int end;      Dinic() {           end = 0;           memset( next, -1, sizeof(next) );       }       inline void insert( int x, int y, int c) {           net[end].v = y, net[end].val = c,          net[end].next = next[x],           next[x] = end ++;           net[end].v = x, net[end].val = 0,          net[end].next = next[y],           next[y] = end ++;       }       bool BFS( int S, int E ) {           memset( level, -1, sizeof(level) );           int low = 0, high = 1;           Qu[0] = S, level[S] = 0;           for( ; low < high; ) {               int x = Qu[low];               for( int i = next[x]; i != -1; i = net[i].next ) {                   if( net[i].val == 0 ) continue;                   int y = net[i].v;                   if( level[y] == -1 ) {                       level[y] = level[x] + 1;                       Qu[ high ++] = y;                   }               }               low ++;           }           return level[E] != -1;       }            int MaxFlow( int S, int E ){           int maxflow = 0;           for( ; BFS(S, E) ; ) {               memcpy( out, next, sizeof(out) );               int now = -1;               for( ;; ) {                   if( now < 0 ) {                       int cur = out[S];                       for(; cur != -1 ; cur = net[cur].next )                            if( net[cur].val && out[net[cur].v] != -1 && level[net[cur].v] == 1 )                               break;                       if( cur >= 0 ) Qu[ ++now ] = cur, out[S] = net[cur].next;                       else break;                   }                   int u = net[ Qu[now] ].v;                   if( u == E ) {                       int flow = inf;                       int index = -1;                       for( int i = 0; i <= now; i ++ ) {                           if( flow > net[ Qu[i] ].val )                               flow = net[ Qu[i] ].val, index = i;                       }                       maxflow += flow;                       for( int i = 0; i <= now; i ++ )                           net[Qu[i]].val -= flow, net[Qu[i]^1].val += flow;                       for( int i = 0; i <= now; i ++ ) {                           if( net[ Qu[i] ].val == 0 ) {                               now = index - 1;                               break;                           }                       }                   }                   else{                       int cur = out[u];                       for(; cur != -1; cur = net[cur].next )                            if (net[cur].val && out[net[cur].v] != -1 && level[u] + 1 == level[net[cur].v])                               break;                       if( cur != -1 )                           Qu[++ now] = cur, out[u] = net[cur].next;                       else out[u] = -1, now --;                   }               }           }           return maxflow;       }   };     

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.