Test instructions
2 1 1 2 (0,1) 20 (1,0) 10 (0) 15 (1) 20
2 1 1 2 represents a total of 2 nodes, the production of energy points 1, the point of energy consumption 1, the transmission of Energy Channel 2;
(0,1) 20 (1,0) 10 represents (starting point, end point) the maximum transmitted energy
(0) The maximum energy generated by 15 (the point of generating energy)
(1) 20 (point of consumption energy) consumption of the maximum energy
Beginner network flow, I want to start from the foundation; just write it again with the EK algorithm.
This problem seems difficult, but in fact, just add a source point and meeting point, so that all the energy points point to the source point, so that all the energy point of consumption point to the sink point;
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>UsingNamespace Std;#define N110#define INF0x3f3f3f3f#defineMin(AB) A<b? A: bint G[N][n], Pre[N];IntEK(int s,int E);boolBFS(int s,int E);IntMain(){int point, source, dest, Edge;While(scanf("%d%d%d%d",&point,&source,&dest,&edge)!=eof){int aB, Flow, in, inflow, out, outflow;Char Ch;Memset(G,0,sizeof(G));For(int I=0; I<edge; I++){scanf ("(%d,%d)%d", &a, &b, &flow); Cin>>ch>>a>>ch>>b>>ch>>flow; G[A+1][b+1]+=flow;}For(int I=0; I<source; I++){scanf ("(%d)%d", &out, &outflow); Cin>>ch>>out>>ch>>outflow; G[0][out+1]+=outflow;}For(int I=0; I<dest; I++){scanf ("(%d)%d", &in, &inflow); Cin>>ch>>in>>ch>>inflow; G[In+1][point+1]+=inflow;}int ans=EK(0, point+1);Printf("%d\ n", ans);}Return0;}IntEK(int s,int E){int Maxflow=0;While(BFS(sE)){int Minflow=inf;For(int I=e; I!=s; I=pre[I]) Minflow=Min(MinflowG[Pre[I]][i]);For(Int J=e; J!=s; J=pre[j]){G[Pre[j]][j]-=minflow; G[j][pre[j]]+=minflow;} Maxflow+=minflow;}Return Maxflow;}boolBFS(int s,int E){Memset(Pre,-1,sizeof(Pre)); Queue<Int>q; Q.Push(s);While(Q.Size()){int I=q.Front(); Q.Pop();If(I==e)ReturnTrue;For(Int J=0; J<=e++) { if (G[i][j< Span class= "Sh-symbol" >]&&pre[j]==- 1) {pre[j< Span class= "Sh-symbol" >]=i; Q. Push } } return false;}
POJ 1459 EK algorithm