alexjercan/bugnet huggingface dataset:
Problem p00116 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int h, w;

  while (1) {
    cin >> h >> w;
    if (!h && !w)
      break;

    vector<string> grid;
    for (int i = 0; i < h; i++) {
      string tmp;
      cin >> tmp;
      grid.push_back(tmp);
    }

    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (grid[i][j] == '.') {
          int dx = 1, dy = 1;
          while (i + dy < h && grid[i + dy][j] == '.')
            dy++;
          while (j + dx < w && grid[i][j + dx] == '.')
            dx++;
          int tmp = 0;
          int x = dx;
          for (int k = 0; k < dy; k++) {
            int l;
            for (l = 0; l < x; l++) {
              if (grid[i + k][j + l] == '*') {
                x = min(x, l);
                break;
              }
            }
            tmp = max(tmp, x * (k + 1));
          }
          // if(ans<tmp)cout << "[" << i << " " << j << "]:" << tmp << endl;
          ans = max(ans, tmp);
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int h, w;

  while (1) {
    cin >> h >> w;
    if (!h && !w)
      break;

    vector<string> grid;
    for (int i = 0; i < h; i++) {
      string tmp;
      cin >> tmp;
      grid.push_back(tmp);
    }

    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (grid[i][j] == '.')
          ;
        int dx = 1, dy = 1;
        while (i + dy < h && grid[i + dy][j] == '.')
          dy++;
        while (j + dx < w && grid[i][j + dx] == '.')
          dx++;
        int tmp = 0;
        int x = dx;
        for (int k = 0; k < dy; k++) {
          int l;
          for (l = 0; l < dx; l++) {
            if (grid[i + k][j + l] == '*') {
              x = min(x, l);
              tmp = max(tmp, x * (k + 1));
              break;
            }
          }
          if (l == dx)
            tmp = max(tmp, x * (k + 1));
        }
        // if(ans<tmp)cout << "[" << i << " " << j << "]:" << tmp << endl;
        ans = max(ans, tmp);
      }
    }
    cout << ans << endl;
  }
}
Problem p00116 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

#define INF 1 << 16

int m[500][500];

int count(int x, int y) {
  if (x < 0 || y < 0)
    return 0;
  return m[y][x];
}

typedef pair<int, int> P;

P min(P p1, P p2) {
  return P(max(p1.first, p2.first), min(p1.second, p2.second));
}

int main() {
  int H, W;
  while (cin >> H >> W, H || W) {
    for (int y = 0; y < H; ++y) {
      string str;
      cin >> str;
      for (int x = 0; x < W; ++x)
        m[y][x] = str[x] == '*';
    }

    int len[500][500] = {0};
    for (int y = 0; y < H; ++y) {
      if (!m[y][W - 1])
        len[y][W - 1] = 1;
      for (int x = W - 1; x; --x)
        if (!m[y][x - 1])
          len[y][x - 1] = len[y][x] + 1;
    }

    int ans = 0;
    for (int y = 0; y < H; ++y)
      for (int x = 0; x < W; ++x) {
        int w = INF;
        for (int y2 = y; y2 < H; ++y2) {
          int h = y2 - y + 1;
          w = min(w, len[y2][x]);
          ans = max(ans, w * h);
          if (!w)
            break;
        }
      }

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int m[500][500];

int count(int x, int y) {
  if (x < 0 || y < 0)
    return 0;
  return m[y][x];
}

int main() {
  int H, W;
  while (cin >> H >> W, H || W) {
    for (int y = 0; y < H; ++y) {
      string str;
      cin >> str;
      for (int x = 0; x < W; ++x) {
        m[y][x] = str[x] == '*';
      }
    }

    for (int y = 0; y < H; ++y)
      for (int x = 0; x < W - 1; ++x)
        m[y][x + 1] += m[y][x];

    for (int x = 0; x < W; ++x)
      for (int y = 0; y < H - 1; ++y)
        m[y + 1][x] += m[y][x];

    int ans = 0;
    bool used[500][500] = {0};
    for (int y1 = 0; y1 < H; ++y1) {
      for (int x1 = 0; x1 < W; ++x1) {
        if (used[x1][y1])
          continue;
        for (int y2 = y1; y2 < H; ++y2) {
          for (int x2 = x1; x2 < W; ++x2) {
            if (count(x2, y2) - (count(x2, y1 - 1) + count(x1 - 1, y2) -
                                 count(x1 - 1, y1 - 1)) ==
                0) {
              used[x2][y2] = true;
              ans = max(ans, (y2 - y1 + 1) * (x2 - x1 + 1));
            }
          }
        }
      }
    }

    cout << ans << endl;
  }
}
Problem p00116 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll b[500][500];
char s[510][510];
ll sum[510][510];
int r[500][500];

ll S(int x1, int y1, int x2, int y2) {
  return sum[y2 + 1][x2 + 1] - sum[y2 + 1][x1] - sum[y1][x2 + 1] + sum[y1][x1];
}

int main() {
  int H, W;
  while (scanf("%d%d", &H, &W), H + W) {
    for (int i = 0; i < H; i++) {
      scanf("%s", s[i]);
      for (int j = 0; j < W; j++) {
        if (s[i][j] == '*')
          b[i][j] = 1;
        else
          b[i][j] = 0;
      }
    }
    memset(sum, 0, sizeof sum);
    memset(r, 0, sizeof r);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        sum[i + 1][j + 1] = sum[i + 1][j] + b[i][j];
      }
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        sum[i + 1][j] += sum[i][j];
      }
    }
    for (int i = 0; i < H; i++) {
      for (int j = W - 1; j >= 0; j--) {
        r[i][j] = r[i][j + 1];
        if (s[i][j] == '*')
          r[i][j] = 0;
        else
          r[i][j]++;
      }
    }

    int ans = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        int w = r[i][j];
        for (int k = i; k < H; k++) {
          w = min(w, r[k][j]);
          ans = max(ans, (k - i + 1) * w);
        }
      }
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll b[500][500];
ll sum[510][510];

