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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

#define INF 1 << 29

using namespace std;

typedef int Weight;

struct Edge {
  int src;
  int dst;
  Weight weight;

  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

struct Node {
  int index;
  Weight cost;
  int prev;

  Node(int index, Weight cost, int prev)
      : index(index), cost(cost), prev(prev) {}
};

bool operator<(const Node &a, const Node &b) {
  if (a.cost == b.cost) {
    return a.index > b.index;
  } else {
    return a.cost > b.cost;
  }
}

void dijkstra(int s, vector<Weight> &costs, vector<int> &prev,
              vector<vector<Edge>> &edges) {
  int n = costs.size();
  priority_queue<Node> q;

  costs.assign(n, INF);
  prev.assign(n, -1);
  costs[s] = 0;
  q.push(Node(s, 0, -2));

  while (!q.empty()) {
    Node fix = q.top();
    q.pop();
    if (prev[fix.index] != -1)
      continue;

    costs[fix.index] = fix.cost;
    prev[fix.index] = fix.prev;

    for (int i = 0; i < edges[fix.index].size();
         i++) { // foreach edge from fixed
      if (costs[edges[fix.index][i].dst] >
          costs[fix.index] + edges[fix.index][i].weight) {
        q.push(Node(edges[fix.index][i].dst,
                    edges[fix.index][i].weight + costs[fix.index], fix.index));
      }
    }
  }
}

int main() {
  int n, k, req, a, b, c;

  while (cin >> n >> k) {
    if ((n | k) == 0)
      break;
    vector<Weight> costs(n);
    vector<int> prev(n);
    vector<vector<Edge>> edges(n);

    for (int i = 0; i < k; i++) {
      cin >> req;
      if (req) {
        cin >> a >> b >> c;
        bool isexist = false;
        for (int j = 0; j < edges[a - 1].size(); j++) {
          if (edges[a - 1][j].dst == b - 1 && edges[a - 1][j].weight > c) {
            edges[a - 1][j].weight = c;
            isexist = true;
          }
        }
        if (!isexist) {
          edges[a - 1].push_back(Edge(a - 1, b - 1, c));
        }

        isexist = false;
        for (int j = 0; j < edges[b - 1].size(); j++) {
          if (edges[b - 1][j].dst == a - 1 && edges[b - 1][j].weight > c) {
            edges[b - 1][j].weight = c;
            isexist = true;
          }
        }
        if (!isexist) {
          edges[b - 1].push_back(Edge(b - 1, a - 1, c));
        }
      } else {
        cin >> a >> b;
        dijkstra(a - 1, costs, prev, edges);
        if (costs[b - 1] == INF) {
          cout << -1 << endl;
        } else {
          cout << costs[b - 1] << endl;
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

#define INF 1 << 28

int main() {
  int n, k, kind, src, dst, tmp, c;

  while (cin >> n >> k) {
    if (!(n | k))
      break;
    int cost[n][n];
    fill((int *)cost, (int *)cost + n * n, INF);

    for (int i = 0; i < k; i++) {
      cin >> kind;
      if (kind == 0) {
        cin >> src >> dst;
        if (cost[src - 1][dst - 1] == INF) {
          cout << -1 << endl;
        } else {
          cout << cost[src - 1][dst - 1] << endl;
        }
      } else {
        cin >> src >> dst >> c;
        tmp = min(cost[src - 1][dst - 1], c);
        cost[src - 1][dst - 1] = tmp;
        cost[dst - 1][src - 1] = tmp;
        for (int r = 0; r < n; r++) {
          for (int p = 0; p < n; p++) {
            for (int q = 0; q < n; q++) {
              cost[p][q] = min(cost[p][q], cost[p][r] + cost[r][q]);
            }
          }
        }
      }
    }
  }
}