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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Edge {
  Edge(){};
  Edge(int _to, int _cost) {
    to = _to;
    cost = _cost;
  }
  int to;
  int cost;
};
struct Data {
  Data(){};
  Data(int _pos, int _t) {
    pos = _pos;
    t = _t;
  }
  int pos;
  int t;
  bool operator<(const Data &a) const { return t > a.t; }
};
int main() {
  int n, k;
  while (cin >> n >> k, n) {
    int vis[120];
    vector<Edge> Node[120];
    priority_queue<Data> Q;
    for (int i = 0; i < k; i++) {
      int z;
      cin >> z;
      if (z == 0) {
        int x, y;
        cin >> x >> y;
        for (int i = 0; i < 120; i++)
          vis[i] = 1e+9;
        Q.push(Data(x, 0));
        while (!Q.empty()) {
          Data D = Q.top();
          Q.pop();
          int p = D.pos;
          if (vis[p] < D.t)
            continue;
          for (int i = 0; i < Node[p].size(); i++) {
            Edge e = Node[p][i];
            if (vis[e.to] <= D.t + e.cost)
              continue;
            vis[e.to] = D.t + e.cost;
            Q.push(Data(e.to, vis[e.to]));
          }
        }
        if (vis[y] == 1e+9)
          vis[y] = -1;
        cout << vis[y] << endl;
      }
      if (z == 1) {
        int x, y, t;
        cin >> x >> y >> t;
        Node[x].push_back(Edge(y, t));
        Node[y].push_back(Edge(x, t));
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
class Edge {
public:
  int to;
  int cost;
  Edge(int x, int y) {
    to = x;
    cost = y;
  }
};
typedef vector<Edge> Node;
struct Data {
  int x;
  int c;
  Data() {}
  Data(int _x, int _c) {
    x = _x;
    c = _c;
  }
  bool operator<(const Data &d) const { return c < d.c; }
};
int main(void) {

  int n, k;
  while (cin >> n >> k, n) {
    Node G[120];
    while (k--) {
      int w;
      cin >> w;
      if (w) {
        int a, b, c;
        cin >> a >> b >> c;
        bool flg = true;
        for (int i = 0; i < G[a].size(); i++) {
          if (G[a][i].to == b)
            G[a][i].cost = min(G[a][i].cost, c), flg = false;
        }
        for (int i = 0; i < G[b].size(); i++) {
          if (G[b][i].to == a)
            G[b][i].cost = min(G[b][i].cost, c);
        }
        if (flg) {
          G[a].push_back(Edge(b, c));
          G[b].push_back(Edge(a, c));
        }
      } else {
        int a, b;
        cin >> a >> b;
        priority_queue<Data> Q;
        Q.push(Data(a, 0));
        int vis[120] = {};
        for (int i = 1; i <= n; i++)
          vis[i] = 2 * 1e9;
        while (!Q.empty()) {
          Data D = Q.top();
          Q.pop();
          vis[D.x] = D.c;
          for (int i = 0; i < G[D.x].size(); i++) {
            Data E;
            E.x = G[D.x][i].to;
            E.c = D.c + G[D.x][i].cost;
            if (vis[G[D.x][i].to] > E.c) {
              Q.push(E);
            }
          }
        }
        if (vis[b] == 2 * 1e9)
          cout << -1 << endl;
        else
          cout << vis[b] << endl;
      }
    }
  }
  return 0;
}