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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
int dis[50][50];
int main() {
  int m, a, ans, b, oop, n, op;
  while (~scanf("%d", &n) && n) {
    m = 0;
    for (int i = 0; i < 20; i++) {
      for (int j = 0; j < 20; j++)
        dis[i][j] = INF;
    }
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &a, &b);
      scanf("%d", &dis[a][b]);
      dis[b][a] = dis[a][b];
      if (m < a || m < b) {
        if (a < b)
          m = b;
        else
          m = a;
      }
    }
    for (int i = 0; i <= m; i++)
      dis[i][i] = 0;
    for (int k = 0; k <= m; k++)
      for (int i = 0; i <= m; i++)
        for (int j = 0; j <= m; j++) {
          if (dis[i][j] > dis[i][k] + dis[k][j]) {
            dis[i][j] = dis[i][k] + dis[k][j];
            dis[j][i] = dis[i][k] + dis[k][j];
          }
        }
    oop = INF;
    for (int i = 0; i <= m; i++) {
      ans = 0;
      for (int u = 0; u <= m; u++) {
        if (dis[i][u] != INF)
          ans += dis[i][u];
      }
      if (ans < oop) {
        op = i;
        oop = ans;
      }
    }
    printf("%d %d\\n", op, oop);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#define MAX 0x0fffffff

int map[15][15];
int maxi;

int main() {
  int n, i, j, k;
  int maxx, temp;
  scanf("%d", &n);
  while (n) {
    maxi = 0;
    for (i = 0; i < 15; i++) {
      for (j = 0; j < 15; j++) {
        map[i][j] = (i != j) * MAX;
      }
    }

    for (temp = 0; temp < n; temp++) {
      scanf("%d%d%d", &i, &j, &k);
      if (i > maxi)
        maxi = i;
      if (j > maxi)
        maxi = j;
      map[i][j] = map[j][i] = k;
    }
    for (k = 0; i <= maxi; i++) {
      for (i = 0; j <= maxi; j++) {
        for (j = 0; k <= maxi; k++) {
          if (map[i][j] > map[i][k] + map[k][j])
            map[i][j] = map[i][k] + map[k][j];
        }
      }
    }
    for (i = 0; i <= maxi; i++) {
      for (j = 1; j <= maxi; j++) {
        map[i][0] += map[i][j];
      }
    }
    for (i = 0, maxx = MAX; i <= maxi; i++) {
      if (map[i][0] < maxx) {
        maxx = map[i][0];
        temp = i;
      }
    }
    printf("%d %d\\n", temp, maxx);
    scanf("%d", &n);
  }
  return 0;
}
Problem p00189 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define SIZE 100005
#define MAX_V 100001
#define INF (1e9 + 1)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int g[10][10];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        if (i == j)
          g[i][j] = 0;
        else
          g[i][j] = 999999999;
      }
    }
    int t = 0;
    for (int i = 0; i < a; i++) {
      int c, d, e;
      scanf("%d%d%d", &c, &d, &e);
      if (t < c)
        t = c;
      if (t < d)
        t = d;
      g[c][d] = e;
      g[d][c] = e;
    }
    t++;
    for (int k = 0; k < t; k++) {
      for (int i = 0; i < t; i++) {
        for (int j = 0; j < t; j++) {
          if (g[i][k] + g[k][j] < g[i][j])
            g[i][j] = g[i][k] + g[k][j];
        }
      }
    }
    int n = 0;
    ll ans = 9999999999;
    for (int i = 0; i < t; i++) {
      ll now = 0;
      for (int j = 0; j < t; j++)
        now += g[i][j];
      if (ans > now) {
        ans = now;
        n = i;
      }
    }
    printf("%d %lld\\n", n, ans);
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define SIZE 100005
#define MAX_V 100001
#define INF (1e9 + 1)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int g[10][10];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i, 10; i++) {
      for (int j = 0; j < 10; j++) {
        if (i == j)
          g[i][j] = 0;
        else
          g[i][j] = 999999999;
      }
    }
    int t = 0;
    for (int i = 0; i < a; i++) {
      int c, d, e;
      scanf("%d%d%d", &c, &d, &e);
      if (t < c)
        t = c;
      if (t < d)
        t = d;
      g[c][d] = e;
      g[d][c] = e;
    }
    t++;
    for (int k = 0; k < t; k++) {
      for (int i = 0; i < t; i++) {
        for (int j = 0; j < t; j++) {
          if (g[i][k] + g[k][j] < g[i][j])
            g[i][j] = g[i][k] + g[k][j];
        }
      }
    }
    int n = 0;
    ll ans = 9999999999;
    for (int i = 0; i < t; i++) {
      ll now = 0;
      for (int j = 0; j < t; j++)
        now += g[i][j];
      if (ans > now) {
        ans = now;
        n = i;
      }
    }
    printf("%d %lld\\n", n, ans);
  }
}
Problem p00189 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define INF 200000000
int main() {
  int e;
  while (cin >> e && e) {
    int i, j, t, k, v = 0, ans = INF;
    int d[10][10];
    int cost_count[10] = {0};
    for (i = 0; i < 10; i++) {
      for (j = 0; j < 10; j++) {
        d[i][j] = INF;
        if (i == j) {
          d[i][j] = 0;
        }
      }
    }
    for (t = 0; t < e; t++) {
      scanf("%d %d %d", &i, &j, &k);
      v = max(v, max(i, j));
      d[i][j] = k;
      d[j][i] = k;
    }
    for (k = 0; k <= v; k++) {
      for (i = 0; i <= v; i++) {
        for (j = 0; j <= v; j++) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
      }
    }
    for (i = 0; i <= v; i++) {
      int cost = 0;
      for (j = 0; j <= v; j++) {
        cost += d[i][j];
      }
      cost_count[i] = cost;
      ans = min(ans, cost_count[i]);
    }
    for (i = 0; i <= v; i++) {
      if (cost_count[i] == ans) {
        printf("%d %d\\n", i, ans);
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define INF 200000000
int main() {
  int e;
  while (cin >> e, e) {
    int i, j, k, s = INF, v = 0, ans = INF;
    int d[10][10];
    int cost_count[10] = {0};
    for (i = 0; i < 10; i++) {
      for (j = 0; j < 10; j++) {
        d[i][j] = INF;
        if (i == j) {
          d[i][j] = 0;
        }
      }
    }
    for (i = 0; i < e; i++) {
      scanf("%d %d %d", &i, &j, &k);
      v = max(v, max(i, j));
      d[i][j] = k;
      d[j][i] = k;
    }
    for (k = 0; k < v; k++) {
      for (i = 0; i < v; i++) {
        for (j = 0; j < v; j++) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
      }
    }
    for (i = 0; i < v; i++) {
      int cost = 0;
      for (j = 0; j < v; j++) {
        cost += d[i][j];
      }
      cost_count[i] = cost;
      ans = min(ans, cost_count[i]);
    }
    for (i = 0; i < v; i++) {
      if (cost_count[i] == ans) {
        printf("%d %d\\n", i, ans);
        break;
      }
    }
  }
  return 0;
}
Problem p00189 (C++) — buggy submission, verdict: Runtime Error

// 2014/06/16 Tazoe

#include <iostream>
using namespace std;

const int INF = 1000000000;

int main() {
  while (true) {
    int n;
    cin >> n;

    if (n == 0)
      break;

    int T[10][10];
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        if (i == j) {
          T[i][j] = 0;
        } else {
          T[i][j] = INF;
        }
      }
    }

    int m = 0;

    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b >> c;

      T[a][b] = T[b][a] = c;

      if (a + 1 > m) {
        m = a + 1;
      }
      if (b + 1 > m) {
        m = b + 1;
      }
    }
    //???????????§???????????????
    //????????????????????????
    for (int k = 0; k < m; k++) {
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          if (T[i][j] > T[i][k] + T[k][j]) {
            T[i][j] = T[i][k] + T[k][j];
          }
        }
      }
    }
    //????°???????????????¢???
    int town;    //????????????
    int x = INF; //????????????
    for (int i = 0; i < m; i++) {
      int r = 0; // i?????§???????????????
      for (int j = 0; j < m; j++) {
        r = r + T[i][j];
      }
      if (r < x) {
        x = r;
        town = i;
      }
    }
    cout << town << ' ' << x << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

// 2014/06/16 Tazoe

#include <iostream>
using namespace std;

const int INF = 1000000000;

int main() {
  while (true) {
    int n;
    cin >> n;

    if (n == 0)
      break;

    int T[10][10];
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        if (i == j) {
          T[i][j] = 0;
        } else {
          T[i][j] = INF;
        }
      }
    }

    int m = 0;

    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b >> c;

      T[a][b] = T[b][a] = c;

      //		if(a+1>m){
      //			m = a+1;
      //		}
      //		if(b+1>m){
      //			m = b+1;
      //		}
    }
    //???????????§???????????????
    //????????????????????????
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (T[i][j] > T[i][k] + T[k][j]) {
            T[i][j] = T[i][k] + T[k][j];
          }
        }
      }
    }
    //????°???????????????¢???
    int town;       //????????????
    int x = 100010; //????????????
    for (int i = 0; i < n; i++) {
      int r = 0; // i?????§???????????????
      for (int j = 0; j < n; j++) {
        r = r + T[i][j];
      }
      if (r < x) {
        x = r;
        town = i;
      }
    }
    cout << town << ' ' << x << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define INF (1 << 20)
typedef pair<int, int> P;

vector<P> edge[10];

int dijkstre(int pos) {
  int min_cost[10];
  fill_n(min_cost, 10, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, pos));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int cost = p.first;
    int now = p.second;
    if (min_cost[now] < cost)
      continue;
    min_cost[now] = cost;
    for (int i = 0; i < edge[now].size(); i++) {
      int nc = edge[now][i].second;
      int next = edge[now][i].first;
      if (cost + nc < min_cost[next]) {
        que.push(P(cost + nc, next));
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < 10; i++) {
    if (min_cost[i] != INF) {
      sum += min_cost[i];
    }
  }
  return sum;
}

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int l = 100, r = 0;
    for (int i = 0; i < 10; i++) {
      edge[i].clear();
    }
    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      edge[a].push_back(P(b, c));
      edge[b].push_back(P(a, c));
      l = min(min(l, a), b);
      r = max(max(r, a), b);
    }
    int ans = INF;
    int pos = l;
    for (int i = l; i <= r; i++) {
      int tmp = dijkstre(i);
      if (ans > tmp) {
        pos = i;
        ans = tmp;
      }
    }
    cout << pos << " " << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF (1 << 20)
typedef pair<int, int> P;

vector<P> edge[10];

int dijkstre(int pos) {
  int min_cost[10];
  fill_n(min_cost, 10, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, pos));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int cost = p.first;
    int now = p.second;
    if (min_cost[now] < cost)
      continue;
    min_cost[now] = cost;
    for (int i = 0; i < edge[now].size(); i++) {
      int nc = edge[now][i].second;
      int next = edge[now][i].first;
      que.push(P(cost + nc, next));
    }
  }
  int sum = 0;
  for (int i = 0; i < 10; i++) {
    if (min_cost[i] != INF) {
      sum += min_cost[i];
    }
  }
  return sum;
}

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int l = 100, r = 0;
    for (int i = 0; i < 10; i++) {
      edge[i].clear();
    }
    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      edge[a].push_back(P(b, c));
      edge[b].push_back(P(a, c));
      l = min(min(l, a), b);
      r = max(max(r, a), b);
    }
    int ans = INF;
    int pos = l;
    for (int i = l; i <= r; i++) {
      int tmp = dijkstre(i);
      if (ans > tmp) {
        pos = i;
        ans = tmp;
      }
    }
    cout << pos << " " << ans << endl;
  }
}
Problem p00190 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int to[13][5] = {{2, -1},           {2, 5, -1},        {0, 1, 3, 6, -1},
                 {2, 7, -1},        {5, -1},           {1, 4, 6, 9, -1},
                 {2, 5, 7, 10, -1}, {3, 6, 8, 11, -1}, {7, -1},
                 {5, 10, -1},       {6, 9, 11, 12},    {7, 10, -1},
                 {10, -1}};
int X[] = {2, 1, 2, 3, 0, 1, 2, 3, 4, 1, 2, 3, 2};
int Y[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4};

