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


#include <algorithm>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;

#define MAX_V 100001
#define INF (1e9 + 1)

struct edge {
  int to, cost;
};

int V, E, r, a, b, c;

vector<edge> G[MAX_V];

int d[MAX_V];
void dijkstra() {
  int i;
  for (i = 0; i < MAX_V; i++)
    d[i] = INF;
  d[r] = 0;
  priority_queue<pair<int, int>> Q;
  Q.push(make_pair(0, r));
  while (!Q.empty()) {
    pair<int, int> p = Q.top();
    Q.pop();
    int pos = p.second, cost = -p.first;
    if (cost > d[pos])
      continue;
    for (i = 0; i < G[pos].size(); i++) {
      edge e = G[pos][i];
      int to = e.to;
      int newcost = cost + e.cost;
      if (newcost < d[to]) {
        d[to] = newcost;
        Q.push(make_pair(-d[to], to));
      }
    }
  }

  for (i = 0; i < V; i++) {
    if (d[i] == INF)
      printf("INF\\n");
    else
      printf("%d\\n", d[i]);
  }
}

int main() {
  int i;
  scanf("%d %d %d", &V, &E, &r);
  for (i = 0; i < E; i++) {
    scanf("%d %d %d", &a, &b, &c);
    G[a].push_back((edge){b, c});
  }
  dijkstra();
  return 0;
}


Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  {                                                                            \\
    cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                     \\
         << " " << __FILE__ << endl;                                           \\
  }
void print_list(int n, int *x) {
  REP(i, n - 1) printf("%d ", x[i]);
  printf("%d\\n", x[n - 1]);
}

#define INF 1000000000

int argmin(int n, int *label, int *selected) {
  int tmp = INF + 1;
  int ans = -1;
  int score;
  REP(i, n) {
    score = label[i];
    if (score < tmp && selected[i] == 0) {
      ans = i;
      tmp = score;
    }
  }
  return ans;
}

int main() {
  int n, m, r, min_v, score;
  scanf("%d%d%d", &n, &m, &r);
  int head[m];
  int tail[m];
  int dist[m];
  REP(i, m) scanf("%d%d%d", &head[i], &tail[i], &dist[i]);

  /*dijkstra method*/
  int label[n];
  int selected[n];
  REP(i, n) {
    label[i] = INF; // shortest path weight from r
    selected[i] = 0;
  }
  label[r] = 0;
  int update = 0;
  while (update != n) {
    update++;
    min_v = argmin(n, label, selected);

    selected[min_v] = 1;

    /*edge??§??´??°*/
    REP(e, m) {
      if (head[e] == min_v && selected[tail[e]] == 0) {
        score = label[min_v] + dist[e];
        if (score < label[tail[e]])
          label[tail[e]] = score;
      }

      /*????????°??????????????§??\\?????????????????????*/
      /*
      else if(tail[e]==min_v && selected[head[e]]==0){
        score = label[min_v] + dist[e];
        if(score < label[head[e]]) label[head[e]] = score;
      }
      */
    }
  }

  REP(i, n) {
    if (label[i] == INF) {
      printf("INF\\n");
    } else {
      printf("%d\\n", label[i]);
    }
  }

  return 0;
}