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

#include <iostream>
#include <queue>
#include <vector>

using namespace std;
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
typedef vector<pii> vp;
typedef vector<vp> vvp;
const int inf = 2e9;
#define pb push_back

int main() {
  int n, m;
  cin >> n >> m;
  vvp cost(n);
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    cost[a].pb(pii(b, c));
  }
  vi used(n);
  vi d(n, inf);
  d[0] = 0;
  used[n - 1] = true;

  priority_queue<pii> q;
  q.push(pii(0, 0));
  while (!q.empty()) {
    pii p = q.top();
    q.pop();
    int pos = p.second, cost1 = -p.first;
    if (cost1 > d[pos])
      continue;
    rep(i, cost[pos].size()) {
      int to = cost[pos][i].first;
      int nowcost = cost[pos][i].second;
      if (d[pos] <= nowcost && d[to] > nowcost) {
        d[to] = nowcost;
        q.push(pii(-d[to], to));
      }
    }
  }
  /*
  while(1){
    int v=-1;
    rep(i,n)if(!used[i]&&(v==-1||d[i]<d[v]))v=i;
    if(v==-1)break;
    used[v]=true;
    rep(i,cost[v].size())if(d[v]<=cost[v][i].second&&d[cost[v][i].first]>cost[v][i].second)d[cost[v][i].first]=cost[v][i].second;
    //
  rep(i,n-1)rep(j,cost[v][i].size())if(d[v]<=cost[v][i][j]&&d[i]>cost[v][i][j])d[i]=cost[v][i][j];
  }
  */
  int out = -1;
  rep(i, n - 1) rep(j, cost[i].size()) if (cost[i][j].first == n - 1 &&
                                           d[i] <= cost[i][j].second) out =
      max(out, cost[i][j].second);
  cout << out << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
typedef vector<pii> vp;
typedef vector<vp> vvp;
const int inf = 2e9;
#define pb push_back
int main() {
  int n, m;
  cin >> n >> m;
  //  vvvi cost(n,vvi(n));
  vvp cost(n);
  //  rep(i,n)cost[i][n-1]=-1;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    cost[a].pb(pii(b, c));
    //    cost[a][b].pb(c);
    //    if(b==n-1)cost[a][b]=max(cost[a][b],c);
    // else cost[a][b]=min(cost[a][b],c);
  }
  vi used(n);
  vi d(n, inf);
  d[0] = 0;
  used[n - 1] = true;
  while (1) {
    int v = -1;
    rep(i, n) if (!used[i] && (v == -1 || d[i] < d[v])) v = i;
    if (v == -1)
      break;
    used[v] = true;
    rep(i, cost[v].size()) if (d[v] <= cost[v][i].second &&
                               d[cost[v][i].first] > cost[v][i].second)
        d[cost[v][i].first] = cost[v][i].second;
    // rep(i,n-1)rep(j,cost[v][i].size())if(d[v]<=cost[v][i][j]&&d[i]>cost[v][i][j])d[i]=cost[v][i][j];
  }
  int out = -1;
  rep(i, n - 1) rep(j, cost[i].size()) if (cost[i][j].first == n - 1 &&
                                           d[i] <= cost[i][j].second) out =
      max(out, cost[i][j].second);
  //  rep(i,n-1)rep(j,cost[i][n-1].size())if(d[i]<=cost[i][n-1][j])out=max(out,cost[i][n-1][j]);
  cout << out << endl;
  // rep(i,n)cout<<d[i]<<endl;
}