From 5d58d96a0d3cfc394bc7104ee8fe6fa8c00e8b16 Mon Sep 17 00:00:00 2001
From: Bruno Freitas Tissei <bft15@inf.ufpr.br>
Date: Sun, 24 Feb 2019 00:54:07 -0300
Subject: [PATCH] Add some Cadernaveis

Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br>
---
 algorithms/graph/edmonds_karp.cpp   |   2 +-
 algorithms/graph/ford_fulkerson.cpp |   2 +-
 contests/Cadernaveis/CAVALOS.cpp    | 101 +++++++++++++++++++++++++++
 contests/Cadernaveis/LA5220.cpp     | 102 ++++++++++++++++++++++++++++
 4 files changed, 205 insertions(+), 2 deletions(-)
 create mode 100644 contests/Cadernaveis/CAVALOS.cpp
 create mode 100644 contests/Cadernaveis/LA5220.cpp

diff --git a/algorithms/graph/edmonds_karp.cpp b/algorithms/graph/edmonds_karp.cpp
index b5c55bb..f9a2de6 100644
--- a/algorithms/graph/edmonds_karp.cpp
+++ b/algorithms/graph/edmonds_karp.cpp
@@ -50,7 +50,7 @@ int edmonds_karp(int s, int t) {
 
     // Get the minimum capacity among all edges of the chosen path
     for (int i = t; par[i] != -1; i = par[i])
-      flow = min(f, rg[par[i]][i]);
+      flow = min(flow, rg[par[i]][i]);
 
     // Update residual graph
     for (int i = t; par[i] != -1; i = par[i]) {
diff --git a/algorithms/graph/ford_fulkerson.cpp b/algorithms/graph/ford_fulkerson.cpp
index 4e87378..871b2ce 100644
--- a/algorithms/graph/ford_fulkerson.cpp
+++ b/algorithms/graph/ford_fulkerson.cpp
@@ -44,7 +44,7 @@ int ford_fulkerson(int s, int t) {
 
     // Get the minimum capacity among all edges of the chosen path
     for (int i = t; par[i] != -1; i = par[i])
-      flow = min(f, rg[par[i]][i]);
+      flow = min(flow, rg[par[i]][i]);
 
     // Update residual graph
     for (int i = t; par[i] != -1; i = par[i]) {
diff --git a/contests/Cadernaveis/CAVALOS.cpp b/contests/Cadernaveis/CAVALOS.cpp
new file mode 100644
index 0000000..839a029
--- /dev/null
+++ b/contests/Cadernaveis/CAVALOS.cpp
@@ -0,0 +1,101 @@
+#include <bits/stdc++.h>
+
+#define MAX 300
+#define EPS 1e-6
+#define MOD 1000000007
+#define inf 0x3f3f3f3f
+#define llinf 0x3f3f3f3f3f3f3f3f
+
+#define fi first
+#define se second
+#define sz size()
+#define pb push_back
+#define ende '\n'
+
+#define all(x) (x).begin(), (x).end()
+#define rall(x) (x).rbegin(), (x).rend()
+#define mset(x, y) memset(&x, (y), sizeof(x))
+
+using namespace std; 
+
+typedef long long ll;
+typedef pair<int,int> ii;
+
+int N;
+int par[MAX];
+int graph[MAX][MAX], rg[MAX][MAX];
+
+bool cont[MAX];
+
+bool path(int s, int t) {
+  cont[s] = true;
+  if (s == t)
+    return true;
+
+  for (int i = 0; i < N; ++i)
+    if (!cont[i] && rg[s][i]) {
+      par[i] = s;
+
+      if (path(i, t)) 
+        return true;
+    }
+
+  return false;
+}
+
+
+int ford_fulkerson(int s, int t) {
+  int ans = 0;
+  par[s] = -1;
+
+  mset(cont, 0);
+  memcpy(rg, graph, sizeof(graph));
+
+  while (path(s, t)) {
+    int flow = inf;
+
+    for (int i = t; par[i] != -1; i = par[i])
+      flow = min(flow, rg[par[i]][i]);
+
+    for (int i = t; par[i] != -1; i = par[i]) {
+      rg[par[i]][i] -= flow;
+      rg[i][par[i]] += flow;
+    }
+
+    ans += flow;
+    mset(cont, 0);
+  }
+
+  return ans;
+}
+
+
+int main() {
+  ios::sync_with_stdio(0);
+  cin.tie(0);
+
+  int n, m, k, cas = 1;
+  while (cin >> n >> m >> k) {
+    mset(graph, 0);
+
+    N = MAX;
+    int s = 0, t = MAX-1;
+    for (int i = 1; i <= n; ++i) {
+      int c; cin >> c;
+      graph[i + 120][t] = c;
+    }
+
+    for (int i = 0; i < k; ++i) {
+      int x, y; cin >> x >> y;
+      graph[y][x + 120] = 1;
+    }
+
+    for (int i = 1; i <= m; ++i)
+      graph[s][i] = 1;
+
+    cout << "Instancia " << cas++ << ende;
+    cout << ford_fulkerson(s, t) << ende;
+    cout << ende;
+  }
+  return 0;
+}
diff --git a/contests/Cadernaveis/LA5220.cpp b/contests/Cadernaveis/LA5220.cpp
new file mode 100644
index 0000000..efa746c
--- /dev/null
+++ b/contests/Cadernaveis/LA5220.cpp
@@ -0,0 +1,102 @@
+#include <bits/stdc++.h>
+
+#define MAX 1000
+#define EPS 1e-6
+#define MOD 1000000007
+#define inf 0x3f3f3f3f
+#define llinf 0x3f3f3f3f3f3f3f3f
+
+#define fi first
+#define se second
+#define sz size()
+#define pb push_back
+#define ende '\n'
+
+#define all(x) (x).begin(), (x).end()
+#define rall(x) (x).rbegin(), (x).rend()
+#define mset(x, y) memset(&x, (y), sizeof(x))
+
+using namespace std; 
+
+typedef long long ll;
+typedef pair<int,int> ii;
+
+int N;
+int par[MAX];
+int graph[MAX][MAX], rg[MAX][MAX];
+
+bool cont[MAX];
+
+bool path(int s, int t) {
+  queue<int> Q;
+  Q.push(s);
+  cont[s] = true;
+
+  while (!Q.empty()) {
+    int u = Q.front(); Q.pop();
+
+    if (u == t)
+      return true;
+
+    for (int i = 0; i < N; ++i)
+      if (!cont[i] && rg[u][i]) {
+        cont[i] = true;
+        par[i] = u;
+        Q.push(i);
+      }
+  }
+
+  return false;
+}
+
+
+int edmonds_karp(int s, int t) {
+  int ans = 0;
+  par[s] = -1;
+
+  mset(cont, 0);
+  memcpy(rg, graph, sizeof(graph));
+
+  while (path(s, t)) {
+    int flow = inf;
+
+    for (int i = t; par[i] != -1; i = par[i])
+      flow = min(flow, rg[par[i]][i]);
+
+    for (int i = t; par[i] != -1; i = par[i]) {
+      rg[par[i]][i] -= flow;
+      rg[i][par[i]] += flow;
+    }
+
+    ans += flow;
+    mset(cont, 0);
+  }
+
+  return ans;
+}
+
+
+int main() {
+  ios::sync_with_stdio(0);
+  cin.tie(0);
+
+  int n, cas = 1;
+  while (cin >> n && n) {
+    N = n;
+    int s, t, c; cin >> s >> t >> c;
+
+    mset(graph, 0);
+    for (int i = 0; i < c; ++i) {
+      int o, d, ca; cin >> o >> d >> ca;
+      o--, d--;
+      graph[o][d] += ca;
+      graph[d][o] += ca;
+    }
+
+    cout << "Network " << cas++ << ende;
+    cout << "The bandwidth is " << edmonds_karp(s-1, t-1) << "." << ende;
+    cout << ende;
+  }
+
+  return 0;
+}
-- 
GitLab