Test instructions: There are n locations, NP generators. NC user, M wire. Give the generator. User. The current limit of the wire, the maximum network current is obtained.
This is a network stream with nodes. In fact, there is no difference, just to add a point in front and back, where I added 0 and n+1 two nodes, and the generator node current limit can be
The current limit of the 0-> node can be converted to the current limit of the node->n+1, and then the maximum network flow template can be applied. Here I wrote the EDMONDS_KARP algorithm.
The dinic algorithm is later passed ...
。
。
。。。。。。。。。。。。。
。。
The AC code is as follows:
Edmonds_karp algorithm
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #define MIN (b) (a <B?A:B) #define INF 100000000using namespace Std;int n,np,nc,m;int map[105][105],jd[105],p[105],f[105];int Edmonds_ Karp (int s,int e) {int i,j; Queue <int > Q; int A, B; for (i=0;i<=n+1;i++) p[i]=-1; F[0]=inf; Q.push (s); while (!q.empty ()) {B=q.front (); Q.pop (); if (b==e) break; for (i=0;i<=n+1;i++) {if (p[i]==-1&&map[b][i]>0) {f[i]=min (map[b ][I],F[B]); P[i]=b; Q.push (i); }}} if (P[e]==-1) return-1; else return f[e];} int maxflow (int s,int e) {int i,j; int a,ans=0; A=edmonds_karp (s,e); while (a!=-1) {int k=e,min=inf; while (k!=s) {map[p[k]][k]-=a; Map[k][p[k]]+=a; K=P[K]; } ans+=a; A=edmonDs_karp (s,e); } return ans; int main () {int i,j; int a,b,c,d,f; Char z,x,y; while (cin>>n>>np>>nc>>m) {memset (map,0,sizeof map); for (i=1;i<=m;i++) {cin>>z>>a>>x>>b>>y>>c; Map[a+1][b+1]+=c; } for (i=1;i<=np;i++) {cin>>z>>d>>x>>f; Map[0][d+1]+=f; } for (i=1;i<=nc;i++) {cin>>z>>d>>x>>f; Map[d+1][n+1]+=f; } cout<<maxflow (0,n+1) <<endl; } return 0;}
Dinic algorithm
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #define INF 100000000# Define min (A, b) (A<B?A:B) using namespace Std;int n,np,nc,m;int map[105][105],cs[105];int BFS () {int i,j; Queue <int > Q; int A, B; memset (cs,-1,sizeof CS); cs[0]=0; Q.push (0); while (!q.empty ()) {B=q.front (); Q.pop (); for (i=0;i<=n+1;i++) {if (cs[i]==-1&&map[b][i]>0) {cs[i]=cs[b]+1 ; Q.push (i); }}} if (Cs[n+1]==-1) return 0; else return 1;} int dfs (int s,int MF) {int i,j; int A, B; if (s==n+1) return MF; for (i=0;i<=n+1;i++) {if (cs[i]==cs[s]+1&&map[s][i]>0&& (A=dfs (I,min (mf,map[s][i)))) {map[s][i]-=a; Map[i][s]+=a; return A; }} return 0;} int main () {int i,j; int a,b,c,d,f; while (Cin>>n>>np>> nc>>m) {memset (map,0,sizeof map); for (i=1;i<=m;i++) {scanf ("(%d,%d)%d", &a,&b,&c); Map[a+1][b+1]+=c; } for (i=1;i<=np;i++) {scanf ("(%d)%d", &d,&f); Map[0][d+1]+=f; } for (i=1;i<=nc;i++) {scanf ("(%d)%d", &d,&f); Map[d+1][n+1]+=f; } int ans=0,sum; while (BFS ()) {while (Sum=dfs (0,inf)) ans+=sum; } cout<<ans<<endl; } return 0;}
POJ 1459 Power Network