ll S(int x1, int y1, int x2, int y2) {
  return sum[y2 + 1][x2 + 1] - sum[y2 + 1][x1] - sum[y1][x2 + 1] + sum[y1][x1];
}

int main() {
  int H, W;
  while (scanf("%d%d", &H, &W), H + W) {
    for (int i = 0; i < H; i++) {
      char s[510];
      scanf("%s", s);
      for (int j = 0; j < W; j++) {
        if (s[j] == '*')
          b[i][j] = 1;
        else
          b[i][j] = 0;
      }
    }
    memset(sum, 0, sizeof sum);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        sum[i + 1][j + 1] = sum[i + 1][j] + b[i][j];
      }
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        sum[i + 1][j] += sum[i][j];
      }
    }

    int ans = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        for (int h = 1; i + h <= H; h++) {
          int ok = 0, ng = W + 1;
          while (abs(ok - ng) > 1) {
            int mid = (ok + ng) / 2;
            if (S(j, i, j + mid - 1, i + h - 1) == 0)
              ok = mid;
            else
              ng = mid;
          }
          ans = max(ans, h * ok);
        }
      }
    }
    printf("%d\\n", ans);
  }
}
Problem p00116 (C++) — buggy submission, verdict: Time Limit Exceeded

// 32
#include <algorithm>
#include <iostream>
#include <stack>
#include <utility>

using namespace std;

int main() {
  for (int h, w; cin >> h >> w, h | w;) {
    char g[500][501];
    for (int y = 0; y < h; y++) {
      cin >> g[y];
    }
    int m = 0;
    int a[501] = {};
    for (int y = 0; y < h; y++) {
      stack<pair<int, int>> s;
      for (int x = 0; x < w + 1; x++) {
        a[x] = (g[y][x] == '.') ? a[x] + 1 : 0;
        int l = x;
        while (!s.empty() && a[x] < s.top().first) {
          m = max(m, s.top().first * (x - s.top().second));
          l = s.top().second;
          s.pop();
        }
        s.push(make_pair(a[x], l));
      }
    }
    cout << m << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  for (int h, w; cin >> h >> w, h | w;) {
    int g[501][501] = {{}};
    int ans = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        char c;
        cin >> c;
        g[y + 1][x + 1] = g[y][x + 1] + g[y + 1][x] - g[y][x] + (c == '*');
        for (int i = 0; i <= y + 1; i++) {
          for (int j = 0; j <= x + 1; j++) {
            int s = (y + 1 - i) * (x + 1 - j);
            if (s > ans) {
              if (g[y + 1][x + 1] - g[y + 1][j] - g[i][x + 1] + g[i][j] == 0) {
                ans = s;
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00116 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

int main(void) {
  int H, W;
  while (cin >> H >> W) {
    if (H == 0)
      break;
    vector<string> grid(H);
    int sum[501][501] = {0};
    REP(i, H) { cin >> grid[i]; }
    REP(y, H) REP(x, W) {
      sum[y + 1][x + 1] = sum[y + 1][x] + sum[y][x + 1] - sum[y][x];
      sum[y + 1][x + 1] += (grid[y][x] == '*') ? 1 : 0;
    }
    int ans = 0;
    /*
    REP(y1,H)REP(x1,W)FOR(y2,y1+1,H)FOR(x2,x1+1,W){
      if(sum[x2+1][y2+1]-sum[x1][y2+1]
          -sum[x2+1][y1]+sum[x1][y1]==0){
        ans = max((x2-x1+1)*(y2-y1+1),ans);
      }
    }
    */
    bool f = false;
    FOR(dy, 1, H + 1) {
      FOR(dx, 1, W + 1) {
        f = false;
        FOR(y, dy - 1, H) {
          FOR(x, dx - 1, W) {
            if (sum[y + 1][x + 1] - sum[y + 1 - dy][x + 1] -
                    sum[y + 1][x + 1 - dx] + sum[y + 1 - dy][x + 1 - dx] ==
                0) {
              f = true;
              break;
            }
          }
          if (f)
            break;
        }
        if (f)
          ans = max(ans, dx * dy);
        else
          break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

int main(void) {
  int H, W;
  while (cin >> H >> W) {
    if (H == 0)
      break;
    vector<string> grid(H);
    int sum[501][501] = {0};
    REP(i, H) { cin >> grid[i]; }
    REP(y, H) REP(x, W) {
      sum[y + 1][x + 1] = sum[y + 1][x] + sum[y][x + 1] - sum[y][x];
      sum[y + 1][x + 1] += (grid[y][x] == '*') ? 1 : 0;
    }
    int ans = 0;
    REP(y1, H) REP(x1, W) FOR(y2, y1 + 1, H) FOR(x2, x1 + 1, W) {
      if (sum[x2 + 1][y2 + 1] - sum[x1][y2 + 1] - sum[x2 + 1][y1] +
              sum[x1][y1] ==
          0) {
        ans = max((x2 - x1 + 1) * (y2 - y1 + 1), ans);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00116 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<VI> VVI;

int main() {
  int h, w;
  while (cin >> h >> w && h && w) {
    VS lines;
    string line;
    for (int i = 0; i < h; ++i) {
      cin >> line;
      lines.push_back(line);
    }
    VS M = lines;

    VVI W(h, VI(w, 0));
    VVI H(h, VI(w, 0));
    for (int i = 0; i < h; ++i) {
      int t = 0;
      for (int j = w - 1; j >= 0; --j) {
        if (M[i][j] == '*') {
          t = 0;
        } else {
          t++;
          W[i][j] = t;
        }
      }
    }

    int answer = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (M[i][j] == '*')
          continue;
        int width = W[i][j];
        for (int k = 0; i + k < h; ++k) {
          width = min(width, W[i + k][j]);
          answer = max(answer, (k + 1) * width);
        }
      }
    }
    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

long long gcd(long long x, long long y) {
  if (x < y)
    swap(x, y);
  return y == 0 ? (x ? x : 1) : gcd(y, x % y);
}

long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }

long long calc(long long a, long long m) {
  long long t = 0;
  long long x = 1;
  do {
    x = (a * x) % m;
    t++;
  } while (x != 1);
  return t;
}

int main() {
  long long a[3];
  long long m[3];
  long long t[3];
  while (true) {
    for (int i = 0; i < 3; ++i) {
      cin >> a[i] >> m[i];
    }
    if (a[0] == 0 && m[0] == 0 && a[1] == 0 && m[1] == 0 && a[2] == 0 &&
        m[2] == 0)
      break;
    for (int i = 0; i < 3; ++i) {
      t[i] = calc(a[i], m[i]);
    }
    cout << lcm(t[0], lcm(t[1], t[2])) << endl;
  }
  return 0;
}
Problem p00117 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)

struct e {
  int f, t, c;
};

int main() {
  int n, m, x[2], y1, y2, a, b, c, d, cost;
  int di[20];
  e l[800];
  const int inf = 9999999;
  scanf("%d%d", &n, &m);
  rep(i, m) {
    scanf("%d,%d,%d,%d", &a, &b, &c, &d);
    a--;
    b--;
    l[i * 2].f = l[i * 2 + 1].t = a;
    l[i * 2].t = l[i * 2 + 1].f = b;
    l[i * 2].c = c;
    l[i * 2 + 1].c = d;
  }
  scanf("%d,%d,%d,%d", x, x + 1, &y1, &y2);
  cost = 0;
  rep(i, 2) {
    rep(j, n) di[j] = inf;
    di[x[i] - 1] = 0;
    int f = 1;
    while (f) {
      f = 0;
      rep(j, m * 2) {
        e t = l[j];
        if (di[t.f] < inf && di[t.t] > di[t.f] + t.c) {
          di[t.t] = di[t.f] + t.c;
          f = 1;
        }
      }
    }
    cost += di[x[1 - i] - 1];
  }
  printf("%d\\n", y1 - y2 - cost);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)

struct e {
  int f, t, c;
};

int main() {
  int n, m, x[2], y1, y2, a, b, c, d, cost;
  int di[200];
  e l[8000];
  const int inf = 9999999;
  scanf("%d%d", &n, &m);
  rep(i, m) {
    scanf("%d,%d,%d,%d", &a, &b, &c, &d);
    a--;
    b--;
    l[i * 2].f = l[i * 2 + 1].t = a;
    l[i * 2].t = l[i * 2 + 1].f = b;
    l[i * 2].c = c;
    l[i * 2 + 1].c = d;
  }
  cost = 0;
  rep(i, 2) {
    rep(j, n) di[j] = inf;
    di[x[i] - 1] = 0;
    int f = 1;
    while (f) {
      f = 0;
      rep(j, m * 2) {
        e t = l[j];
        if (di[t.f] < inf && di[t.t] > di[t.f] + t.c) {
          di[t.t] = di[t.f] + t.c;
          f = 1;
        }
      }
    }
    cost += di[x[1 - i] - 1];
  }
  printf("%d\\n", y1 - y2 - cost);
  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;
int K[32][32];

int mini(int s, int t) {
  if (s > t) {
    return t;
  }
  return s;
}

int main() {
  for (int i = 0; i < 32; ++i) {
    for (int j = 0; j < 32; ++j) {
      K[i][j] = 100100100;
    }
  }

  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; ++i) {
    scanf("%d,%d,%d,%d", &A, &B, &C, &D);
    K[A][B] = C;
    K[B][A] = D;
    // cerr << "read " <<A<<" " << B << " " << C <<" "<< D <<endl;
  }
  for (int i = 1; i < N + 1; ++i) {
    for (int j = 0; j < N + 1; ++j) {
      for (int k = 0; k < N + 1; ++k) {
        K[j][k] = mini(K[j][i] + K[i][k], K[j][k]);
      }
    }
  }
  scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2);
  // cout << K[x1][x2];
  cout << y1 - y2 - K[x1][x2] - K[x2][x1] << endl;
}

Error: -11

Fixed submission:

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

int mini(int s, int t) {
  if (s > t) {
    return t;
  }
  return s;
}

int main() {
  for (int i = 0; i < 32; ++i) {
    for (int j = 0; i < 32; ++j) {
      K[i][j] = 1001001001;
    }
  }

  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; ++i) {
    scanf("%d,%d,%d,%d", &A, &B, &C, &D);
    K[A][B] = C;
    K[B][A] = D;
    // cerr << "read " <<A<<" " << B << " " << C <<" "<< D <<endl;
  }
  for (int i = 1; i < N + 1; ++i) {
    for (int j = 0; j < N + 1; ++j) {
      for (int k = 0; k < N + 1; ++k) {
        K[j][k] = mini(K[j][i] + K[i][k], K[j][k]);
      }
    }
  }
  scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2);
  cerr << y2 - y1 - K[x1][x2] << endl;
}
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;
int K[32][32];
const int inf = 1001001001;

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 - 1][B - 1] = C;
    K[B - 1][A - 1] = D;
  }

  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (K[i][j] > (K[i][k] + K[k][j])) {
          K[i][j] = K[i][k] + K[k][j];
        }
      }
    }
  }
  scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2);

  cout << y1 - K[x1 - 1][x2 - 1] - y2 - K[x2 - 1][x1 - 1] << endl;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;
int N, M, A, B, C, D, x1, x2, y1, y2;
int K[32][32];
const int inf = 1001001001;

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 - 1][B - 1] = C;
    K[B - 1][A - 1] = D;
  }

  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (K[i][j] > (K[i][k] + K[k][j])) {
          K[i][j] = K[i][k] + K[k][j];
        }
      }
    }
  }
  scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2);

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

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

#pragma warning(disable : 4996)

int x[30][30], D[30];
int N, M, a, b, c, d;
int s, t, p, q;

int gr(int st, int go) {
  for (int i = 0; i < 30; i++) {
    D[i] = 1145141919;
  }
  D[st] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= N; k++) {
        if (x[j][k] < 1145141919) {
          D[k] = min(D[k], D[j] + x[j][k]);
        }
      }
    }
  }
  return D[go];
}

int main() {
  while (cin >> N) {
    cin >> M;
    for (int i = 0; i < 30; i++) {
      for (int j = 0; j < 30; j++) {
        x[i][j] = 1145141919;
      }
    }
    for (int i = 0; i < M; i++) {
      scanf("%d,%d,%d,%d", &a, &b, &c, &d);
      x[a][b] = c;
      x[b][a] = d;
    }
    scanf("%d,%d,%d,%d", &t, &s, &p, &q);

    cout << p - q - gr(s, t) - gr(t, s) << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

#pragma warning(disable : 4996)

int x[30][30], D[30];
int N, M, a, b, c, d;
int s, t, p, q;

int main() {
  cin >> N;
  cin >> M;
  for (int i = 0; i < M; i++) {
    scanf("%d,%d,%d,%d", a, b, c, d);
    x[a][b] = c;
    x[b][a] = d;
  }
  cin >> s >> t >> p >> q;
  for (int i = 0; i < 30; i++) {
    D[i] = 1145141919;
  }
  D[s] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= N; k++) {
        if (x[j][k] != 0) {
          D[k] = min(D[k], D[j] + x[j][k]);
        }
      }
    }
  }
  cout << p - q - D[t] << endl;
  return 0;
}
Problem p00117 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
#define pInt pair<int, int>

struct asc {
  bool operator()(const pInt &x, const pInt &y) const {
    return x.second > y.second;
  }
};

priority_queue<pInt, vector<pInt>, asc> pq;
int dijkstra(int *cost, int terminal, int n, vector<vector<int>> vc) {
  while (!pq.empty()) {
    pInt p = pq.top();
    pq.pop();
    rep(i, n) {
      if (vc[p.first][i] != -1) {
        int total = p.second + vc[p.first][i];
        if (total < cost[i]) {
          pq.push(make_pair(i, total));
          cost[i] = total;
        }
      }
    }
  }
  return cost[terminal];
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> vc(n);
  rep(i, n) {
    vector<int> va(n);
    vc[i] = va;
    rep(j, n) { vc[i][j] = -1; }
  }
  rep(i, m) {
    int s, t, c1, c2;
    scanf("%d,%d,%d,%d", &s, &t, &c1, &c2);
    vc[--s][--t] = c1;
    vc[t][s] = c2;
  }
  int s, g, a, b;
  scanf("%d,%d,%d,%d", &s, &g, &a, &b);
  --s, --g;
  int cost[20] = {};

  rep(i, n) {
    if (i == s)
      cost[i] = 0;
    else
      cost[i] = INT_MAX;
  }
  pq.push(make_pair(s, 0));
  int ret = 0;
  ret += dijkstra(cost, g, n, vc);

  rep(i, n) {
    if (i == g)
      cost[i] = 0;
    else
      cost[i] = INT_MAX;
  }
  pq.push(make_pair(g, 0));
  ret += dijkstra(cost, s, n, vc);

  cout << a - b - ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
int ret = INT_MAX;
int s, g;
void dfs(vector<vector<int>> vc, int cost, int v, bool f,
         vector<bool> &visited) {
  if (ret < cost)
    return;
  if (f && v == s) {
    if (ret > cost)
      ret = cost;
    return;
  }
  if (!f && v == g) {
    f = true;
    rep(i, visited.size()) { visited[i] = false; }
  }
  rep(i, vc[v].size()) {
    if (vc[v][i] != -1 && !visited[i]) {
      visited[i] = true;
      dfs(vc, cost + vc[v][i], i, f, visited);
      visited[i] = false;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> vc(n);
  rep(i, n) {
    vector<int> va(n);
    vc[i] = va;
    rep(j, n) { vc[i][j] = -1; }
  }
  rep(i, m) {
    int s, t, c1, c2;
    scanf("%d,%d,%d,%d", &s, &t, &c1, &c2);
    vc[--s][--t] = c1;
    vc[t][s] = c2;
  }
  int sum, cost;
  scanf("%d,%d,%d,%d", &s, &g, &sum, &cost);
  --s, --g;
  vector<bool> visited(n);
  rep(i, n) { visited[i] = false; }
  dfs(vc, 0, s, false, visited);
  cout << sum - cost - ret << endl;
  return 0;
}