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

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

struct edge {
  int to, cost;
};

#define P pair<int, int>

int v;
vector<edge> g[100];
long long int d[100];

const long long int INF = 1000000 * 500;

void swap(edge &a, edge &b) {
  edge tmp;
  tmp.to = a.to;
  a.to = b.to;
  b.to = tmp.to;
  tmp.cost = a.cost;
  a.cost = b.cost;
  b.cost = tmp.cost;
}

long long int dijkstra(int s, int a) {

  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + v, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int k = p.second;
    if (d[k] < p.first)
      continue;
    for (int i = 0; i < g[k].size(); i++) {
      edge e = g[k][i];
      if (d[e.to] > d[k] + e.cost) {
        d[e.to] = d[k] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
  if (d[a] != INF) {
    return d[a];
  } else {
    return -1;
  }
}
int main() {

  int k;

  while (1) {

    cin >> v >> k;

    if (v == 0 && k == 0) {
      break;
    }

    for (int i = 0; i < 100; i++) {
      vector<edge>().swap(g[i]);
    }

    int order, a, b, c;

    while (k--) {
      cin >> order;
      if (order == 0) {
        cin >> a >> b;
        a--;
        b--;
        cout << dijkstra(a, b) << endl;
      } else {
        bool flag = false;
        cin >> a >> b >> c;
        a--;
        b--;
        /*  for(int i=0; i<g[a].size(); i++){
            if(g[a][i].to == b){
              if(g[a][i].cost > c){
                g[a][i].cost = c;
                for(int j=0; j<g[i].size(); j++){
                  if(g[i][j].to == a){
                    g[i][j].cost = c;
                    break;
                  }
                }
                flag = true;
                break;
              }
            }
          }
          if(!flag){*/
        edge ed;
        ed.to = b;
        ed.cost = c;
        g[a].push_back(ed);
        ed.to = a;
        g[b].push_back(ed);
        //  }
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
//??\\??????????????¨?????????
const long int INF = 1000000 * 100 + 100;

int main() {

  while (1) {

    int n, k, order, a, b, c, d, e;

    cin >> n >> k;

    if (n == 0 && k == 0) {
      break;
    }

    vector<vector<int>> table;

    table.resize(n);

    for (int i = 0; i < n; i++) {
      table[i].resize(n);
    }

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

    vector<vector<int>> froid(table);

    for (int i = 0; i < k; i++) {
      cin >> order;

      if (order == 0) {
        cin >> a >> b;
        if (froid[a - 1][b - 1] == INF) {
          cout << -1 << endl;
        } else {
          cout << froid[a - 1][b - 1] << endl;
        }
      } else {
        cin >> c >> d >> e;
        if (table[c - 1][d - 1] > e) {
          table[c - 1][d - 1] = e;
        }
        if (table[d - 1][c - 1] > e) {
          table[d - 1][c - 1] = e;
        }
        froid = table;
        for (int r = 0; r < n; r++) {
          for (int ii = 0; ii < n; ii++) {
            for (int j = 0; j < n; j++) {
              if (froid[ii][j] > froid[ii][r] + froid[r][j]) {
                froid[ii][j] = froid[ii][r] + froid[r][j];
              }
            }
          }
        }
        /*for(int i=0;i<n+1;i++){
          for(int j=0;j<n+1;j++){
            cout << table[i][j] << " ";
          }
          cout << endl;
        }*/
      }
    }
  }
  return 0;
}