Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
competitive
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bruno Freitas Tissei
competitive
Commits
39ab5757
Commit
39ab5757
authored
6 years ago
by
Bruno Freitas Tissei
Browse files
Options
Downloads
Patches
Plain Diff
Add dinic's algorithm
Signed-off-by:
Bruno Freitas Tissei
<
bft15@inf.ufpr.br
>
parent
b0a4a96b
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
algorithms/graph/dinic.cpp
+103
-0
103 additions, 0 deletions
algorithms/graph/dinic.cpp
with
103 additions
and
0 deletions
algorithms/graph/dinic.cpp
0 → 100644
+
103
−
0
View file @
39ab5757
/**
* Dinic's
*
* Complexity (Time): O(E*V^2)
* Complexity (Space): O(V + E)
*/
// Edge struct to be used in adjacency list similar to vector<ii> graph[MAX],
// but storing more information than ii
typedef
struct
edge
{
int
u
;
int
flow
,
cap
;
// Id of the reverse edge on graph[u]
int
rev
;
edge
(
int
u
,
int
flow
,
int
cap
,
int
rev
)
:
u
(
u
),
flow
(
flow
),
cap
(
cap
),
rev
(
rev
)
{}
}
egde
;
int
depth
[
MAX
];
int
start
[
MAX
];
vector
<
edge
>
graph
[
MAX
];
// Adds edge between s and t with capacity c
void
add_edge
(
int
s
,
int
t
,
int
c
)
{
edge
forward
(
t
,
0
,
c
,
graph
[
t
].
sz
);
edge
backward
(
s
,
0
,
0
,
graph
[
s
].
sz
);
graph
[
s
].
pb
(
forward
);
graph
[
t
].
pb
(
backward
);
}
// Calculates depth of each vertex from source, considering only
// edges with remaining capacity
bool
bfs
(
int
s
,
int
t
)
{
queue
<
int
>
Q
;
Q
.
push
(
s
);
mset
(
depth
,
-
1
);
depth
[
s
]
=
0
;
while
(
!
Q
.
empty
())
{
int
v
=
Q
.
front
();
Q
.
pop
();
for
(
auto
i
:
graph
[
v
])
{
if
(
depth
[
i
.
u
]
==
-
1
&&
i
.
flow
<
i
.
cap
)
{
depth
[
i
.
u
]
=
depth
[
v
]
+
1
;
Q
.
push
(
i
.
u
);
}
}
}
return
depth
[
t
]
!=
-
1
;
}
// Finds bottleneck flow and add to the edges belonging to the paths found
int
dfs
(
int
s
,
int
t
,
int
flow
)
{
if
(
s
==
t
)
return
flow
;
// Start iteration from where it last stopped to avoid repetitions
for
(
;
start
[
s
]
<
graph
[
s
].
sz
;
++
start
[
s
])
{
edge
&
e
=
graph
[
s
][
start
[
s
]];
// If the next vertex is further from the source (and closer to the sink)
// and the edge is not at full capacity, then explore it
if
(
depth
[
e
.
u
]
==
depth
[
s
]
+
1
&&
e
.
flow
<
e
.
cap
)
{
int
min_flow
=
dfs
(
e
.
u
,
t
,
min
(
flow
,
e
.
cap
-
e
.
flow
));
if
(
min_flow
>
0
)
{
e
.
flow
+=
min_flow
;
graph
[
e
.
u
][
e
.
rev
].
flow
-=
min_flow
;
return
min_flow
;
}
}
}
return
0
;
}
// Returns maximum flow between s and t
int
dinic
(
int
s
,
int
t
)
{
int
ans
=
0
;
// Run bfs to set depth array (depth of each vertex from source)
while
(
bfs
(
s
,
t
))
{
mset
(
start
,
0
);
// Find every available path from the current depth information,
// set the flow of the edges and add the pushed flow to the answer
while
(
int
flow
=
dfs
(
s
,
t
,
inf
))
ans
+=
flow
;
}
return
ans
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment