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


#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;

int cost[200][200];
int d[200];
bool used[200];
int V;

void dijkstra(int s) {
  fill(d, d + V, INF);
  fill(used, used + V, false);
  d[s] = 0;

  while (true) {
    //    cout << "a";
    int v = -1;

    rep(u, 0, V) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;
    rep(u, 0, V) { d[u] = min(d[u], d[v] + cost[v][u]); }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  while (true) {
    rep(j, 0, 105) { rep(k, 0, 105) cost[j][k] = INF; }
    cin >> n >> k;
    V = n;
    if (!n && !k)
      break;
    rep(i, 0, k) {
      int a, b, c, l;
      cin >> a >> b >> c;
      b--;
      c--;
      if (a == 1) {
        cin >> l;
        cost[b][c] = min(cost[b][c], l);
        cost[c][b] = min(cost[c][b], l);
      } else {
        dijkstra(b);
        if (d[c] == INF)
          cout << -1 << endl;
        else
          cout << d[c] << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:



/*
        ?????¨NANA????????§?\\????

 */
#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;

int n, k, shima[105][105];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    cin >> n >> k;
    rep(i, 0, n) {
      rep(j, 0, n) {
        if (i == j)
          shima[i][j] = 0;
        else
          shima[i][j] = INF;
      }
    }
    if (n == 0 && k == 0)
      break;
    rep(i, 0, k) {
      int a, b, c, d;
      cin >> a >> b >> c;
      b--;
      c--;
      if (a) {
        cin >> d;
        shima[b][c] = d;
        shima[c][b] = d;
      } else {
        rep(j, 0, n) {
          rep(f, 0, n) {
            rep(g, 0, n) {
              shima[f][g] = min(shima[f][g], shima[f][j] + shima[j][g]);
            }
          }
        }
        if (shima[b][c] == INF)
          cout << -1;
        else
          cout << shima[b][c] << endl;
      }
    }
  }
  return 0;
}