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

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
//    #define int long long
#define inf 1000000007
#define pa pair<int, int>
#define ll long long
#define pal pair<ll, ll>
#define ppa pair<int, pa>
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
//----------------kokomade temple------------
int dis[100005];
vector<pa> G[100005];
int V, E, r;
int main() {

  cin >> V >> E >> r;
  for (int i = 0; i < V; i++)
    dis[i] = inf;
  for (int i = 0; i < E; i++) {
    int z, zz, zzz;
    cin >> z >> zz >> zzz;
    G[z].push_back(make_pair(zz, zzz));
  }

  priority_queue<pa, vector<pa>, greater<pa>> pq;
  dis[r] = 0;

  for (int i = 0; i < G[r].size(); i++) {
    pq.push(make_pair(G[r][i].second, G[r][i].first));
  }

  while (pq.size() > 0) {
    pa w = pq.top();
    pq.pop();
    if (dis[w.second] < w.first)
      continue;
    dis[w.second] = w.first;
    for (int i = 0; i < G[w.second].size(); i++) {
      pq.push(make_pair(dis[w.second] + G[w.second][i].second,
                        G[w.second][i].first));
    }
  }
  for (int i = 0; i < V; i++) {
    if (dis[i] == inf)
      cout << "INF" << endl;
    else
      cout << dis[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int uni[100010];

int oya(int a) {
  if (uni[a] < 0) {
    return a;
  } else {
    uni[a] = oya(uni[a]);
    return oya(uni[a]);
  }
}

bool ren(int a, int b) {
  a = oya(a);
  b = oya(b);
  if (a == b) {
    return false; // renketu
  } else {
    return true; // not renketu
  }
}

void con(int a, int b) {
  if (ren(a, b)) {
    a = oya(a);
    uni[a] = b;
  }
}

int main() {
  // your code goes here

  int e, v, r;
  cin >> v >> e >> r;

  for (int y = 0; y < v; y++) {
    uni[y] = -1;
  }

  int cho[100010] = {0};
  int gen[100010];
  int kai[100010];

  vector<int> iki[100010];
  vector<int> kyo[100010];
  for (int u = 0; u < e; u++) {
    int sta, goa, dis;
    cin >> sta >> goa >> dis;

    iki[sta].push_back(goa);
    kyo[sta].push_back(dis);
    //		iki[goa].push_back(sta);
    //	kyo[goa].push_back(dis);
  }
  for (int u = 0; u < v; u++) {
    gen[u] = 100000000;
    kai[u] = -1;
  }
  cho[r] = 1;
  queue<int> c;
  c.push(r);
  kai[r] = 0;
  for (int u = 0; u < v - 1; u++) {
    int d = c.back();
    //	cout<<"kijun"<<d<<endl;
    //		cout<<"hennkazu"<<iki[d].size()<<endl;
    for (int w = 0; w < iki[d].size(); w++) {
      //	cout<<kai[d]<<" "<<kyo[d][w]<<" "<<gen[iki[d][w]];
      if (kai[d] + kyo[d][w] < gen[iki[d][w]]) {

        gen[iki[d][w]] = kai[d] + kyo[d][w];
        //		cout<<iki[d][w]<<" dainyuu "<<kyo[d][w]<<endl;
      }
    }
    int ma = 100000000, num = -1;
    for (int z = 0; z < v; z++) {
      if (gen[z] < ma && ren(z, r) == true) {
        ma = gen[z];
        num = z;
      }
    }
    if (num >= 0) {
      kai[num] = gen[num];
      con(num, r);
      c.push(num);
    }

    //	cout<<kai[0]<<" "<<kai[1]<<" "<<kai[2]<<" "<<kai[3]<<" "<<kai[4]<<endl;
  }
  for (int pp = 0; pp < v; pp++) {
    if (kai[pp] > -1) {
      cout << kai[pp] << endl;
    } else {
      cout << "INF" << endl;
    }
  }

  return 0;
}