Problem p00244 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define INF (1 << 10)

typedef pair<int, int> P;

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<vector<int>> cost(n, vector<int>(n, INF));
    vector<vector<int>> connect(n, vector<int>(n, 0));

    for (int i = 0; i < n; ++i)
      cost[i][i] = 0;

    for (int i = 0; i < m; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      --a, --b;
      cost[a][b] = cost[b][a] = c;
      connect[a][b] = connect[b][a] = 1;
    }

    vector<vector<int>> prev(n, vector<int>(n));

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; j++)
        prev[i][j] = i;

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        for (int k = 0; k < n; ++k)
          if (cost[j][i] + cost[i][k] < cost[j][k]) {
            prev[j][k] = prev[i][k];
            cost[j][k] = cost[j][i] + cost[i][k];
          }

    int ans = cost[0][n - 1];

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) {
        bool flag = false;
        for (int k = 0; k < n; ++k)
          if (connect[i][k] && connect[k][j])
            flag = true;

        if (!flag)
          continue;

        ans = min(ans, cost[0][i] + cost[j][n - 1]);
      }

    cout << ans << endl;
    /*
                    // 経路復元
                    int from = 0, to = n-1;
                    vector<int> v;
                    v.push_back(from);
                    while (from != to) {
                            from = prev[to][from];
                            v.push_back(from);
                    }

                    // 逆関数
                    map<int, int> pos;
                    for (int i = 0; i < v.size(); ++i)
                            pos[v[i]] = i+1;

                    // 累積和
                    int s = 0;
                    vector<int> sum;

                    sum.push_back(0);
                    for (int i = 0; i < v.size()-1; ++i) {
                            s += cost[v[i]][v[i+1]];
                            sum.push_back(s);
                    }

                    int ans = sum[n-1];
                    for (int i = 0; i < n; ++i)
                            for (int j = 0; j < n; ++j) {
                                    if (!pos[i]) continue;
                                    if (pos[i] >= pos[j]) continue;

                                    bool flag = false;
                                    for (int k = 0; k < n; ++k)
                                            if (connect[i][k] && connect[k][j])
                                                    flag = true;

                                    if (!flag) continue;

                                    ans = min( ans, sum[i] + cost[j][n-1] );
                            }

                    cout << ans << endl;
    */
    /*
                    vector<P> koho;
                    for (int i = 0; i < n; ++i)
                            for (int j = i+1; j < n; ++j)
                                    for (int k = 0; k < n; ++k)
                                            if (i != j && connect[i][k] &&
       connect[k][j]) koho.push_back( P(i, j) );

                    koho.erase( unique( koho.begin(), koho.end() ), koho.end()
       );

                    for (int i = 0; i < n; ++i)
                            for (int j = 0; j < n; ++j)
                                    for (int k = 0; k < n; ++k)
                                            if (cost[i][k] + cost[k][j] <
       cost[i][j]) cost[i][j] = cost[i][k] + cost[k][j];

                    int ans = cost[0][n-1];
                    for (int x = 0; x != koho.size(); ++x) {
                            vector< vector<int> > c = cost;
                            c[koho[x].first][koho[x].second] =
       c[koho[x].second][koho[x].first] = 0; for (int i = 0; i < n; ++i) for
       (int j = 0; j < n; ++j) for (int k = 0; k < n; ++k) if (c[i][k] + c[k][j]
       < c[i][j]) c[i][j] = c[i][k] + c[k][j];

                            if (c[0][n-1] < ans) ans = c[0][n-1];
                    }
    */
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

#define INF (1 << 10)

typedef pair<int, int> P;

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<vector<int>> cost(n, vector<int>(n, INF));
    vector<vector<int>> connect(n, vector<int>(n, 0));

    for (int i = 0; i < n; ++i)
      cost[i][i] = 0;

    for (int i = 0; i < m; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      --a, --b;
      cost[a][b] = cost[b][a] = c;
      connect[a][b] = connect[b][a] = 1;
    }

    vector<P> koho;
    for (int i = 0; i < n; ++i)
      for (int j = i + 1; j < n; ++j)
        for (int k = 0; k < n; ++k)
          if (i != j && connect[i][k] && connect[k][j])
            koho.push_back(P(i, j));

    koho.erase(unique(koho.begin(), koho.end()), koho.end());

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        for (int k = 0; k < n; ++k)
          if (cost[i][k] + cost[k][j] < cost[i][j])
            cost[i][j] = cost[i][k] + cost[k][j];

    int ans = cost[0][n - 1];
    for (int x = 0; x != koho.size(); ++x) {
      vector<vector<int>> c = cost;
      c[koho[x].first][koho[x].second] = c[koho[x].second][koho[x].first] = 0;
      for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
          for (int k = 0; k < n; ++k)
            if (c[i][k] + c[k][j] < c[i][j])
              c[i][j] = c[i][k] + c[k][j];

      if (c[0][n - 1] < ans)
        ans = c[0][n - 1];
    }

    cout << ans << endl;
  }
}