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


#include <bits/stdc++.h>
using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

class E {
public:
  int to, dist, value;
  E(int to, int dist, int value) : to(to), dist(dist), value(value) {}
};

const int N = 111;

int n, m, r;
vector<E> edge[N];

bool input() {
  cin >> n >> m >> r;
  if (n == 0)
    return false;

  rep(i, N) edge[i].clear();

  rep(i, m) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    edge[a].push_back(E(b, c, d));
    edge[b].push_back(E(a, c, d));
  }

  return true;
}

class Q {
public:
  int pos;
  int life;
  int val;
  Q(int a, int b, int c) : pos(a), life(b), val(c) {}
  bool operator<(const Q &a) const {
    if (val != a.val)
      return val > a.val;
    return life < a.life;
  }
};

int visit[N][N];

int solve() {
  rep(i, N) rep(j, N) visit[i][j] = 0;
  // puts("hello");

  priority_queue<Q> que;
  que.push(Q(1, r, 0));

  int ans = -1;
  while (!que.empty()) {
    priority_queue<Q> cp = que;

    Q u = que.top();
    que.pop();

    // printf("@@ %d %d %d   %d\\n",u.pos,u.life,u.val, que.size());

    if (visit[u.pos][u.life] == 1)
      continue;
    visit[u.pos][u.life] = 1;

    // printf("** %d %d %d\\n",u.pos,u.life,u.val);

    if (u.pos == n) {
      if (ans == -1 || ans > u.val)
        ans = u.val;
      return u.val;
    }

    rep(i, edge[u.pos].size()) {
      E e = edge[u.pos][i];
      if (u.life - e.dist >= 0)
        que.push(Q(e.to, u.life - e.dist, u.val));
      que.push(Q(e.to, u.life, u.val + e.value));
    }
  }
  return ans;
}

int main() {
  while (input())
    cout << solve() << endl;
}

Error: TLE

Fixed submission:



#include <bits/stdc++.h>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

class E {
public:
  int to, dist, value;
  E(int to, int dist, int value) : to(to), dist(dist), value(value) {}
};

const int N = 111;

int n, m, r;
vector<E> edge[N];

bool input() {
  cin >> n >> m >> r;
  if (n == 0)
    return false;

  rep(i, N) edge[i].clear();

  rep(i, m) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    edge[a].push_back(E(b, c, d));
    edge[b].push_back(E(a, c, d));
  }

  return true;
}

class Q {
public:
  int pos;
  int life;
  int val;
  Q(int a, int b, int c) : pos(a), life(b), val(c) {}
  bool operator<(const Q &a) const {
    if (val != a.val)
      return val > a.val;
    return life < a.life;
  }
};

int visit[N][N];

int solve() {
  rep(i, N) rep(j, N) visit[i][j] = 0;
  // puts("hello");

  priority_queue<Q> que;
  que.push(Q(1, r, 0));

  int ans = -1;
  while (!que.empty()) {
    priority_queue<Q> cp = que;

    Q u = que.top();
    que.pop();

    // printf("@@ %d %d %d   %d\\n",u.pos,u.life,u.val, que.size());

    if (visit[u.pos][u.life] == 1)
      continue;
    visit[u.pos][u.life] = 1;

    // printf("** %d %d %d\\n",u.pos,u.life,u.val);

    if (u.pos == n) {
      if (ans == -1 || ans > u.val)
        ans = u.val;
      // return u.val;
    }

    rep(i, edge[u.pos].size()) {
      E e = edge[u.pos][i];
      if (u.life - e.dist >= 0)
        que.push(Q(e.to, u.life - e.dist, u.val));
      que.push(Q(e.to, u.life, u.val + e.value));
    }
  }
  return ans;
}

int main() {
  while (input())
    cout << solve() << endl;
}