alexjercan/bugnet huggingface dataset:
Problem p02376 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef pair<int, int> pii;

const int INF = 0x3fffffff;

class Flow {
public:
  int N;
  vector<vector<int>> F, C, edge;
  Flow(int n, int e)
      : F(n, vector<int>(n)), C(n, vector<int>(n)), N(n), edge(n) {}
  void addEdge(int f, int t, int c) {
    F[f][t] = C[f][t] = c;
    edge[f].push_back(t);
    edge[t].push_back(f);
  }
  int flow(int n, int c, int T, vector<bool> &bf) {
    if (n == T)
      return c;
    bf[n] = true;
    for (int i : edge[n])
      if (F[n][i] > 0 && !bf[i]) {
        int f = flow(i, min(c, F[n][i]), T, bf);
        F[n][i] -= f;
        F[i][n] += f;
        if (f > 0)
          return f;
      }
    return 0;
  }
  int maxFlow(int S, int T) {
    int ret = 0;
    vector<bool> bf(N);
    for (int add; add = flow(0, INF, T, bf); bf.assign(N, 0))
      ret += add;
    return ret;
  }
  void clear() { rep(i, N) rep(j, N) F[i][j] = C[i][j]; }
};

int main() {
  int V, E;
  cin >> V >> E;
  Flow fl(V, E);
  for (int i = 1; i <= E; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    fl.addEdge(u, v, c);
  }

  cout << fl.maxFlow(0, V - 1) << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef pair<int, int> pii;

const int INF = 0x3fffffff;

class Flow {
public:
  int N;
  vector<vector<int>> F, C, edge;
  Flow(int n, int e)
      : F(n, vector<int>(n)), C(n, vector<int>(n)), N(n), edge(e) {}
  void addEdge(int f, int t, int c) {
    F[f][t] = C[f][t] = c;
    edge[f].push_back(t);
    edge[t].push_back(f);
  }
  int flow(int n, int c, int T, vector<bool> &bf) {
    if (n == T)
      return c;
    bf[n] = true;
    for (int i : edge[n])
      if (F[n][i] > 0 && !bf[i]) {
        int f = flow(i, min(c, F[n][i]), T, bf);
        F[n][i] -= f;
        F[i][n] += f;
        if (f > 0)
          return f;
      }
    return 0;
  }
  int maxFlow(int S, int T) {
    int ret = 0;
    vector<bool> bf(N);
    for (int add; add = flow(0, INF, T, bf); bf.assign(N, 0))
      ret += add;
    return ret;
  }
  void clear() { rep(i, N) rep(j, N) F[i][j] = C[i][j]; }
};

int main() {
  int V, E;
  cin >> V >> E;
  Flow fl(V, E);
  for (int i = 1; i <= E; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    fl.addEdge(u, v, c);
  }

  cout << fl.maxFlow(0, V - 1) << endl;

  return 0;
}
Problem p02376 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
#include <vector>
int v, e;
struct Edge {
  Edge() {}
  Edge(int from_, int to_, int capacity_, int rev_)
      : from(from_), to(to_), capacity(capacity_), rev(rev_) {}
  int from;
  int to;
  int capacity;
  int rev;
};

std::vector<Edge> edges[128];

void addEdge(int from, int to, int capacity) {
  int n1 = edges[from].size();
  int n2 = edges[to].size();
  edges[from].push_back(Edge(from, to, capacity, n2));
  edges[to].push_back(Edge(to, from, 0, n1));
}

bool checked[128];
void init_flow() {
  for (int i = 0; i < 128; ++i) {
    checked[i] = false;
  }
}
int flow(int from, int to, int v) {
  if (from == to)
    return v;
  checked[from] = true;
  for (Edge &edge : edges[from]) {
    if (checked[edge.to])
      continue;
    int nv = std::min(v, edge.capacity);
    if (nv == 0)
      continue;
    int f = flow(edge.to, to, nv);
    if (f == 0)
      continue;
    edge.capacity -= f;
    edges[edge.to][edge.rev].capacity += f;
    return f;
  }
  return 0;
}

int maximum_flow(int from, int to) {
  int res = 0;
  for (;;) {
    init_flow();
    int v = flow(from, to, (1 << 30));
    if (v == 0)
      break;
    res += v;
  }
  return res;
}

int main() {
  scanf("%d %d", &v, &e);
  for (int i = 0; i < e; ++i) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    addEdge(a, b, c);
  }

  printf("%d\\n", maximum_flow(0, v - 1));

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>
int v, e;
struct Edge {
  Edge() {}
  Edge(int from_, int to_, int capacity_, int rev_)
      : from(from_), to(to_), capacity(capacity_), rev(rev_) {}
  int from;
  int to;
  int capacity;
  int rev;
};

std::vector<Edge> edges[128];

void addEdge(int from, int to, int capacity) {
  int n1 = edges[from].size();
  int n2 = edges[to].size();
  edges[from].push_back(Edge(from, to, capacity, n2));
  edges[to].push_back(Edge(to, from, 0, n1));
}

bool checked[128];
void init_flow() {
  for (int i = 0; i < 128; ++i) {
    checked[i] = false;
  }
}
int flow(int from, int to, int v) {
  if (from == to)
    return v;
  checked[from] = true;
  for (Edge edge : edges[from]) {
    if (checked[edge.to])
      continue;
    int nv = std::min(v, edge.capacity);
    if (nv == 0)
      continue;
    int f = flow(edge.to, to, nv);
    if (f == 0)
      continue;
    return f;
  }
  return 0;
}

int maximum_flow(int from, int to) {
  int res = 0;
  for (;;) {
    init_flow();
    int v = flow(from, to, (1 << 30));
    if (v == 0)
      break;
    res += v;
  }
  return res;
}

int main() {
  scanf("%d %d", &v, &e);
  for (int i = 0; i < e; ++i) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    addEdge(a, b, c);
  }

  printf("%d\\n", maximum_flow(0, v - 1));

  return 0;
}
Problem p02376 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <vector>

