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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

#define NO_ROUTE 1000000

using namespace std;

struct edge {
  // int n;
  int tozoku;
  int dist;
};

struct node {
  int n;
  int money;
  vector<int> visit;
  bool done;
  int tozoku;
  node(int n0 = 0, int money0 = 0, int done0 = false, int tozoku0 = 1000000) {
    n = n0, money = money0, done = done0, tozoku = tozoku0;
  }
  /*push_back(int n) {
    vector.push_back(n);
    } */
  bool operator<(const node &t) const { return tozoku > t.tozoku; }
};

int N, M, L;
node G[100][101];
edge e[100][100];

int search() {
  priority_queue<node> que;
  node start(0, L, true, 0);
  // start.push_back(0);
  G[0][L] = node(0, L, true, 0);
  // G[0][L].push_back(0);
  que.push(start);

  while (!que.empty()) {
    node n_node;
    n_node = que.top();
    G[n_node.n][n_node.money].done;
    // cout << "n " << n_node.n << " money " << n_node.money << " done " <<
    // n_node.done << " tozoku " << n_node.tozoku << endl;
    que.pop();
    if (n_node.n == N - 1)
      return n_node.tozoku;

    for (int i = 0; i < N; i++) {
      if (e[n_node.n][i].dist == NO_ROUTE)
        continue;
      //////cout << "dist " << e[n_node.n][i].dist << endl;
      if (!G[i][n_node.money].done) {
        // printf("(%d, %d) : %d %d\\n", n_node.n, i, n_node.tozoku +
        // e[i][n_node.money].tozoku, )
        if (n_node.tozoku + e[i][n_node.n].tozoku < G[i][n_node.money].tozoku) {
          G[i][n_node.money] = node(i, n_node.money, false,
                                    n_node.tozoku + e[i][n_node.n].tozoku);
          ////cout << n_node.tozoku << " + " << e[n_node.n][i].tozoku << endl;
          node t_node = G[i][n_node.money];
          // cout << "::n " << t_node.n << " money " << t_node.money << " done "
          // << t_node.done << " tozoku " << t_node.tozoku << endl;
          que.push(t_node);
        }
      }
      if (n_node.money >= e[n_node.n][i].dist &&
          !G[i][n_node.money - e[n_node.n][i].dist].done &&
          n_node.tozoku < G[i][n_node.money - e[n_node.n][i].dist].tozoku) {
        G[i][n_node.money - e[n_node.n][i].dist] =
            node(i, n_node.money - e[n_node.n][i].dist, false, n_node.tozoku);
        node t_node = G[i][n_node.money - e[n_node.n][i].dist];
        // cout << "::n " << t_node.n << " money " << t_node.money << " done "
        // << t_node.done << " tozoku " << t_node.tozoku << endl;
        que.push(t_node);
      }
    }
  }
  return -1;
}

int main() {
  while (true) {
    scanf("%d %d %d ", &N, &M, &L);
    if (N == 0 && M == 0 && L == 0)
      break;
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        G[i][j].n = i;
        G[i][j].money = j;
        G[i][j].tozoku = 1000000;
        G[i][j].done = false;
        e[i][j].dist = NO_ROUTE;
        e[i][j].tozoku = 0;
      }
    }

    for (int i = 0; i < 100; i++) {
      G[i][100].tozoku = 1000000;
      G[i][100].n = i;
      G[i][100].done = false;
      G[i][100].money = 100;
    }

    for (int i = 0; i < M; i++) {
      int x, y;
      scanf("%d %d ", &x, &y);
      x--, y--;
      scanf("%d %d ", &e[x][y].dist, &e[x][y].tozoku);
      e[y][x].dist = e[x][y].dist, e[y][x].tozoku = e[x][y].tozoku;
    }

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        ////cout << e[i][j].dist << " " << e[i][j].tozoku << "   ";
      }
      ////cout << endl;
    }

    cout << search() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;

struct node {
  int n;
  int money;
  int tozoku;
  // int to;
  bool operator<(const node &t) const {
    return (tozoku != t.tozoku) ? tozoku > t.tozoku : money > t.money;
  }
};

struct edge {
  int dist;
  int tozoku;
};

node G[100][101];
edge e[100][100];
int n, m, l;

int search() {
  priority_queue<node> que;
  node start;
  start.n = 0, start.money = l, start.tozoku = 0;
  que.push(start);
  while (!que.empty()) {
    node n_node = que.top();
    // cout<< "n, money, tozoku = " << n_node.n << " " << n_node.money << " " <<
    // n_node.tozoku << endl;
    que.pop();
    if (n_node.n == n - 1)
      return n_node.tozoku;
    for (int i = 0; i < n; i++) {
      // cout << e[n_node.n][i].dist << " " << e[n_node.n][i].tozoku << endl;
      if (e[n_node.n][i].dist != 100000) {
        if (G[i][n_node.money].tozoku > n_node.tozoku + e[n_node.n][i].tozoku) {
          node t_node;
          t_node.n = i, t_node.money = n_node.money,
          t_node.tozoku = n_node.tozoku + e[n_node.n][i].tozoku;
          que.push(t_node);
        }
        if (n_node.money >= e[n_node.n][i].dist &&
            G[i][n_node.money - e[n_node.n][i].dist].tozoku > n_node.tozoku) {
          node t_node;
          t_node.n = i, t_node.money = n_node.money - e[n_node.n][i].dist,
          t_node.tozoku = n_node.tozoku;
          que.push(t_node);
        }
      }
    }
  }
}

int main() {
  while (1) {
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 101; j++) {
        G[i][j].n = i;
        G[i][j].money = j;
        G[i][j].tozoku = 100000;
      }
    }
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        e[i][j].dist = 100000;
      }
    }

    scanf("%d %d %d ", &n, &m, &l);
    if (n == 0 && m == 0 && l == 0)
      break;

    for (int i = 0; i < m; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      x--, y--;
      ////cout<< x << " " << y << endl;

      scanf("%d %d ", &e[x][y].dist, &e[x][y].tozoku);
      e[y][x].dist = e[x][y].dist, e[y][x].tozoku = e[x][y].tozoku;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        // cout<< "edge "<< e[i][j].dist << " " << e[i][j].tozoku << " ";
      }
      // cout<< endl;
    }
    cout << search() << endl;
  }
}