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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 28;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-10;
const ll mod = 1e9 + 7;
const vi emp;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, -0};
const int DX[8] = {-1, -1, -1, 0, 0, 1, 1, 1},
          DY[8] = {1, 0, -1, 1, -1, 1, 0, -1};

class Graph {
private:
  int V;
  vvp List;

public:
  Graph(int v) {
    V = v;
    List = vvp(v);
  }
  void add_edge(int s, int t, int c) {
    List[s].push_back(P(t, c));
    List[t].push_back(P(s, c));
  }
  int DIJ(int s, int t) {
    priority_queue<P> que;
    vi d(V, inf);
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < -p.first)
        continue;
      for (int i = 0; i < List[v].size(); i++) {
        int F = List[v][i].first, S = List[v][i].second;
        if (d[F] > d[v] + S) {
          d[F] = d[v] + S;
          que.push(P(-d[F], F));
        }
      }
    }
    return (d[t] == inf) ? -1 : d[t];
  }
};

int n;

int main() {
  while (1) {
    int n, k;
    cin >> n >> k;
    if (!n)
      break;
    Graph g(n);
    for (int i = 0; i < k; i++) {
      int a;
      cin >> a;
      if (a) {
        int s, t, c;
        cin >> s >> t >> c;
        g.add_edge(s - 1, t - 1, c);
      } else {
        int s, t;
        cin >> s >> t;
        cout << g.DIJ(s - 1, t - 1) << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 25;
const double pi = acos(-1);
const double eps = 1e-10;
const vi emp;

class Graph {
private:
  int V;
  vvp List;

public:
  Graph(int v) {
    V = v;
    List = vvp(v);
  }
  void add_edge(int x, int y, int c) {
    List[x].push_back(P(y, c));
    List[y].push_back(P(x, c));
  }
  int DIJ(int s, int t) {
    priority_queue<P> que;
    vi d(V, inf);
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (auto i = List[v].begin(); i != List[v].end(); i++) {
        int F = (*i).first, S = (*i).second;
        if (d[F] > d[v] + S) {
          d[F] = d[v] + S;
          que.push(P(d[F], F));
        }
      }
    }
    return d[t];
  }
};

int V, n;

int main() {
  while (1) {
    cin >> V >> n;
    if (!V && !n)
      return 0;
    Graph gra(V);
    int tmp;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      if (tmp) {
        int s, t, c;
        cin >> s >> t >> c;
        gra.add_edge(s - 1, t - 1, c);
      } else {
        int s, t;
        cin >> s >> t;
        int ans = gra.DIJ(s - 1, t - 1);
        if (ans == inf)
          cout << -1 << endl;
        else
          cout << ans << endl;
      }
    }
  }
}