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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = ll(1e5) + 5;
const double EPS = 10e-8;
const double PI = 3.14159265359;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

ll n, d[100000];
vector<P> g[100000];

void dijkstra(ll s) {
  fill(d, d + n, INF);
  d[s] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push({0, s});
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll u = p.second;
    if (p.first > d[u])
      continue;
    REP(i, g[u].size()) {
      ll v = g[u][i].first, cost = g[u][i].second;
      if (d[v] > d[u] + cost) {
        d[v] = d[u] + cost;
        que.push({d[v], v});
      }
    }
  }
}

int main() {
  cin >> n;
  REP(i, n) {
    ll from, k;
    cin >> from >> k;
    REP(j, k) {
      ll to, c;
      cin >> to >> c;
      g[from].push_back({to, c});
    }
  }
  dijkstra(0);
  REP(i, n) printf("%ld %ld\\n", i, d[i]);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = ll(1e5) + 5;
const double EPS = 10e-8;
const double PI = 3.14159265359;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

struct UF {
  static const int MAX_N = 100000;
  int p[MAX_N];
  int r[MAX_N];

  void init(int n) {
    REP(i, n) {
      p[i] = i;
      r[i] = 0;
    }
  }
  int find(int x) {
    if (p[x] == x)
      return x;
    return p[x] = find(p[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (r[x] < r[y])
      swap(x, y);
    if (r[x] == r[y])
      r[x]++;
    p[y] = x;
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

ll n, d[100000];
vector<P> g[100000];

void dijkstra(ll s) {
  fill(d, d + n, INF);
  d[s] = 0;
  priority_queue<P> que;
  que.push({0, s});
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll u = p.second;
    if (p.first > d[u])
      continue;
    REP(i, g[u].size()) {
      ll v = g[u][i].first, cost = g[u][i].second;
      if (d[v] > d[u] + cost) {
        d[v] = d[u] + cost;
        que.push({d[v], v});
      }
    }
  }
}

int main() {
  cin >> n;
  REP(i, n) {
    ll from, k;
    cin >> from >> k;
    REP(j, k) {
      ll to, c;
      cin >> to >> c;
      g[from].push_back({to, c});
    }
  }
  dijkstra(0);
  REP(i, n) printf("%ld %ld\\n", i, d[i]);
  return 0;
}