int solve(vector<int> data) {
  queue<vector<int>> Q;
  map<vector<int>, int> done;
  Q.push(data);
  done[Q.front()] = 0;

  while (Q.size()) {
    vector<int> q = Q.front();
    Q.pop();
    int dif = 0;
    rep(i, 13) if (q[i]) dif += abs(X[q[i]] - X[i]) + abs(Y[q[i]] - Y[i]);
    if (dif == 0)
      return done[q];
    ;
    if (dif + done[q] > 20)
      continue;
    int curcost = done[q];
    rep(x, 13) {
      if (q[x] == 0) {
        rep(i, 5) {
          if (!~to[x][i])
            break;
          swap(q[x], q[to[x][i]]);
          if (done.find(q) == done.end()) {
            done[q] = curcost + 1;
            Q.push(q);
          }
          swap(q[x], q[to[x][i]]);
        }
      }
    }
  }
  return 21;
}
int main() {
  vector<int> data(13);
  while (1) {
    rep(i, 13) if (!(cin >> data[i])) return 0;
    int ans = solve(data);
    if (ans > 20)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int to[13][5] = {{2, -1},           {2, 5, -1},        {0, 1, 3, 6, -1},
                 {2, 7, -1},        {5, -1},           {1, 4, 6, 9, -1},
                 {2, 5, 7, 10, -1}, {3, 6, 8, 11, -1}, {7, -1},
                 {5, 10, -1},       {6, 9, 11, 12},    {7, 10, -1},
                 {10, -1}};
int X[] = {2, 1, 2, 3, 0, 1, 2, 3, 4, 1, 2, 3, 2};
int Y[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4};

int solve(vector<int> data) {
  queue<vector<int>> Q;
  map<vector<int>, int> done;
  Q.push(data);
  done[Q.front()] = 0;

  while (Q.size()) {
    vector<int> q = Q.front();
    Q.pop();
    int dif = 0;
    rep(i, 13) if (q[i]) dif += abs(X[q[i]] - X[i]) + abs(Y[q[i]] - Y[i]);
    if (dif == 0)
      return done[q];
    ;
    if (dif / 2 + done[q] >= 20)
      continue;
    int curcost = done[q];
    rep(x, 13) {
      if (q[x] == 0) {
        rep(i, 5) {
          if (!~to[x][i])
            break;
          swap(q[x], q[to[x][i]]);
          if (done.find(q) == done.end()) {
            done[q] = curcost + 1;
            Q.push(q);
          }
          swap(q[x], q[to[x][i]]);
        }
      }
    }
  }
  return 21;
}
int main() {
  vector<int> data(13);
  while (1) {
    rep(i, 13) if (!(cin >> data[i])) return 0;
    int ans = solve(data);
    if (ans > 20)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
Problem p00191 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#define INF 0
using namespace std;
int main() {
  int n, m;
  double t[200][200];
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        cin >> t[i][j];
    double dp[200][200];
    for (int i = 0; i <= m; i++)
      for (int j = 0; j < n; j++)
        dp[j][i] = INF;
    for (int i = 0; i < n; i++)
      dp[i][0] = 1.0;
    for (int i = 1; i < m; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++)
          dp[j][i] = max(dp[j][i], dp[k][i - 1] * t[k][j]);
    double ans = INF;
    for (int i = 0; i < n; i++)
      ans = max(ans, dp[i][m - 1]);
    printf("%.2lf\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int n, m;
double ma, s[110][110];
void func(int pre, int cnt, double now) {
  cnt++;
  if (cnt != m) {
    for (int i = 0; i < n; i++) {
      func(i, cnt, now * s[pre][i]);
    }
  } else {
    if (ma < now)
      ma = now;
  }
}
int main() {
  while (cin >> n >> m, n || m) {
    ma = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        scanf("%lf", &s[i][j]);
    for (int i = 0; i < n; i++)
      func(i /*OñÌÔ*/, 0 /*JEg*/,
           1.0 /*»ÝÌ¬·*/);
    printf("%.2lf\\n", ma);
    ;
  }
  return 0;
}
Problem p00191 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
double g[100][100], dp[100][100];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n || m) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        scanf("%lf", &g[i][j]);

    fill_n(dp[0], 100, 1.0);
    fill_n(dp[1], 9900, 0.0);
    for (int i = 1; i < m; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++)
          dp[i][j] = max(dp[i][j], dp[i - 1][k] * g[k][j]);

    printf("%.2f\\n", *max_element(dp[m - 1], dp[m - 1] + n));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
double g[100][100], dp[100][100];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n || m) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        scanf("%lf", &g[i][j]);

    fill_n(dp[0], 100, 1.0);
    fill_n(dp[1], 9900, 0.0);
    for (int i = 1; i < m; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++)
          dp[i][j] = max(dp[i][j], dp[i - 1][k] * g[k][j]);

    printf("%.2f\\n", *max_element(dp[m - 1], dp[m - 1] + n));
  }
  return 0;
}
Problem p00191 (C++) — buggy submission, verdict: Time Limit Exceeded

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

double memo[101][102];
double mp[101][101];
double ans;
int n, m;
int cnt;

void saiki(double seki, double pseki, int mae, int kai) {
  //  cout <<"seki="<<seki<<" mae="<<mae<<" kai="<<kai<<endl;
  cnt++;

  if (kai == m && ans < seki)
    ans = seki;
  if (kai == m && mae != -1)
    memo[kai][mae] = 1;

  if (kai < m) {
    for (int i = 1; i <= n; i++) {
      if (memo[kai + 1][i] == -1) {
        saiki(seki * mp[mae][i], seki, i, kai + 1);
        if (memo[kai][mae] < memo[kai + 1][i] * mp[mae][i])
          memo[kai][mae] = memo[kai + 1][i] * mp[mae][i];
      } else {
        if (memo[kai][mae] < memo[kai + 1][i] * mp[mae][i])
          memo[kai][mae] = memo[kai + 1][i] * mp[mae][i];
        saiki(memo[kai][mae] * seki, -1, -1, m);
      }
    }
  }
}

int main() {

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++)
        if (i == 0 || j == 0)
          mp[i][j] = 1;
        else
          cin >> mp[i][j];

    for (int i = 0; i <= m; i++)
      for (int j = 0; j <= n; j++)
        memo[i][j] = -1;

    ans = 0;

    int ans2 = 0;
    for (int i = 1; i <= n; i++) {
      saiki(1, 1, i, 1);
      for (int j = 0; j <= n; j++) {
        if (ans2 < memo[1][i])
          ans2 = memo[1][i];
        memo[1][i] = memo[2][i] = -1;
      }
      for (int j = 0; j <= m; j++)
        for (int k = 0; k <= n; k++)
          memo[j][k] = -1;
    }

    //    cout << ans2 << endl;
    printf("%.2f\\n", ans);
    // cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
double mp[101][101];
double ans;
int n, m;
int sta[101];
int flg;
int cnt;

void saiki(double sum, int mae, int kai) {
  cnt++;
  if (kai == m && sum > ans)
    ans = sum;

  if (kai < m) {
    for (int i = 0; i < n; i++) {
      saiki(sum * mp[mae][i], i, kai + 1);
    }
  }
}

int main() {

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        cin >> mp[i][j];

    ans = 0;
    for (int i = 0; i < n; i++)
      saiki(1, i, 1);

    printf("%.2f\\n", ans);
    // cout << cnt <<endl;
  }

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

#include <algorithm>
#include <climits>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

typedef vector<double> VD;
typedef vector<VD> VVD;

VVD C; // cost
VD prev;
VD next;
int n; // ì¿ÌíÞ
int m; // ì¿ð^¦éñ
double answer;

void solve() {
  prev = VD(n, 1.0);
  next = VD(n);

  for (int k = 1; k < m; k++) {
    for (int i = 0; i < n; i++) {
      next[i] = -1.0;
      for (int j = 0; j < n; j++) {
        double tmp = prev[j] * C[j][i];
        next[i] = max(next[i], tmp);
      }
    }
    prev = next;
  }

  answer = -1.0;
  for (int i = 0; i < n; i++)
    answer = max(answer, prev[i]);
}

int main(void) {
  while (cin >> n >> m && n > 0 && m > 0) {
    C = VVD(n, VD(n));

    // üÍ
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> C[i][j];
      }
    }

    solve();

    cout.setf(ios::fixed);
    cout.precision(2);
    cout << answer << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

typedef vector<double> VD;
typedef vector<VD> VVD;

VVD C;
int n; // ì¿ÌíÞ
int m; // ì¿ð^¦éñ
double answer;
double tmp;

/* æÃ~ÉT[` */
void greedy_search(int prev, int depth) {
  if (m == depth) {
    answer = max(answer, tmp);
    return;
  }

  double max_value = 0.0;
  for (int i = 0; i < n; i++) {
    max_value = max(max_value, C[prev][i]);
  }

  double backup = tmp;
  for (int i = 0; i < n; i++) {
    if (C[prev][i] == max_value) {
      tmp = backup;
      tmp *= C[prev][i];
      greedy_search(i, depth + 1);
    }
  }
}

int main(void) {
  while (cin >> n >> m && n > 0 && m > 0) {
    C = VVD(n, VD(n, 0.0));

    // üÍ
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> C[i][j];
      }
    }

    // Tõ
    answer = 0.0;
    for (int i = 0; i < n; i++) {
      tmp = 1.0;
      greedy_search(i, 1);
    }
    cout.setf(ios::fixed);
    cout.precision(2);
    cout << answer << endl;
  }

  return 0;
}