typedef struct {
  int to;
  int cap;
  int rev;
} edge;

int v;
std::vector<edge> vec[100];
bool flag[100];

int min(int a, int b) { return a > b ? b : a; }

int dfs(int a, int b) {
  int i;
  if (a == v - 1)
    return b;
  flag[a] = false;
  for (i = 0; i < vec[a].size(); ++i)
    if (flag[vec[a][i].to] && vec[a][i].cap) {
      edge *e = &vec[a][i];
      int t = min(b, (*e).cap), d;
      d = dfs((*e).to, t);
      if (d) {
        (*e).cap -= d;
        vec[(*e).to][(*e).rev].cap += d;
        // flag[a] = true;
        return d;
      }
    }
  // flag[a] = true;
  return 0;
}

int main(void) {
  int i, j, k, e, a, b, c, ans = 0;
  scanf("%d%d", &v, &e);
  for (i = 0; i < v; ++i)
    flag[i] = true;
  flag[0] = false;
  for (i = 0; i < e; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    edge ta, tb;
    ta.to = b, ta.cap = c, ta.rev = vec[b].size();
    tb.to = a, tb.cap = 0, tb.rev = vec[a].size();
    vec[a].push_back(ta);
    vec[b].push_back(tb);
  }
  while (1) {
    for (i = 0; i < v; ++i)
      flag[i] = true;
    flag[0] = false;
    a = dfs(0, 10e8);
    if (a)
      ans += a;
    else
      break;
  }
  printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <vector>

typedef struct {
  int to;
  int cap;
  int rev;
} edge;

int v;
std::vector<edge> vec[100];
bool flag[100];

int min(int a, int b) { return a > b ? b : a; }

int dfs(int a, int b) {
  int i;
  if (a == v - 1)
    return b;
  flag[a] = false;
  for (i = 0; i < vec[a].size(); ++i)
    if (flag[vec[a][i].to] && vec[a][i].cap) {
      edge *e = &vec[a][i];
      int t = min(b, (*e).cap), d;
      d = dfs((*e).to, t);
      if (d) {
        (*e).cap -= d;
        vec[(*e).to][(*e).rev].cap += d;
        flag[a] = true;
        return d;
      }
    }
  flag[a] = true;
  return 0;
}

int main(void) {
  int i, j, k, e, a, b, c, ans = 0;
  scanf("%d%d", &v, &e);
  for (i = 0; i < v; ++i)
    flag[i] = true;
  flag[0] = false;
  for (i = 0; i < e; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    edge ta, tb;
    ta.to = b, ta.cap = c, ta.rev = vec[b].size();
    tb.to = a, tb.cap = 0, tb.rev = vec[a].size();
    vec[a].push_back(ta);
    vec[b].push_back(tb);
  }
  while (1) {
    a = dfs(0, 10e8);
    if (a)
      ans += a;
    else
      break;
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02376 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef pair<int, int> pii;

const int INF = 0x3fffffff;

int V, E;
int C[1005];
int F[1005];
vector<pii> e[105];
int solve(int n, int c, bool bf[105]) {
  if (n == V - 1)
    return c;
  bf[n] = true;
  for (auto to : e[n]) {
    int i = to.second;
    int cc = (i > 0 ? C[i] - F[i] : F[-i]);
    if (bf[to.first] || cc == 0)
      continue;
    int flow = solve(to.first, min(c, cc), bf);
    if (flow > 0) {
      F[abs(i)] += flow * (i / abs(i));
      return flow;
    }
  }
  return 0;
}

int main() {
  cin >> V >> E;
  for (int i = 1; i <= E; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    e[u].emplace_back(v, i);
    e[v].emplace_back(u, -i);
    C[i] = c;
  }
  int ans = 0;
  while (1) {
    bool bf[105] = {};
    int add = solve(0, INF, bf);
    ans += add;
    if (add == 0)
      break;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) begin(x), end(x)

using namespace std;
typedef pair<int, int> pii;

const int INF = 0x3fffffff;

int V, E;
int C[1005];
int F[1005];
vector<pii> e[105];
int solve(int n, int c, vector<bool> bf) {
  if (bf[n] || c == 0)
    return 0;
  //	cout << "solve: " << n << " " << c << endl;
  if (n == V - 1)
    return c;
  bf[n] = true;
  for (auto to : e[n]) {
    int i = to.second;
    int cc = (i > 0 ? C[i] - F[i] : F[-i]);
    //		cout << "flow " << n << endl;
    int flow = solve(to.first, min(c, cc), bf);
    if (flow > 0) {
      F[abs(i)] += flow * (i / abs(i));
      return flow;
    }
  }
  return 0;
}

int main() {
  cin >> V >> E;
  rep(i, E + 1) {
    if (i == 0)
      continue;
    int u, v, c;
    cin >> u >> v >> c;
    e[u].emplace_back(v, i);
    e[v].emplace_back(u, -i);
    C[i] = c;
  }
  int ans = 0;
  vector<bool> bf(105, false);
  for (int add; add = solve(0, INF, bf); /*fill(all(bf),false)*/)
    ans += add;

  cout << ans << endl;

  return 0;
}
Problem p02376 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) begin(x), end(x)

using namespace std;
typedef pair<int, int> pii;

const int INF = 0x3fffffff;

int V, E;
int C[1005];
int F[1005];
vector<pii> e[105];
int solve(int n, int c, vector<bool> &bf) {
  if (bf[n] || c == 0)
    return 0;
  //	cout << "solve: " << n << " " << c << endl;
  if (n == V - 1)
    return c;
  bf[n] = true;
  for (auto to : e[n]) {
    int i = to.second;
    int cc = (i > 0 ? C[i] - F[i] : F[-i]);
    //		cout << "flow " << n << endl;
    int flow = solve(to.first, min(c, cc), bf);
    if (flow > 0) {
      F[abs(i)] += flow * (i / abs(i));
      return flow;
    }
  }
  return 0;
}

int main() {
  cin >> V >> E;
  rep(i, E + 1) {
    if (i == 0)
      continue;
    int u, v, c;
    cin >> u >> v >> c;
    e[u].emplace_back(v, i);
    e[v].emplace_back(u, -i);
    C[i] = c;
  }
  int ans = 0;
  vector<bool> bf(105, false);
  for (int add; add = solve(0, INF, bf); fill(all(bf), false))
    ans += add;

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef pair<int, int> pii;

const int INF = 0x3fffffff;

int V, E;
int C[1005];
int F[1005];
vector<pii> e[105];
int solve(int n, int c, vector<bool> bf) {
  if (bf[n])
    return 0;
  if (n == V - 1)
    return c;
  bf[n] = true;
  for (auto to : e[n]) {
    int i = to.second;
    int cc = (i > 0 ? C[i] - F[i] : F[-i]);
    int flow = solve(to.first, min(c, cc), bf);
    if (flow > 0) {
      F[abs(i)] += flow * (i / abs(i));
      return flow;
    }
  }
  return 0;
}

int main() {
  cin >> V >> E;
  rep(i, E + 1) {
    if (i == 0)
      continue;
    int u, v, c;
    cin >> u >> v >> c;
    e[u].emplace_back(v, i);
    e[v].emplace_back(u, -i);
    C[i] = c;
  }
  int ans = 0;
  vector<bool> bf(105, false);
  for (int add; add = solve(0, INF, bf);)
    ans += add;

  cout << ans << endl;

  return 0;
}
Problem p02376 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define INF 1000000000
#define MAX_V 100
#define MAX_E 1000
using namespace std;
struct edge {
  int to, cap, rev;
  edge(int to_, int cap_, int rev_) : to(to_), cap(cap_), rev(rev_){};
};

int V, E;
vector<edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];

void AddEdge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
}

void BFS(int s) {
  fill(level, level + V, -1);
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (size_t i = 0; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

int DFS(int v, int t, int f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      int d = DFS(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int Dinic(int s, int t) {
  int flow = 0;
  while (true) {
    BFS(s);
    if (level[t] < 0)
      return flow;
    fill(iter, iter + V, 0);
    int f;
    while ((f = DFS(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

int main() {
  cin >> V >> E;
  for (int i = 0, u, v, c; i < E; i++) {
    cin >> u >> v >> c;
    AddEdge(u, v, c);
  }
  cout << Dinic(0, V - 1) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 1000000000
#define MAX_V 100
#define MAX_E 1000
using namespace std;
struct edge {
  int to, cap, rev;
  edge(int to_, int cap_, int rev_) : to(to_), cap(cap_), rev(rev_){};
};

int V, E;
vector<edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];

void AddEdge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
}

void BFS(int s) {
  fill(level, level + V, -1);
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (size_t i = 0; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[v] + 1) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

int DFS(int v, int t, int f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      int d = DFS(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int Dinic(int s, int t) {
  int flow = 0;
  while (true) {
    BFS(s);
    if (level[t] < 0)
      return flow;
    fill(iter, iter + V, 0);
    int f;
    while ((f = DFS(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

int main() {
  cin >> V >> E;
  for (int i = 0, u, v, c; i < E; i++) {
    cin >> u >> v >> c;
    AddEdge(u, v, c);
  }
  cout << Dinic(0, V - 1) << endl;
  return 0;
}
Problem p02376 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

const int inf = 1000000009;

int dfs(vector<vector<int>> &graph, bitset<101> &used, int flow, int pos,
        int t) {
  if (pos == t)
    return flow;
  used[pos] = 1;
  for (int i = 0; i < graph[pos].size(); i++) {
    if (used[i] == 0 && graph[pos][i] > 0) {
      int maxflow = dfs(graph, used, min(flow, graph[pos][i]), i, t);
      if (maxflow > 0) {
        graph[i][pos] = graph[i][pos] + maxflow;
        graph[pos][i] = graph[pos][i] - maxflow;
        return maxflow;
      }
    }
  }
  return 0;
}

int maxflow(vector<vector<int>> graph, int s, int t) {
  bool f = true;
  int sum = 0;
  while (f) {
    bitset<101> b(0);
    b[s] = 1;
    int current = dfs(graph, b, inf, s, t);
    sum += current;
    f &= current != 0;
  }
  return sum;
}

int main() {
  int v, e;
  cin >> v >> e;
  vector<vector<int>> graph(v);
  for (auto &l : graph)
    l.resize(v, 0);

  for (int i = 0; i < e; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    graph[u][v] = c;
  }

  cout << maxflow(graph, 0, v - 1) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

const int inf = 1000000009;

int dfs(vector<vector<int>> &graph, bitset<101> used, int flow, int pos,
        int t) {
  if (pos == t)
    return flow;
  used[pos] = 1;
  for (int i = 0; i < graph[pos].size(); i++) {
    if (used[i] == 0 && graph[pos][i] > 0) {
      auto new_used = used;
      new_used[i] = 1;
      int maxflow = dfs(graph, new_used, min(flow, graph[pos][i]), i, t);
      if (maxflow > 0) {
        graph[i][pos] = graph[i][pos] + maxflow;
        graph[pos][i] = graph[pos][i] - maxflow;
        return maxflow;
      }
    }
  }
  return 0;
}

int maxflow(vector<vector<int>> graph, int s, int t) {
  bool f = true;
  int sum = 0;
  while (f) {
    bitset<101> b(0);
    b[s] = 1;
    int current = dfs(graph, b, inf, s, t);
    sum += current;
    f &= current != 0;
  }
  return sum;
}

int main() {
  int v, e;
  cin >> v >> e;
  vector<vector<int>> graph(v);
  for (auto &l : graph)
    l.resize(v, 0);

  for (int i = 0; i < e; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    graph[u][v] = c;
  }

  cout << maxflow(graph, 0, v - 1) << endl;
}
Problem p02377 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 105;
const int maxq = 1005;

struct edge {
  int to, cap, cost, rev;
};

int n, m, k;
int d[maxn];
int a[maxn];
int p1[maxn];
int p2[maxn];
bool inq[maxn];
vector<edge> G[maxn];

void add_edge(int u, int v, int c, int w) {
  G[u].pb(edge{v, c, w, int(G[v].size())});
  G[v].pb(edge{u, 0, -w, int(G[u].size() - 1)});
}

int mcmf(int s, int t) {
  int flow = 0, cost = 0;
  while (1) {
    memset(d, 0x3f, sizeof(d));
    d[s] = 0;
    a[s] = max(0, k - flow);
    int qh = 0, qt = 0, q[maxq];
    q[qt++] = s;
    inq[s] = 1;
    while (qh < qt) {
      int u = q[qh++];
      inq[u] = 0;
      for (int i = 0; i < G[u].size(); i++) {
        edge e = G[u][i];
        if (d[e.to] > d[u] + e.cost && e.cap) {
          d[e.to] = d[u] + e.cost;
          a[e.to] = min(a[u], e.cap);
          p1[e.to] = u;
          p2[e.to] = i;
          if (!inq[e.to]) {
            q[qt++] = e.to;
            inq[e.to] = 1;
          }
        }
      }
    }
    if (d[t] == INF || !a[t])
      break;
    flow += a[t];
    cost += a[t] * d[t];
    for (int v = t, u = p1[t]; v != s; v = u, u = p1[u]) {
      int id = p2[v];
      G[u][id].cap -= a[t];
      id = G[u][id].rev;
      G[v][id].cap += a[t];
    }
  }
  if (flow < k)
    return -1;
  else
    return cost;
}

int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < m; i++) {
    int u, v, c, w;
    cin >> u >> v >> c >> w;
    add_edge(u, v, c, w);
  }
  cout << mcmf(0, n - 1) << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 105;

struct edge {
  int to, cap, cost, rev;
};

int n, m, k;
int d[maxn];
int a[maxn];
int p1[maxn];
int p2[maxn];
bool inq[maxn];
vector<edge> G[maxn];

void add_edge(int u, int v, int c, int w) {
  G[u].pb(edge{v, c, w, int(G[v].size())});
  G[v].pb(edge{u, 0, -w, int(G[u].size() - 1)});
}

int mcmf(int s, int t) {
  int flow = 0, cost = 0;
  while (1) {
    memset(d, 0x3f, sizeof(d));
    d[s] = 0;
    a[s] = max(0, k - flow);
    int qh = 0, qt = 0, q[maxn];
    q[qt++] = s;
    inq[s] = 1;
    while (qh < qt) {
      int u = q[qh++];
      inq[u] = 0;
      for (int i = 0; i < G[u].size(); i++) {
        edge &e = G[u][i];
        if (d[e.to] > d[u] + e.cost && e.cap) {
          d[e.to] = d[u] + e.cost;
          a[e.to] = min(a[u], e.cap);
          p1[e.to] = u;
          p2[e.to] = i;
          if (!inq[e.to]) {
            q[qt++] = e.to;
            inq[e.to] = 1;
          }
        }
      }
    }
    if (d[t] == INF || !a[t])
      break;
    flow += a[t];
    cost += a[t] * d[t];
    for (int v = t, u = p1[t]; v != s; v = u, u = p1[u]) {
      int id = p2[v];
      G[u][id].cap -= a[t];
      id = G[u][id].rev;
      G[v][id].cap += a[t];
    }
  }
  if (flow < k)
    return -1;
  else
    return cost;
}

int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < m; i++) {
    int u, v, c, w;
    cin >> u >> v >> c >> w;
    add_edge(u, v, c, w);
  }
  cout << mcmf(0, n - 1) << '\\n';
}
Problem p02378 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
#define MAX_V 100000
#define INF 100000000
using namespace std;

int V, M;             // 頂点，辺の数
vector<int> G[MAX_V]; // グラフの隣接リスト表現
int match[MAX_V];     // マッチングのペア
bool used[MAX_V];     // DFSですでに調べたかのフラグ

// uとvを結ぶ辺をグラフに追加する
void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

// 増加パスをDFSで探す
bool dfs(int v) {
  for (int i = 0; i < G[v].size(); i++) {
    if (used[G[v][i]] == false) {
      used[G[v][i]] = true;
      if (match[G[v][i]] < 0 || dfs(match[G[v][i]])) {
        match[G[v][i]] = v;
        match[v] = G[v][i];
        return true;
      }
    }
  }
  return false;
}

// 二部グラフの最大マッチングを求める
int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof(used));
      if (dfs(v)) {
        res++;
      }
    }
  }
  return res;
}

int main(void) {
  // cin >> V >> M;
  // int a,b;
  // for(int i=0;i<M;i++){
  // 	cin >> a >> b;
  // 	add_edge(a,b);
  // }
  int X, Y;
  cin >> X >> Y >> M;
  V = X + Y;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    add_edge(x, X + y);
  }

  std::cout << bipartite_matching() << std::endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
#define MAX_V 100
#define INF 1000
using namespace std;

int V, M;             // 頂点，辺の数
vector<int> G[MAX_V]; // グラフの隣接リスト表現
int match[MAX_V];     // マッチングのペア
bool used[MAX_V];     // DFSですでに調べたかのフラグ

// uとvを結ぶ辺をグラフに追加する
void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

// 増加パスをDFSで探す
bool dfs(int v) {
  for (int i = 0; i < G[v].size(); i++) {
    if (used[G[v][i]] == false) {
      used[G[v][i]] = true;
      if (match[G[v][i]] < 0 || dfs(match[G[v][i]])) {
        match[G[v][i]] = v;
        match[v] = G[v][i];
        return true;
      }
    }
  }
  return false;
}

// 二部グラフの最大マッチングを求める
int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof(used));
      if (dfs(v)) {
        res++;
      }
    }
  }
  return res;
}

int main(void) {
  // cin >> V >> M;
  // int a,b;
  // for(int i=0;i<M;i++){
  // 	cin >> a >> b;
  // 	add_edge(a,b);
  // }
  int X, Y;
  cin >> X >> Y >> M;
  V = X + Y;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    add_edge(x, X + y);
  }

  std::cout << bipartite_matching() << std::endl;
}