-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdinic.cpp
More file actions
145 lines (141 loc) · 2.85 KB
/
dinic.cpp
File metadata and controls
145 lines (141 loc) · 2.85 KB
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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long int
#define endl '\n'
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 705
#define mod 1000000007
#define INF 1e9
struct edge
{
int to, from, flow, capacity, id;
};
struct dinic
{
int n, src, sink;
vector<vector<edge>> adj;
vector<int> level;
vector<int> ptr;
dinic(int sz)
{
n = sz;
adj.resize(n);
level.resize(n);
ptr.resize(n);
}
void add_edge(int a, int b, int c, int id)
{
adj[a].pb({b, (int)adj[b].size(), c, c, id});
adj[b].pb({a, (int)adj[a].size() - 1, 0, 0, id});
}
bool bfs()
{
level.assign(n, -1);
level[src] = 0;
queue<int> q;
q.push(src);
while (!q.empty())
{
int u = q.front();
q.pop();
for (auto at : adj[u])
{
if (at.flow && level[at.to] == -1)
{
q.push(at.to);
level[at.to] = level[u] + 1;
}
}
}
return level[sink] != -1;
}
int dfs(int u, int flow)
{
if (u == sink || flow == 0)
return flow;
for (int &p = ptr[u]; p < adj[u].size(); p++)
{
edge &at = adj[u][p];
if (at.flow && level[u] == level[at.to] - 1)
{
int kappa = dfs(at.to, min(flow, at.flow));
at.flow -= kappa;
adj[at.to][at.from].flow += kappa;
if (kappa != 0)
return kappa;
}
}
return 0;
}
int run()
{
int max_flow = 0;
while (bfs())
{
ptr.assign(n, 0);
while (1)
{
int flow = dfs(src, INF);
if (flow == 0)
break;
max_flow += flow;
}
}
return max_flow;
}
vector<pii> cut_edges() // arestas do corte minimo
{
bfs();
vector<pii> ans;
for (int i = 0; i < n; i++)
{
for (auto const &j : adj[i])
{
if (level[i] != -1 && level[j.to] == -1 && j.capacity > 0)
ans.pb({j.capacity, {i, j.to}});
}
}
return ans;
}
vector<int> flow_edges(int n, int m) // fluxo em cada aresta, na ordem da entrada
{
vector<int> ans(m);
for (int i = 0; i < n; i++)
{
for (auto const &j : adj[i])
if (!j.capacity)
ans[j.id] = j.flow;
}
return ans;
}
};
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
dinic d(n);
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
a--, b--;
d.add_edge(a, b, c, i);
}
d.src = 0, d.sink = n - 1;
cout << d.run() << endl;
vector<int> ans = d.flow_edges(n, m);
for (auto const &i : ans)
cout << i << endl;
return 0;
}