POJ 1459 Power Network, poj1459
There are n data points, np generators, nc users, m wires, current limits for generators, users, and wires, and find the maximum network current.
This is a network stream with nodes. In fact, it is no different from the original. As long as a data point is added before and after, I have added 0 and n + 1 nodes here, the current limit on the generator node can be
Convert to the current limit of 0-> node. The current limit of the user node can be converted to the current limit of node-> n + 1, and then apply the maximum network flow template. Here I wrote, edmonds_karp algorithm.
Upload the Dinic algorithm ....................
The AC code is as follows:
Edmonds_karp Algorithm
#include <iostream>#include <cstdio>#include <queue>#include <cstring>#define min(a,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;}
Poj1459 sap code
# Include <stdio. h>
# Include <string>
# Include <queue>
# Include <conio. h>
Using namespace std;
# Define N 105
# Define inf 100000000
Int g [N] [N], d [N], num [N], pre [N];
Int n, flow;
Void init (int s, int t)
{
Queue <int> que;
Int k;
Que. push (t );
Memset (d, 1, sizeof (d ));
Memset (num, 0, sizeof (num ));
D [t] = 0;
Num [0] = 1;
While (que. empty () = 0)
{
K = que. front ();
Que. pop ();
For (int I = 0; I <n + 2; I ++)
{
If (d [I]> = n + 2 & g [I] [k]> 0)
{
D [I] = d [k] + 1;
Que. push (I );
Num [d [I] ++;
}
}
}
}
Int find_path (int I)
{
Int j;
For (j = 0; j <n + 2; j ++)
{
If (g [I] [j]> 0 & d [I] = d [j] + 1)
Return j;
}
Return-1;
}
Int relable (int I)
{
Int mm = inf;
Int j;
For (j = 0; j <n + 2; j ++)
{
If (g [I] [j]> 0)
Mm = mm <d [j] + 1? Mm: d [j] + 1;
}
Return mm = inf? N: mm;
}
Int max_flow (int s, int t)
{
Int flow = 0;
Int I, j, add, k;
I = s;
Memset (pre,-1, sizeof (pre ));
While (d [s] <n + 2)
{
J = find_path (I );
If (j> = 0)
{
Pre [j] = I;
I = j;
If (I = t)
{
Add = inf;
For (k = t; k! = S; k = pre [k])
Add = add <g [pre [k] [k]? Add: g [pre [k] [k];
For (k = t; k !...... Remaining full text>
Answer: Does poj 1459 use the Dinic algorithm or ISA algorithm in JAVA?
I use C ++... It cannot help you...
But I am using the EK algorithm... The most watery one... Not so advanced...