Dinic Mincost Algorithm

Zhiyuan Xu Lv1
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
51
52
53
54
namespace Dinic{
int n,m,s,t,a,b,head[MAXN],d,edg[MAXN],c,tot=1,dist[MAXN],flow,pre[MAXN],val,lst[MAXN];
bool vis[MAXN];
struct Edge{
int u,v,nxt,d,f;
}edge[MAXM<<1];

inline void add(int u, int v, int f,int d){
edge[++tot].u = u;
edge[tot].v = v;
edge[tot].nxt = head[u];
edge[tot].f = f;
edge[tot].d = d;
head[u] = tot;
}
inline void addedge(int f,int t, int d, int k){
add(f,t,d,k);add(t,f,0,-k);
}
inline bool Dinic(){
queue<int> q;
q.push(s);
memset(dist,0x3f3f,sizeof(dist));
memset(edg,0x3f3f,sizeof(edg));
dist[s] = 0;
edg[s] = 1e9;
memset(pre,0,sizeof(pre));
while(!q.empty()){
int qf = q.front();q.pop();
vis[qf] = false;
for (int i=head[qf];i;i=edge[i].nxt){
int v= edge[i].v,d = edge[i].d;
if (dist[v]>dist[qf]+d && edge[i].f){
pre[v] = qf;
lst[v] = i;
edg[v] = min(edge[i].f,edg[qf]);
dist[v] = dist[qf]+d;
if (!vis[v]) vis[v] = true,q.push(v);
}
}
}
return pre[t];
}
inline void update(){
flow+=edg[t];
val+=edg[t]*dist[t];
int now = t;
while(now!=s){
edge[lst[now]].f-=edg[t];
edge[lst[now]^1].f+=edg[t];
now = pre[now];
}
}
inline void solve();
}
  • Title: Dinic Mincost Algorithm
  • Author: Zhiyuan Xu
  • Created at : 2024-07-06 00:07:45
  • Updated at : 2024-07-06 00:28:02
  • Link: https://redefine.ohevan.com/Template/DinicMC/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
Dinic Mincost Algorithm