-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph_implentations.cpp
More file actions
160 lines (141 loc) · 3.46 KB
/
graph_implentations.cpp
File metadata and controls
160 lines (141 loc) · 3.46 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//inspired by cses handbook and cp-algorithms.com
void dfs(int v) {
visited[v] = true;
for (auto u: adj[v]) {
if (!visited[u]) dfs(u);
}
}
void bfs(int source){
queue<int> q; q.push(source);
dis[source] = 0;
visited[source] = true;
while(!q.empty()){
int a = q.front(); q.pop();
for(auto b: adj[a]){
if(visited[b]) continue;
visited[b] = true;
dis[b] = dis[a] + 1;
q.push(b);
}
}
}
void bellman(int source){
dis[source] = 0;
for(int i=0;i<n-1;i++){
for(auto e: edges){
int a,b,w;
tie(a,b,w) = e;
dis[b] = min(dis[a] + w, dis[b]);
}
}
}
void floyd(int source){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i == j) dis[i][j] = 0;
if(adj[i][j]) dis[i][j] = adj[i][j];
else dis[i][j] = inf;
}
}
for(int k=1;k<=n;i++){
for(int a=1;a<=n;a++){
for(int b=1;b<=n;b++){
dis[a][b] = min(dis[a][b], dis[a][k] + dis[k][b]);
}
}
}
}
void dijk(int source){
priority_queue<int> pq;
pq.push(mp(0, source));
dis[source] = 0;
while(!pq.empty()){
int a = pq.front().second; pq.pop();
if(processed[a]) continue;
processed[a] = true;
for(auto u: adj[a]){
int b = u.first, w = u.second;
if(dis[b] > dis[a] + w){
dis[b] = dis[a] + w;
pq.push(mp(-dis[b], b));
}
}
}
}
struct FlowNetwork {
int n;
vector<vector<pii>> adj;
vector<int> capacity;
vector<pii> parent;
vector<int> dis;
vector<int> used;
int edge_num = 0;
int s;
int t;
FlowNetwork(int lim, int source, int sink) {
n = lim;
adj = vector<vector<pii>>(n);
s = source;
t = sink;
parent = vector<pii>(n);
dis = vector<int>(n);
used = vector<int>(n);
}
void add_edge(int i, int j) {
adj[i].pb({j, edge_num});
capacity.pb(1);
edge_num++;
adj[j].pb({i, edge_num});
capacity.pb(0);
edge_num++;
}
bool dfs(int v) {
if (v == t) return true;
int lim = adj[v].size();
for (int i=used[v]; i < lim; i++) {
used[v]++;
int u = adj[v][i].first;
int edge_u = adj[v][i].second;
if (capacity[edge_u] <= 0) continue;
if (dis[u] != dis[v] + 1) continue;
if (!dfs(u)) continue;
capacity[edge_u]--;
int back_edge = edge_u;
if (back_edge % 2 == 0) back_edge++;
else back_edge--;
capacity[back_edge]++;
return true;
}
return false;
}
bool bfs () {
queue<int> q;
// doubles as visited array
// vector<int> dis(n, -1);
for (int i=0;i<n;i++) dis[i] = -1;
dis[s] = 0;
q.push(s);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto [u, edge_u]: adj[v]) {
if (dis[u] != -1 or capacity[edge_u] <= 0) continue;
dis[u] = dis[v] + 1;
q.push(u);
parent[u] = {v, edge_u};
}
}
if (dis[t] == -1) return false;
return true;
}
int find_flow () {
int flow = 0;
while (true) {
for (int i=0;i<n;i++) parent[i] = {-1, -1};
for (int i=0;i<n;i++) used[i] = 0;
if (!bfs()) break;
while (dfs(s)) flow++;
}
return flow;
}
};