Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3929d0e

Browse files
2 parents 4637f03 + 6bf806d commit 3929d0e

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

‎Library/Grafos/Dinic.cpp

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22
using namespace std;
33
#define ll long long
44

5-
struct Aresta {
5+
struct Edge {
66
int u, v; ll cap;
7-
Aresta(int u, int v, ll cap) : u(u), v(v), cap(cap) {}
7+
Edge(int u, int v, ll cap) : u(u), v(v), cap(cap) {}
88
};
99

1010
struct Dinic {
1111

12-
int n, source, sink;
12+
int n, src, sink;
1313
vector<vector<int>> adj;
14-
vector<Aresta> arestas;
15-
vector<int> level, ptr; //pointer para a próxima aresta não saturada de cada vértice
14+
vector<Edge> edges;
15+
vector<int> lvl, ptr; //pointer para a próxima Edge não saturada de cada vértice
1616

17-
Dinic(int n, int source, int sink) : n(n), source(source), sink(sink) { adj.resize(n); }
17+
Dinic(int n, int src, int sink) : n(n), src(src), sink(sink) { adj.resize(n); }
1818

19-
void addAresta(int u, int v, ll cap)
19+
void addEdge(int u, int v, ll cap)
2020
{
21-
adj[u].push_back(arestas.size());
22-
arestas.emplace_back(u, v, cap);
21+
adj[u].push_back(edges.size());
22+
edges.emplace_back(u, v, cap);
2323

24-
adj[v].push_back(arestas.size());
25-
arestas.emplace_back(v, u, 0);
24+
adj[v].push_back(edges.size());
25+
edges.emplace_back(v, u, 0);
2626
}
2727

2828
ll dfs(int u, ll flow = 1e9){
@@ -31,15 +31,15 @@ struct Dinic {
3131

3232
for(int &i = ptr[u]; i < adj[u].size(); i++)
3333
{
34-
int atual = adj[u][i];
35-
int v = arestas[atual].v;
34+
int at = adj[u][i];
35+
int v = edges[at].v;
3636

37-
if(level[u] + 1 != level[v]) continue;
37+
if(lvl[u] + 1 != lvl[v]) continue;
3838

39-
if(ll got = dfs(v, min(flow, arestas[atual].cap)) )
39+
if(ll got = dfs(v, min(flow, edges[at].cap)) )
4040
{
41-
arestas[atual].cap -= got;
42-
arestas[atual^1].cap += got;
41+
edges[at].cap -= got;
42+
edges[at^1].cap += got;
4343
return got;
4444
}
4545
}
@@ -48,39 +48,39 @@ struct Dinic {
4848
}
4949

5050
bool bfs(){
51-
level = vector<int> (n, n);
52-
level[source] = 0;
51+
lvl = vector<int> (n, n);
52+
lvl[src] = 0;
5353

54-
queue<int> fila;
55-
fila.push(source);
54+
queue<int> q;
55+
q.push(src);
5656

57-
while(!fila.empty())
57+
while(!q.empty())
5858
{
59-
int u = fila.front();
60-
fila.pop();
59+
int u = q.front();
60+
q.pop();
6161

6262
for(auto i : adj[u]){
63-
int v = arestas[i].v;
63+
int v = edges[i].v;
6464

65-
if(arestas[i].cap == 0 || level[v] <= level[u] + 1 ) continue;
65+
if(edges[i].cap == 0 || lvl[v] <= lvl[u] + 1 ) continue;
6666

67-
level[v] = level[u] + 1;
68-
fila.push(v);
67+
lvl[v] = lvl[u] + 1;
68+
q.push(v);
6969
}
7070
}
7171

72-
return level[sink] < n;
72+
return lvl[sink] < n;
7373
}
7474

75-
bool inCut(int u){ return level[u] < n; }
75+
bool inCut(int u){ return lvl[u] < n; }
7676

7777
ll maxFlow(){
7878
ll ans = 0;
7979

8080
while( bfs() ){
8181
ptr = vector<int> (n+1, 0);
8282

83-
while(ll got = dfs(source)) ans += got;
83+
while(ll got = dfs(src)) ans += got;
8484
}
8585

8686
return ans;
@@ -94,27 +94,27 @@ IMPORTANTE! O algoritmo está 0-indexado
9494
9595
**Complexity:**
9696
O( V^2 * E ) -> caso geral
97-
O( sqrt(V) * E ) -> grafos com cap = 1 para toda aresta // matching bipartido
97+
O( sqrt(V) * E ) -> grafos com cap = 1 para toda Edge // matching bipartido
9898
9999
* Informações:
100-
Crie o Dinic: Dinic dinic(n, source, sink);
101-
Adicione as Arestas: dinic.addAresta(u, v, capacity);
100+
Crie o Dinic: Dinic dinic(n, src, sink);
101+
Adicione as edges: dinic.addEdge(u, v, capacity);
102102
Para calcular o Fluxo Máximo: dinic.maxFlow()
103103
Para saber se um vértice U está no Corte Mínimo: dinic.inCut(u)
104104
105105
* Sobre o Código:
106-
vector<Aresta> arestas; -> Guarda todas as arestas do grafo e do grafo residual
107-
vector<vector<int>> adj; -> Guarda em adj[u] os índices de todas as arestas saindo de u
108-
vector<int> ptr; -> Pointer para a próxima aresta ainda não visitada de cada vértice
109-
vector<int> level; -> Distância em vértices a partir do Source. Se igual a N o vértice não foi visitado.
106+
vector<Edge> edges; -> Guarda todas as edges do grafo e do grafo residual
107+
vector<vector<int>> adj; -> Guarda em adj[u] os índices de todas as edges saindo de u
108+
vector<int> ptr; -> Pointer para a próxima Edge ainda não visitada de cada vértice
109+
vector<int> lvl; -> Distância em vértices a partir do Source. Se igual a N o vértice não foi visitado.
110110
A BFS retorna se Sink é alcançavel de Source. Se não é porque foi atingido o Fluxo Máximo
111111
A DFS retorna um possível aumento do Fluxo
112112
*****************************LATEX_DESC_END*/
113113
/**************************************LATEX_IGNORED_BEGIN
114114
* Use Cases of Flow
115115
116116
+ Minimum cut: the minimum cut is equal to maximum flow.
117-
i.e. to split the graph in two parts, one on the source side and another on sink side.
117+
i.e. to split the graph in two parts, one on the src side and another on sink side.
118118
The capacity of each edge is it weight.
119119
120120
+ Edge-disjoint paths: maximum number of edge-disjoint paths equals maximum flow of the
@@ -124,8 +124,8 @@ O( sqrt(V) * E ) -> grafos com cap = 1 para toda aresta // matching bipartido
124124
path, so limit the flow through a node dividing each node in two. One with incoming edges,
125125
other with outgoing edges and a new edge from the first to the second with capacity 1.
126126
127-
+ Maximum matching (bipartite): maximum matching is equal to maximum flow. Add a source and
128-
a sink, edges from the source to every node at one partition and from each node of the
127+
+ Maximum matching (bipartite): maximum matching is equal to maximum flow. Add a src and
128+
a sink, edges from the src to every node at one partition and from each node of the
129129
other partition to the sink.
130130
131131
+ Minimum node cover (bipartite): minimum set of nodes such each edge has at least one
@@ -153,4 +153,4 @@ O( sqrt(V) * E ) -> grafos com cap = 1 para toda aresta // matching bipartido
153153
maximum sum. a closure is a set of nodes such that there is no edge from a node inside
154154
the set to a node outside. Is a general case of project selection. Original edges with cap INF.
155155
Add edges from Source to nodes with W > 0; and from nodes with W < 0 to Sink (cap |W|).
156-
LATEX_IGNORED_END***************************************/
156+
LATEX_IGNORED_END***************************************/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /