Problem p01770 (C++)

Buggy submission (status: Time Limit Exceeded):
// Template {{{
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (int i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 < v.size() ? ", " : "");
  os << "}";
  return os;
}

class range {
  struct Iterator {
    int val, inc;
    int operator*() { return val; }
    bool operator!=(Iterator &rhs) { return val < rhs.val; }
    void operator++() { val += inc; }
  };
  Iterator i, n;

public:
  range(int e) : i({0, 1}), n({e, 1}) {}
  range(int b, int e) : i({b, 1}), n({e, 1}) {}
  range(int b, int e, int inc) : i({b, inc}), n({e, inc}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(12);
}
//}}}

struct Edge {
  int dst, type;
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;
typedef tuple<int, int, int, int> TP;
const int INF = 1000000000;
int main() {
  iostream_init();
  int N, M, E, S, T, R;
  while (cin >> N >> M >> E >> S >> T >> R) {
    R++; // < R

    Graph G(N);
    REP(i, M) {
      int a, b;
      cin >> a >> b;
      G[a].push_back({b, -1});
      G[b].push_back({a, -1});
    }
    vector<int> eve[101];
    REP(i, E) {
      int a, b, c;
      cin >> a >> b >> c;
      eve[c].push_back(i);
      G[a].push_back({b, i});
      G[b].push_back({a, i});
    }
    static int dist[102][102][1 << 8] = {};
    REP(i, N) REP(j, R) REP(s, 1 << E) dist[i][j][s] = INF;
    priority_queue<TP> que;
    auto insert = [&](int a, int b, int c, int d) {
      for (int e : eve[b])
        d |= 1 << e;
      if (a < dist[b][c][d]) {
        dist[b][c][d] = a;
        que.push(make_tuple(a, b, c, d));
      }
    };
    insert(0, S, 0, 0);
    while (!que.empty()) {
      int cost, cur, time, state;
      tie(cost, cur, time, state) = que.top();
      que.pop();
      if (dist[cur][time][state] < cost)
        continue;

      // back
      insert(cost + 1, S, 0, state);

      if (time + 1 < R) {
        for (Edge ed : G[cur]) {
          int next = ed.dst;
          int type = ed.type;
          if (type != -1 && !(state >> type & 1))
            continue;
          insert(cost + 1, next, time + 1, state);
        }
      }
    }

    int ans = INF;
    REP(i, R) REP(s, 1 << E) ans = min(ans, dist[T][i][s]);
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}

/* vim:set foldmethod=marker: */

Fixed submission:
// Template {{{
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (int i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 < v.size() ? ", " : "");
  os << "}";
  return os;
}

class range {
  struct Iterator {
    int val, inc;
    int operator*() { return val; }
    bool operator!=(Iterator &rhs) { return val < rhs.val; }
    void operator++() { val += inc; }
  };
  Iterator i, n;

public:
  range(int e) : i({0, 1}), n({e, 1}) {}
  range(int b, int e) : i({b, 1}), n({e, 1}) {}
  range(int b, int e, int inc) : i({b, inc}), n({e, inc}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(12);
}
//}}}

struct Edge {
  int dst, type;
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;
typedef tuple<int, int, int, int> TP;
const int INF = 1000000000;
int main() {
  iostream_init();
  int N, M, E, S, T, R;
  while (cin >> N >> M >> E >> S >> T >> R) {
    R++; // < R

    Graph G(N);
    REP(i, M) {
      int a, b;
      cin >> a >> b;
      G[a].push_back({b, -1});
      G[b].push_back({a, -1});
    }
    vector<int> eve[101];
    REP(i, E) {
      int a, b, c;
      cin >> a >> b >> c;
      eve[c].push_back(i);
      G[a].push_back({b, i});
      G[b].push_back({a, i});
    }
    static int dist[102][102][1 << 8] = {};
    REP(i, N) REP(j, R) REP(s, 1 << E) dist[i][j][s] = INF;
    priority_queue<TP, vector<TP>, greater<TP>> que;
    auto insert = [&](int a, int b, int c, int d) {
      for (int e : eve[b])
        d |= 1 << e;
      if (a < dist[b][c][d]) {
        dist[b][c][d] = a;
        que.push(make_tuple(a, b, c, d));
      }
    };
    insert(0, S, 0, 0);
    while (!que.empty()) {
      int cost, cur, time, state;
      tie(cost, cur, time, state) = que.top();
      que.pop();
      if (dist[cur][time][state] < cost)
        continue;

      // back
      insert(cost + 1, S, 0, state);

      if (time + 1 < R) {
        for (Edge ed : G[cur]) {
          int next = ed.dst;
          int type = ed.type;
          if (type != -1 && !(state >> type & 1))
            continue;
          insert(cost + 1, next, time + 1, state);
        }
      }
    }

    int ans = INF;
    REP(i, R) REP(s, 1 << E) ans = min(ans, dist[T][i][s]);
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}

/* vim:set foldmethod=marker: */