-
Notifications
You must be signed in to change notification settings - Fork 2
/
ac_mincostflow.go2
169 lines (152 loc) · 4.02 KB
/
ac_mincostflow.go2
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
// vim:set ft=go:
// This is a port of the AC(AtCoder) Library [1] to Go.
// These logics are based on the ac-library.
// The ac-library is distributed under CC0.
//
// [1] https://github.com/atcoder/ac-library
// snip ------------------------------------------------------------------------
type MinCostFlowEdge[Cap OrderedNumeric, Cost SignedNumeric] struct {
From, To int
Capa, Flow Cap
Cost Cost
}
type minCostFloeEdge[Cap OrderedNumeric, Cost SignedNumeric] struct {
to, rev int
capa Cap
cost Cost
}
type MinCostFlow[Cap OrderedNumeric, Cost SignedNumeric] struct {
n int
pos [][2]int
g [][]minCostFloeEdge[Cap, Cost]
}
func NewMinCostFlow[Cap OrderedNumeric, Cost SignedNumeric](n int) *MinCostFlow[Cap, Cost] {
return &MinCostFlow[Cap, Cost]{
n: n,
g: make([][]minCostFloeEdge[Cap, Cost], n),
}
}
func (mcf *MinCostFlow[Cap, Cost]) AddEdge(from, to int, capa Cap, cost Cost) int {
assert(0 <= from && from < mcf.n)
assert(0 <= to && to < mcf.n)
m := len(mcf.pos)
mcf.pos = append(mcf.pos, [2]int{from, len(mcf.g[from])})
mcf.g[from] = append(mcf.g[from], minCostFloeEdge[Cap, Cost]{to, len(mcf.g[to]), capa, cost})
mcf.g[to] = append(mcf.g[to], minCostFloeEdge[Cap, Cost]{from, len(mcf.g[from]) - 1, 0, -cost})
return m
}
func (mcf *MinCostFlow[Cap, Cost]) GetEdge(i int) MinCostFlowEdge[Cap, Cost] {
m := len(mcf.pos)
assert(0 <= i && i < m)
e := mcf.g[mcf.pos[i][0]][mcf.pos[i][1]]
re := mcf.g[e.to][e.rev]
return MinCostFlowEdge[Cap, Cost]{mcf.pos[i][0], e.to, e.capa + re.capa, re.capa, e.cost}
}
func (mcf *MinCostFlow[Cap, Cost]) Edges() []MinCostFlowEdge[Cap, Cost] {
m := len(mcf.pos)
res := make([]MinCostFlowEdge[Cap, Cost], m)
for i := range res {
res[i] = mcf.GetEdge(i)
}
return res
}
func (mcf *MinCostFlow[Cap, Cost]) Flow(s, t int) (Cap, Cost) {
sl := mcf.Slope(s, t)
n := len(sl) - 1
return sl[n].First, sl[n].Second
}
func (mcf *MinCostFlow[Cap, Cost]) FlowWithLimit(s, t int, flowLimit Cap) (Cap, Cost) {
sl := mcf.SlopeWithLimit(s, t, flowLimit)
n := len(sl) - 1
return sl[n].First, sl[n].Second
}
func (mcf *MinCostFlow[Cap, Cost]) Slope(s, t int) []Pair[Cap, Cost] {
return mcf.SlopeWithLimit(s, t, numeric_limits_max[Cap]())
}
type minCostFlowQueueElm[Cost OrderedNumeric] struct {
key Cost
to int
}
func (mcf *MinCostFlow[Cap, Cost]) SlopeWithLimit(s, t int, flowLimit Cap) []Pair[Cap, Cost] {
assert(0 <= s && s < mcf.n)
assert(0 <= t && t < mcf.n)
assert(s != t)
dual := make([]Cost, mcf.n)
dist := make([]Cost, mcf.n)
pv := make([]int, mcf.n)
pe := make([]int, mcf.n)
vis := make([]bool, mcf.n)
dualRef := func() bool {
for i := range dual {
dist[i] = numeric_limits_max[Cost]()
pv[i] = -1
pe[i] = -1
vis[i] = false
}
que := NewPriorityQueue[Pair[Cost, int]](func(a, b Pair[Cost, int]) bool { return a.First < b.First })
dist[s] = 0
que.Push(MakePair(Cost(0), s))
for !que.Empty() {
v := que.Pop().Second
if vis[v] {
continue
}
vis[v] = true
if v == t {
break
}
for i := 0; i < len(mcf.g[v]); i++ {
e := mcf.g[v][i]
if vis[e.to] || e.capa == 0 {
continue
}
cost := e.cost - dual[e.to] + dual[v]
if dist[e.to]-dist[v] > cost {
dist[e.to] = dist[v] + cost
pv[e.to] = v
pe[e.to] = i
que.Push(MakePair(dist[e.to], e.to))
}
}
}
if !vis[t] {
return false
}
for v := 0; v < mcf.n; v++ {
if !vis[v] {
continue
}
dual[v] -= dist[t] - dist[v]
}
return true
}
var flow Cap
var cost Cost
var prevCost Cost = -1
result := make([]Pair[Cap, Cost], 0)
result = append(result, MakePair(flow, cost))
for flow < flowLimit {
if !dualRef() {
break
}
c := flowLimit - flow
for v := t; v != s; v = pv[v] {
c = min(c, mcf.g[pv[v]][pe[v]].capa)
}
for v := t; v != s; v = pv[v] {
e := &mcf.g[pv[v]][pe[v]]
e.capa -= c
mcf.g[v][e.rev].capa += c
}
d := -dual[s]
flow += c
cost += Cost(c) * d
if prevCost == d {
result = result[:len(result)-1]
}
result = append(result, MakePair(flow, cost))
prevCost = cost
}
return result
}