Let's talk about building a map ... poj1459:
Add Super Source Point, Super Meeting point, run once.
#include <cstdio>#include <cstring>#include <vector>#include <cstdlib>#include <cmath>#include <queue>#include <algorithm>using namespace STD;Const intMAX =107;Const intINF =0xFFFFFFF;structNode {intto;intCapintRev;}; vector<node>G[max];intLevel[max];BOOLVis[max];intN, NP, NC, M, S, T;inline voidAdd_edge (intUintVintc) {G[u].push_back (node) {V, C, g[v].size ()}); G[v].push_back (node) {u,0, G[u].size ()-1});}//From S-T, with Max Cap:cBOOLBFS (intSintT) { Queue<int>Q; Q.push (S);memset(Level,-1,sizeof(level)); Level[s] =0; while(! Q.empty ()) {intp = Q.front (); Q.pop (); for( vector<node>:: Iterator it = G[p].begin (); It! = G[p].end (); ++it) {if(Level[it->to] <0&& It->cap >0) {Level[it->to] = Level[p] +1; Q.push (it->to);if(it->to = = T)return true; } } }return false;}intDFS (intUintVintc) {if(U = = v)returnCintsum =0, TMP; for( vector<node>:: Iterator it = G[u].begin (); It! = G[u].end (); ++it) {if(Level[it->to] = = Level[u] +1&& It->cap >0) {tmp = DFS (it->to, T, Min (c-sum, it->cap)); sum + = tmp; IT->CAP = tmp; G[it->to][it->rev].cap + = tmp; } }returnsum;}intDinic (intSintT) {intsum =0; while(BFS (S, T)) {memset(Vis,false,sizeof(VIS)); Sum + = DFS (S, T, INF); }returnsum;}intMain () { while(~scanf("%d %d%d%d", &n, &NP, &NC, &m)) {S = n, T = n +1; for(inti =0; I <= T; ++i) g[i].clear ();intA, B, C; for(inti =0; I < m; ++i) {scanf("(%d,%d)%d", &a, &b, &c); Add_edge (A, B, c); } for(inti =0; I < NP; ++i) {scanf("(%d)%d", &a, &c); Add_edge (S, A, c); } for(inti =0; I < NC; ++i) {scanf("(%d)%d", &a, &c); Add_edge (A, T, c); }printf("%d\n", Dinic (S, T)); }return 0;}
poj3436 ACM Computer Factory: Test instructions:
Many machines produce computers, and each machine corresponds to P components.
Input:
0 means there is no
1 means there must be
2 means dispensable
Output:
0 means no
1 indicates that there are
There is also a flow value Q, which is the production capacity.
For example, if there are 3 kinds of components, then the machine {15, [1, 2, 0], [1, 0, 1]} means that it can be 艹 (i.e. processing , editor note) 15 products {The subject is the production of computer}, and the products to be processed must have component 1 (otherwise it may cause malfunction), Element 2 is dispensable (does not affect), must not have the element 3 (otherwise stuck will not pull out); At the same time, after its processing of the product will be left the element 1 and the element 3, there will be no element 2.
After a deep understanding, you can build a diagram:
① Add Super Source Point, Super Meeting point. For each machine to determine whether it can be used as the source point (the input P component requirements are not ' 1 '), whether it can be used as a meeting point (the output of the original P is ' 1 ', that is, the product must be complete). Corresponds with Yuanhui point Edge, Volume INF.
② for each machine, the split, the intermediate flow rate corresponds to the production capacity of that machine.
③ 22 Check each pair of machines, we judge each pair of combinations:
For a component, the machine 1 output is x, machine 2 requires the input as Y, then
x,y=
0,0:ok
0, 1: No.
0, 2: Line
1, 0: No.
1, 1: Line
1, 2: Line
So just check
X+y==1? No: Yes.
Can.
#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <climits>#include <algorithm>using namespace STD;Const intMAX = -;Const intINF =0xFFFFFFF;intG[max <<1][max <<1];intIn[max][max], Out[max][max];intLevel[max <<1];intN, P;structo_o {intUintVintc;};BOOLBFS (intSintT) { Queue<int>Q;memset(Level,-1,sizeof(level)); Q.push (S); Level[s] =0; while(! Q.empty ()) {intp = Q.front (); Q.pop ();if(p = = T)return true;//Make sure T is the largest for(inti =0; I <= T; ++i) {if(Level[i] = =-1&& G[p][i] >0) {Level[i] = Level[p] +1; Q.push (i); } } }return false;}intDFS (intSintTintFlow) {//printf ("Vis%d----------------\ n", S); if(s = = t)returnFlowintsum =0, TMP; for(inti =0; I <= t; ++i) {if(Level[i] = = Level[s] +1&& G[s][i] >0) {tmp = DFS (i, T, Min (flow-sum, g[s][i])); sum + = tmp;//printf ("(%d,%d) \ n", S, sum);G[s][i]-= tmp; G[i][s] + = tmp; } }returnsum;}intDinic (intSintT) {intsum =0; while(BFS (S, T)) {//puts ("---------------"); //for (int i = 0; I <= T; ++i) { //printf ("%d", Level[i]); //} //puts ("\ n----------------");Sum + = DFS (S, T, INF);//printf ("sum =%d\n", sum); //puts ("-----------------------"); //getchar ();}returnsum;}intMain () {intGt[max <<1][max <<1]; while(~scanf("%d%d", &p, &n)) {memsetG0,sizeof(G)); for(inti =1; I <= N; ++i) {//Split point scanf("%d", &g[i][i + N]); for(intj =1; J <= P; ++J) {scanf("%d", &in[i][j]); } for(intj =1; J <= P; ++J) {scanf("%d", &out[i][j]); } }intS =0, T = N <<1|1; for(inti =1; I <= N; ++i) {BOOLFlag1 =true, Flag2 =true;; for(intj =1; J <= P; ++J) {if(In[i][j] = =1) {Flag1 =false; }if(Out[i][j]! =1) {Flag2 =false; } }if(FLAG1) G[s][i] = INF;//source if(FLAG2) G[i + n][t] = INF;//slink for(intj =1; J <= N; ++J) {if(i = = j)Continue; Flag1 =true; for(intK =1; K <= P; ++K) {if(Out[i][k] + in[j][k] = =1) {Flag1 =false; Break; } }if(FLAG1) G[i + n][j] = INF; } }memcpy(GT, G,sizeof(G));intAns = dinic (S, T); vector<o_o>V O_o U_u;if(ANS) { for(inti =1; i < T; ++i) { for(intj =1; J < T; ++J) {if(G[i][j] < gt[i][j]) {u_u.u = i > N? i-n: i; U_U.V = j > N? J-n: J; U_U.C = Gt[i][j]-g[i][j];if(u_u.u = = U_U.V)Continue; V.push_back (U_u); } } }printf("%d%d\n", ans, v.size ()); for( vector<o_o>:: Iterator it = V.begin (); It! = V.end (); ++it) {printf("%d%d%d\n", It->u, It->v, it->c); } }Else{puts("0 0"); } }return 0;}
Network flow-Maximum flow: two [poj1459&poj3436]