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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

int main() {

  int N, M, st, A, B, C, D, X1, X2, Y1, Y2;
  int K[20][20];
  for (int i = 0; i < 20; ++i) {
    for (int j = 0; j < 20; ++j) {
      K[i][j] = INT_MAX / 2;
    }
  }
  scanf("%d%d", &N, &M);
  {

    for (int i = 0; i < M; ++i) {
      scanf("%d,%d,%d,%d", &A, &B, &C, &D);
      K[A - 1][B - 1] = C;
      K[B - 1][A - 1] = D;
    }
    scanf("%d,%d,%d,%d", &X1, &X2, &Y1, &Y2);
    for (int k = 0; k < N; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          K[i][j] = min(K[i][j], K[i][k] + K[k][j]);
        }
      }
    }
  }
  cout << (Y1 - Y2 - K[X1 - 1][X2 - 1] - K[X2 - 1][X1 - 1]) << endl;
  return 0;
}

Error: 36

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

int main() {

  int N, M, st, A, B, C, D, X1, X2, Y1, Y2;
  int K[24][24];
  scanf("%d%d", &N, &M);
  {
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        K[i][j] = INT_MAX;
      }
    }
    for (int i = 0; i < M; ++i) {
      scanf("%d,%d,%d,%d", &A, &B, &C, &D);
      K[A - 1][B - 1] = C;
      K[B - 1][A - 1] = D;
    }
    scanf("%d,%d,%d,%d", &X1, &X2, &Y1, &Y2);
    for (int k = 0; k < N; ++k) {
      for (int j = 0; j < N; ++j) {
        for (int i = 0; i < N; ++i) {
          K[j][i] = min(K[j][i], K[j][k] + K[k][i]);
        }
      }
    }
  }
  return Y1 - Y2 - K[X1 - 1][X2 - 1] - K[X2 - 1][X1 - 1];
}
Problem p00117 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

const int MAX = 10000100;

#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define all(a) (a).begin(), (a).end()

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> town(n, vector<int>(n + 1, 1000));

  int a, b, c, d;
  for (int i = 0; i < m; i++) {
    scanf("%d,%d,%d,%d", &a, &b, &c, &d);
    town[--a][--b] = c;
    town[b][a] = d;
  }
  scanf("%d,%d,%d,%d", &a, &b, &c, &d);
  int start = --a;
  int end = --b;
  int get = c;
  int lose = d;

  int max = 0;
  /*
  rep(i,town.size()){
    cout<<i<< " : ";
    rep(j,town[i].size()){
      printf("%4d ",town[i][j]);
    }
    cout<<endl;
  }
  */
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        town[i][j] = min(town[i][j], town[i][k] + town[k][j]);
      }
    }
  }
  // cout<<town[start][end]<<" "<<town[end][start]<<endl;
  cout << get - lose - town[start][end] - town[end][start] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

const int MAX = 10000100;

#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define all(a) (a).begin(), (a).end()

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> town(n + 1, vector<int>(n + 1, 1000));

  int a, b, c, d;
  for (int i = 0; i < m; i++) {
    scanf("%d,%d,%d,%d", &a, &b, &c, &d);
    town[a][b] = c;
    town[b][a] = d;
  }
  scanf("%d,%d,%d,%d", &a, &b, &c, &d);

  int start = a;
  int end = b;

  int get = c;
  int lose = d;

  int max = 0;
  /*
  rep(i,town.size()){
    cout<<i<< " : ";
    rep(j,town[i].size()){
      printf("%4d ",town[i][j]);
    }
    cout<<endl;
  }
  */
  for (int k = 1; k < m - 1; k++) {
    for (int i = 1; i < n + 1; i++) {
      for (int j = 1; j < n + 1; j++) {
        town[i][j] = min(town[i][j], town[i][k] + town[k][j]);
      }
    }
  }
  // cout<<town[start][end]<<" "<<town[end][start]<<endl;
  cout << get - lose - town[start][end] - town[end][start] << endl;
  return 0;
}
Problem p00117 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;
/** Problem0117 : A reward for a Carpenter **/
const int INF = 100000;

int main() {
  int d[21][21];
  int V, E;

  int s, g, plus, minus;
  char c;
  cin >> V >> E;

  for (int i = 0; i < 21; i++)
    for (int j = 0; j < 21; j++)
      d[i][j] = INF;

  for (int i = 0; i < E; i++) {
    int a, b, dd, ddd;
    cin >> a >> c >> b >> c >> dd >> c >> ddd;
    d[a][b] = dd;
    d[b][a] = ddd;
  }

  cin >> s >> c >> g >> c >> plus >> c >> minus;

  for (int k = 1; k <= V; k++)
    for (int i = 1; i <= V; i++)
      for (int j = 1; j <= V; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

  cout << plus - (minus + d[s][g] + d[g][s]) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;
/** Problem0117 : A reward for a Carpenter **/
const int INF = 100000;
int d[21][21];
int path[21][21];
int V, E;

int main() {
  int s, g, plus, minus;
  char c;
  cin >> V >> E;

  for (int i = 0; i < 21; i++)
    for (int j = 0; j < 21; j++)
      d[i][j] = INF;

  for (int i = 0; i < E; i++) {
    int a, b;
    cin >> a >> c >> b >> c >> d[a][b] >> c >> d[b][a];
  }

  cin >> s >> c >> g >> c >> plus >> c >> minus;

  for (int k = 1; k <= V; k++)
    for (int i = 1; i <= V; i++)
      for (int j = 1; j <= V; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

  cout << plus - (minus + d[s][g] + d[g][s]) << endl;
  return 0;
}
Problem p00117 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
#define INF 1000000000;
using namespace std;
int main() {
  int n, m;
  char q;
  cin >> n >> m;
  int dp[n][n];
  rep(i, n) rep(j, n) dp[i][j] = i == j ? 0 : INF;
  rep(i, m) {
    int a, b, c, d;
    cin >> a >> q >> b >> q >> c >> q >> d;
    a--;
    b--;
    dp[a][b] = c;
    dp[b][a] = d;
  }
  rep(k, n) rep(i, n) rep(j, n) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
  int s, g, V, P;
  cin >> s >> q >> g >> q >> V >> q >> P;
  s--;
  g--;
  cout << V - P - dp[s][g] - dp[g][s] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
#define INF 1000000000;
using namespace std;
int main() {
  int n, m;
  char q;
  cin >> n >> m;
  int dp[n][n];
  rep(i, n) rep(j, n) dp[i][j] = i == j ? 0 : INF;
  rep(i, m) {
    int a, b, c, d;
    cin >> a >> q >> b >> q >> c >> q >> d;
    dp[a][b] = c;
    dp[b][a] = d;
  }
  rep(k, n) rep(i, n) rep(j, n) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
  int s, g, V, P;
  cin >> s >> q >> g >> q >> V >> q >> P;
  cout << V - P - dp[s][g] - dp[g][s] + 1 << endl;
  return 0;
}
Problem p00117 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define MAX_N 20
#define INF 1e9
using namespace std;

int dp[MAX_N][MAX_N];
int n, m;

void warshall_froyd() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
}

int main() {
  cin >> n;
  cin >> m;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = (i == j ? 0 : INF);
    }
  }

  char camma;
  for (int i = 0; i < m; i++) {
    int a, b, atob, btoa;
    cin >> a >> camma >> b >> camma >> atob >> camma >> btoa;
    a--;
    b--;
    dp[a][b] = atob;
    dp[b][a] = btoa;
  }

  warshall_froyd();

  int start, goal, reward, price;
  cin >> start >> camma >> goal >> camma >> reward >> camma >> price;
  start--;
  goal--;
  cout << reward - dp[start][goal] - dp[goal][start] - price << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#define MAX_N 20
#define INF 1e9
using namespace std;

int dp[MAX_N][MAX_N];
int n, m;

void warshall_froyd() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
    }
  }
}

int main() {
  cin >> n;
  cin >> m;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = (i == j ? 0 : INF);
      dp[i][j] = (i == j ? 0 : INF);
    }
  }

  for (int i = 0; i < m; i++) {
    int a, b, atob, btoa;
    cin >> a >> b >> atob >> btoa;
    a--;
    b--;
    dp[a][b] = atob;
    dp[b][a] = btoa;
  }

  warshall_froyd();

  int start, goal, reward, price;
  cin >> start >> goal >> reward >> price;
  cout << reward - dp[start][goal] - price << endl;
  return 0;
}
Problem p00117 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
int N, M, A, B, C, D, x1, x2, y1, y2;
const int inf = 1001000100;
int K[32][32] = {};

int main() {
  scanf("%d%d", &N, &M);

  for (int i = 1; i < N + 1; i++)
    for (int j = 1; j < N + 1; j++)
      K[i][j] = inf;

  for (int i = 0; i < M; ++i) {
    scanf("%d,%d,%d,%d", &A, &B, &C, &D);
    //   cerr << "read " << A << " " << B << " " << C << " " << D << endl;
    K[A][B] = C;
    K[B][A] = D;
  }

  scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2);

  for (int k = 1; k < N + 1; k++) {
    for (int i = 1; i < N + 1; i++) {
      for (int j = 1; j < N + 1; j++) {
        if (K[i][j] > K[i][k] + K[k][j])
          K[i][j] = K[i][k] + K[k][j];
        // cout << K[i][j] << " ";
      }
      // cout << endl;
    }
  }
  cout << y1 - y2 - K[x1][x2] - K[x2][x1] << endl;
}

Error: 0
read 1 2 2 2
read 1 3 4 3
read 1 4 4 2
read 2 5 3 2
read 3 4 4 2
read 3 6 1 2
read 4 6 1 1
read 5 6 1 2


Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int N, M, A, B, C, D, x1, x2, y1, y2;
const int inf = 1001000100;
int K[32][32] = {};

int main() {
  scanf("%d%d", &N, &M);

  for (int i = 1; i < N + 1; i++)
    for (int j = 1; j < N + 1; j++)
      K[i][j] = inf;

  for (int i = 0; i < M; ++i) {
    scanf("%d,%d,%d,%d", &A, &B, &C, &D);
    cerr << "read " << A << " " << B << " " << C << " " << D << endl;
    K[A][B] = C;
    K[B][A] = D;
  }

  scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2);

  for (int k = 1; k < N + 1; k++) {
    for (int i = 1; i < N + 1; i++) {
      for (int j = 1; j < N + 1; j++) {
        if (K[i][j] > K[i][k] + K[k][j])
          K[i][j] = K[i][k] + K[k][j];
        // cout << K[i][j] << " ";
      }
      // cout << endl;
    }
  }

  cout << y1 - y2 - K[x1][x2] - K[x2][x1] << endl;
}
Problem p00117 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>

#define INF 10000000

int n, m, st, gl, money, pole;
int roads[21][21];
int in[4];
std::string input;

int check(int start, int goal) {
  int cost[21];
  int used = 0;

  std::fill(cost, cost + 21, INF);
  cost[start] = 0;

  while (1) {
    int v = -1;
    for (int i = 1; i <= n; i++) {
      if (!(used & 1 << i) && (v == -1 || cost[i] < cost[v]))
        v = i;
    }

    if (v == -1)
      break;

    used = used | 1 << v;

    for (int i = 1; i <= n; ++i) {
      cost[i] = std::min(cost[i], cost[v] + roads[v][i]);
    }
  }

  return cost[goal];
}

int main() {
  for (int i = 0; i < 21; ++i)
    std::fill(roads[i], roads[i] + 21, INF);

  std::cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    std::cin >> input;

    sscanf(input.c_str(), "%d,%d,%d,%d", in, &in[1], &in[2], &in[3]);

    roads[in[0]][in[1]] = in[2];
    roads[in[1]][in[0]] = in[3];
  }

  std::cin >> input;
  sscanf(input.c_str(), "%d,%d,%d,%d", &st, &gl, &money, &pole);

  money -= pole;

  int cost = check(st, gl);
  cost += check(gl, st);

  std::cout << money - cost << std::endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>

#define INF 10000000

int n, m, st, gl, money, pole;
int roads[21][21];
int in[4];
std::string input;

int check(int start, int goal) {
  int cost[21];
  int used = 0;

  std::fill(cost, cost + 21, INF);
  cost[start] = 0;

  while (1) {
    int v = -1;
    for (int i = 1; i <= m; i++) {
      if (!(used & 1 << i) && (v == -1 || cost[i] < cost[v]))
        v = i;
    }

    if (v == -1)
      break;

    used = used | 1 << v;

    for (int i = 1; i <= m; ++i) {
      cost[i] = std::min(cost[i], cost[v] + roads[v][i]);
    }
  }

  return cost[goal];
}

int main() {
  for (int i = 0; i < 21; ++i)
    std::fill(roads[i], roads[i] + 21, INF);

  std::cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    std::cin >> input;

    sscanf(input.c_str(), "%d,%d,%d,%d", in, &in[1], &in[2], &in[3]);

    roads[in[0]][in[1]] = in[2];
    roads[in[1]][in[0]] = in[3];
  }

  std::cin >> input;
  sscanf(input.c_str(), "%d,%d,%d,%d", &st, &gl, &money, &pole);

  money -= pole;

  int cost = check(st, gl);
  cost += check(gl, st);

  std::cout << money - cost << std::endl;
}
Problem p00117 (C++) — buggy submission, verdict: Runtime Error

#include <climits>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct edge {
  edge(){};
  edge(int to, int cost) {
    p = to;
    c = cost;
  }
  int p, c;
};
bool operator<(edge a, edge b) { return a.c > b.c; }
int s, e, money, pay;
vector<vector<edge>> edges;

void init() { edges.clear(); }

bool input() {
  char t;
  int n;
  if (!(cin >> n))
    return false;
  edges.resize(n);
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c, d;
    cin >> a >> t >> b >> t >> c >> t >> d;
    a--;
    b--;
    edges[a].push_back(edge(b, c));
    edges[b].push_back(edge(a, d));
  }
  cin >> s >> t >> e >> t >> money >> t >> pay;
  s--, e--;
  return true;
}

int path_cost(int start, int end) {
  priority_queue<edge> q;
  vector<int> dp(edges.size());
  for (int i = 0; i < dp.size(); i++) {
    dp[i] = -1;
  }
  q.push(edge(start, 0));
  while (!q.empty()) {
    edge now = q.top();
    q.pop();

    if (dp[now.p] != -1)
      continue;
    dp[now.p] = now.c;

    if (now.p == end)
      return now.c;

    for (int i = 0; i < edges[now.p].size(); i++) {
      edge to = edges[now.p][i];
      q.push(edge(to.p, now.c + to.c));
    }
  }
  return dp[end];
}

int solve() {
  int tmp = 0;
  tmp += path_cost(s, e);
  tmp += path_cost(e, s);
  return money - tmp - pay;
}

int main() {
  while (init(), input()) {
    cout << solve() << endl;
  }
}

Error: -11

Fixed submission:

#include <climits>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct edge {
  edge(){};
  edge(int to, int cost) {
    p = to;
    c = cost;
  }
  int p, c;
};
bool operator<(edge a, edge b) { return a.c < b.c; }
int s, e, money, pay;
vector<vector<edge>> edges;

void init() { edges.clear(); }

void input() {
  char t;
  int n;
  edges.resize(n);
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c, d;
    cin >> a >> t >> b >> t >> c >> t >> d;
    a--;
    b--;
    edges[a].push_back(edge(b, c));
    edges[b].push_back(edge(a, d));
  }
  cin >> s >> t >> e >> t >> money >> t >> pay;
  s--, e--;
}

int path_cost(int start, int end) {
  priority_queue<edge> q;
  vector<int> dp(edges.size());
  for (int i = 0; i < dp.size(); i++) {
    dp[i] = INT_MAX;
  }
  q.push(edge(start, 0));
  dp[start] = 0;
  while (!q.empty()) {
    edge now = q.top();
    q.pop();
    if (now.p == end)
      return now.c;
    for (int i = 0; i < edges[now.p].size(); i++) {
      edge tmp = edges[now.p][i];
      if (dp[tmp.p] > tmp.c + now.c) {
        dp[tmp.p] = tmp.c + now.c;
        q.push(edge(tmp.p, tmp.c + now.c));
      }
    }
  }
  return dp[end];
}

int solve() {
  int tmp = 0;
  tmp += path_cost(s, e);
  tmp += path_cost(e, s);
  return money - tmp - pay;
}

int main() {
  init();
  input();
  cout << solve() << endl;
}
Problem p00117 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>

using namespace std;

const int inf = 1001001001;
int n, m, a, b, c, d, x1, x2, y1, y2;
int K[32][32];

void floyd() {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (K[i][j] > K[i][k] + K[k][j])
          K[i][j] = K[i][k] + K[k][j];
      }
    }
  }
}

void show() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (j != 1)
        cout << " ";
      cout << K[i][j];
    }
    cout << endl;
  }
}

int main() {
  for (int i = 0; i < 32; i++) {
    for (int j = 0; j < 32; j++) {
      K[i][j] = inf;
    }
  }
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d,%d,%d,%d", &a, &b, &c, &d);
    // cerr << "read " << a << " " << b << " " << c << " " << d << endl;
    K[a][b] = c;
    K[b][a] = d;
  }
  floyd();
  scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2);
  // show();
  cout << y1 - y2 - K[x1][x2] - K[x2][x1] << endl;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

const int inf = 1001001001;
int n, m, a, b, c, d, x1, x2, y1, y2;
int K[32][32];

void floyd() {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (K[i][j] > K[i][k] + K[k][j])
          K[i][j] = K[i][k] + K[k][j];
      }
    }
  }
}

int main() {
  for (int i = 0; i < 32; i++) {
    for (int j = 0; j < 32; j++) {
      K[i][j] = inf;
    }
  }
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d,%d,%d,%d", &a, &b, &c, &d);
    cerr << "read " << a << " " << b << " " << c << " " << d << endl;
    K[a][b] = c;
    K[b][a] = d;
  }
  floyd();
  scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2);
  cout << y1 - y2 - K[x1][x2] << endl;
}
Problem p00117 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#define INF 200
#define rep(i, a) for (i = 0; i < a; i++)
using namespace std;
int main(void) {
  int glid[20][20], n, m, a, b, c, d, i, j, k, p, q, w, v;
  cin >> n >> m;
  rep(i, n) rep(j, n) glid[i][j] = INF;
  rep(i, n) glid[i][i] = 0;
  rep(i, m) {
    scanf("%d,%d,%d,%d", &a, &b, &c, &d);
    a--;
    b--;
    glid[a][b] = c;
    glid[b][a] = d;
  }
  scanf("%d,%d,%d,%d", &p, &q, &w, &v);
  rep(k, n) rep(i, n) rep(j, n) glid[i][j] =
      min(glid[i][j], glid[i][k] + glid[k][j]);
  p--;
  q--;
  cout << w - v - (glid[p][q] + glid[q][p]) << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#define INF 10000000
using namespace std;
int glid[21][21];
int kakutei[21], leng[21];
int main(void) {
  ios::sync_with_stdio(false);
  int i, j, k;
  int n, m, a, b, a_b, b_a;
  int start, finish, okane, value, mi, point;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      glid[i][j] = INF;
    }
  }
  for (i = 0; i < n; i++)
    glid[i][i] = 0;
  for (i = 0; i < m; i++) {
    scanf("%d,%d,%d,%d", &a, &b, &a_b, &b_a);
    a--;
    b--;
    glid[a][b] = a_b;
    glid[b][a] = b_a;
  }
  scanf("%d,%d,%d,%d", &start, &finish, &okane, &value);
  start--;
  finish--;
  for (i = 0; i < n; i++) {
    leng[i] = INF;
    kakutei[i] = 0;
  }
  leng[start] = 0;
  for (i = 0; i < n; i++) {
    mi = INF;
    for (j = 0; j < n; j++) {
      if (kakutei[j] == 0 && leng[j] < mi) {
        point = j;
        mi = leng[j];
      }
    }
    kakutei[point] = 1;
    mi = INF;
    for (j = 0; j < n; j++) {
      if (leng[point] + glid[point][j] < leng[j]) {
        leng[j] = leng[point] + glid[point][j];
      }
    }
  }
  okane -= leng[finish];
  for (i = 0; i < n; i++) {
    leng[i] = INF;
    kakutei[i] = 0;
  }
  swap(start, finish);
  leng[start] = 0;
  for (i = 0; i < n; i++) {
    mi = INF;
    for (j = 0; j < n; j++) {
      if (kakutei[j] == 0 && leng[j] < mi) {
        point = j;
        mi = leng[j];
      }
    }
    kakutei[point] = 1;
    for (j = 0; j < n; j++) {
      if (leng[point] + glid[point][j] < leng[j]) {
        leng[j] = leng[point] + glid[point][j];
      }
    }
  }
  okane -= leng[finish];
  cout << okane - value << endl;
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

char c[100][100];

void dfs(int x, int y) {
  char ch = c[x][y];
  c[x][y] = 0;
  int dx[4] = {0, 0, 1, -1};
  int dy[4] = {-1, 1, 0, 0};
  for (int i = 0; i < 4; i++) {
    if (x + dx[i] < 100 && x + dx[i] >= 0 && y + dy[i] < 100 &&
        y + dy[i] >= 0 && ch == c[x + dx[i]][y + dy[i]]) {
      dfs(x + dx[i], y + dy[i]);
    }
  }
  return;
}

int solve(int h, int w) {
  for (int i = 0; i < h; i++) {
    getchar();
    for (int j = 0; j < w; j++) {
      c[i][j] = getchar();
    }
  }
  int res = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (c[i][j] != 0) {
        res++;
        dfs(i, j);
      }
    }
  }
  return res;
}

int main() {
  while (1) {
    int h, w;
    scanf("%d %d", &h, &w);
    if (h == 0 && w == 0)
      return 0;
    else
      printf("%d\\n", solve(h, w));
  }
}

Error: 0

Fixed submission:

#include <cstdio>

char c[100][100];

void dfs(int x, int y) {
  char ch = c[x][y];
  c[x][y] = 0;
  int dx[4] = {0, 0, 1, -1};
  int dy[4] = {-1, 1, 0, 0};
  for (int i = 0; i < 4; i++) {
    if (ch == c[x + dx[i]][y + dy[i]]) {
      dfs(x + dx[i], y + dy[i]);
    }
  }
  return;
}

int solve(int h, int w) {
  for (int i = 0; i < h; i++) {
    getchar();
    for (int j = 0; j < w; j++) {
      c[i][j] = getchar();
    }
  }
  int res = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (c[i][j] != 0) {
        res++;
        dfs(i, j);
      }
    }
  }
  return res;
}

int main() {
  while (1) {
    int h, w;
    scanf("%d %d", &h, &w);
    if (h == 0 && w == 0)
      return 0;
    else
      printf("%d\\n", solve(h, w));
  }
  return 0;
}