网络流最大流增广路DINIC算法
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e4+5;
const int maxm=1e5+5;
int n,m,S,T,ne,head[maxn],cur[maxn],depth[maxn],maxflow;
queue<int> q;
struct edge
{
int next,to,cost;
}e[maxm*2];
void init()
{
memset(head,-1,sizeof(head));
ne=0;
maxflow=0;
}
void add(int u,int v,int w)
{
e[ne].to=v;
e[ne].cost=w;
e[ne].next=head[u];
head[u]=ne++;
e[ne].to=u;
e[ne].cost=0;
e[ne].next=head[v];
head[v]=ne++;
}
int bfs(int s,int t)
{
while(!q.empty()) q.pop();
memset(depth,-1,sizeof(depth));
for(int i=1;i<=n;i++) cur[i]=head[i];
depth[s]=0;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
if(depth[e[i].to]==-1&&e[i].cost)
{
depth[e[i].to]=depth[u]+1;
q.push(e[i].to);
}
}
}
if(depth[t]==-1) return -1;
else return 1;
}
int dfs(int s,int t,int limit)
{
if(!limit||s==t) return limit;
int f,flow=0;
for(int i=cur[s];i!=-1;i=e[i].next)
{
cur[s]=i;
if(depth[e[i].to]==depth[s]+1&&(f=dfs(e[i].to,t,min(limit,e[i].cost))))
{
flow+=f;
limit-=f;
e[i].cost-=f;
e[i^1].cost+=f;
if(!limit) break;
}
}
return flow;
}
void dinic(int s,int t)
{
while(bfs(s,t)!=-1)
{
maxflow+=dfs(s,t,INF);
}
}
int main()
{
while(~scanf("%d %d %d %d",&n,&m,&S,&T))
{
init();
for(int i=0;i<m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
dinic(S,T);
printf("%d\n",maxflow);
}
}
网络流最大流增广路ISAP算法
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e4+5;
const int maxm=1e5+5;
int head[maxn],cur[maxn],dep[maxn],ne,pre[maxn],num[maxn],n,m,S,T,maxflow;
queue<int> q;
struct edge
{
int next,to,cost;
}e[maxm*2];
void add(int u,int v,int w)
{
e[ne].to=v;
e[ne].cost=w;
e[ne].next=head[u];
head[u]=ne++;
e[ne].to=u;
e[ne].cost=0;
e[ne].next=head[v];
head[v]=ne++;
}
void bfs(int t)
{
while(!q.empty()) q.pop();
for(int i=1;i<=n;i++) cur[i]=head[i];
for(int i=1;i<=n;i++) dep[i]=n;
dep[t]=0;
q.push(t);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
if(dep[e[i].to]==n&&e[i^1].cost)
{
dep[e[i].to]=dep[u]+1;
q.push(e[i].to);
}
}
}
}
int addflow(int s,int t)
{
int ans=INF,u=t;
while(u!=s)
{
ans=min(ans,e[pre[u]].cost);
u=e[pre[u]^1].to;
}
u=t;
while(u!=s)
{
e[pre[u]].cost-=ans;
e[pre[u]^1].cost+=ans;
u=e[pre[u]^1].to;
}
return ans;
}
void ISAP(int s,int t)
{
int u=s;
bfs(t);
for(int i=1;i<=n;i++) num[dep[i]]++;
while(dep[s]<n)
{
if(u==t)
{
maxflow+=addflow(s,t);
u=s;
}
int flag=0;
for(int &i=cur[u];i!=-1;i=e[i].next)
{
if(dep[u]==dep[e[i].to]+1&&e[i].cost)
{
flag=1;
pre[e[i].to]=i;
u=e[i].to;
break;
}
}
if(!flag)
{
int minn=n-1;
for(int i=head[u];i!=-1;i=e[i].next)
{
if(e[i].cost)
{
minn=min(minn,dep[e[i].to]);
}
}
if((--num[dep[u]])==0) break;
dep[u]=minn+1;
num[dep[u]]++;
cur[u]=head[u];
if(u!=s) u=e[pre[u]^1].to;
}
}
}
void init()
{
memset(head,-1,sizeof(head));
ne=0;
memset(num,0,sizeof(num));
maxflow=0;
}
int main()
{
while(~scanf("%d%d%d%d",&n,&m,&S,&T))
{
init();
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
ISAP(S,T);
printf("%d\n",maxflow);
}
}
网络流最小费用最大流算法
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=10000+5;
const int maxm=100000+5;
int head[maxn],dis[maxn],flow[maxn],pre[maxn],last[maxn],vis[maxn],ne,n,m,S,T,ans,maxflow,mincost;
void init()
{
ans=0;
ne=0;
memset(head,-1,sizeof(head));
}
struct edge{int v,w,nxt,cost;}G[maxm<<1];
void add(int u,int v,int w,int cost)
{
G[ne]=(edge){v,w,head[u],cost};
head[u]=ne++;
}
int spfa()
{
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(flow,0x3f,sizeof(flow));
queue<int> q;
q.push(S);
dis[S]=0;
pre[T]=-1;
vis[S]=1;
while(!q.empty())
{
int u=q.front();
vis[u]=0;
q.pop();
for(int i=head[u];i!=-1;i=G[i].nxt)
{
int v=G[i].v;
if(dis[v]>dis[u]+G[i].cost&&G[i].w>0)
{
dis[v]=dis[u]+G[i].cost;
pre[v]=u;
last[v]=i;
flow[v]=min(flow[u],G[i].w);
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
return pre[T]!=-1;
}
int MCMF()
{
maxflow=0,mincost=0;
while(spfa())
{
int u=T;
maxflow+=flow[T];
mincost+=flow[T]*dis[T];
while(u!=S)
{
G[last[u]].w-=flow[T];
G[last[u]^1].w+=flow[T];
u=pre[u];
}
}
}
int main()
{
init();
scanf("%d%d%d%d",&n,&m,&S,&T);
for(int i=0;i<m;i++)
{
int ui,vi,wi,coi;
scanf("%d%d%d%d",&ui,&vi,&wi,&coi);
add(ui,vi,wi,coi);
add(vi,ui,0,-coi);
}
MCMF();
printf("%d %d\n",maxflow,mincost);
}