-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathzkwflow.cpp
50 lines (50 loc) · 1.35 KB
/
zkwflow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
struct zkwflow{
static const int maxN=10000;
struct Edge{ int v,f,re; ll w;};
int n,s,t,ptr[maxN]; bool vis[maxN]; ll dis[maxN];
vector<Edge> E[maxN];
void init(int _n,int _s,int _t){
n=_n,s=_s,t=_t;
for(int i=0;i<n;i++) E[i].clear();
}
void addEdge(int u,int v,int f,ll w){
E[u].push_back({v,f,(int)E[v].size(),w});
E[v].push_back({u,0,(int)E[u].size()-1,-w});
}
bool SPFA(){
fill_n(dis,n,LLONG_MAX); fill_n(vis,n,false);
queue<int> q; q.push(s); dis[s]=0;
while (!q.empty()){
int u=q.front(); q.pop(); vis[u]=false;
for(auto &it:E[u]){
if(it.f>0&&dis[it.v]>dis[u]+it.w){
dis[it.v]=dis[u]+it.w;
if(!vis[it.v]){
vis[it.v]=true; q.push(it.v);
} } } }
return dis[t]!=LLONG_MAX;
}
int DFS(int u,int nf){
if(u==t) return nf;
int res=0; vis[u]=true;
for(int &i=ptr[u];i<(int)E[u].size();i++){
auto &it=E[u][i];
if(it.f>0&&dis[it.v]==dis[u]+it.w&&!vis[it.v]){
int tf=DFS(it.v,min(nf,it.f));
res+=tf,nf-=tf,it.f-=tf;
E[it.v][it.re].f+=tf;
if(nf==0){ vis[u]=false; break; }
}
}
return res;
}
pair<int,ll> flow(){
int flow=0; ll cost=0;
while (SPFA()){
fill_n(ptr,n,0);
int f=DFS(s,INT_MAX);
flow+=f; cost+=dis[t]*f;
}
return{ flow,cost };
} // reset: do nothing
} flow;