Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <vector>

long long mod = 1e9 + 7;
class Mint {
public:
  long long x;
  Mint(){}; // 引数なしでも定義できるように引数なしコンストラクタも用意しておく
  Mint(long long a) {
    x = a % mod;
    while (x < 0) {
      x += mod;
    }
  };
  Mint &operator+=(const Mint &a) {
    x += a.x;
    x %= mod;
    return *this;
  }
  Mint &operator-=(const Mint &a) {
    x += (mod - a.x);
    x %= mod;
    return *this;
  }
  Mint &operator*=(const Mint &a) {
    x *= a.x;
    x %= mod;
    return *this;
  }

  // a^n mod を計算する
  // Useaeg: Mint z = y.pow(y, n);
  Mint pow(Mint a, long long n) const {
    Mint y(1);
    Mint b(a);
    while (n > 0) {
      if (n & 1)
        y *= b;
      b *= b;
      n >>= 1;
    }
    return y;
  }

  // a^{-1} mod を計算する
  Mint modinv(const Mint &a) const { return pow(a, mod - 2); }

  Mint &operator/=(const Mint &a) {
    x *= modinv(a).x;
    x %= mod;
    return *this;
  }

  Mint operator+(Mint &a) const {
    Mint y(*this);
    y += a;
    return y;
  }

  Mint operator-(Mint &a) const {
    Mint y(*this);
    y -= a;
    return y;
  }

  Mint operator*(Mint &a) const {
    Mint y(*this);
    y *= a;
    return y;
  }

  Mint operator/(Mint &a) const {
    Mint y(*this);
    y /= a;
    return y.x;
  }

  // nCk @mod を計算する
  Mint nCk(Mint &n, const long long k) const {
    Mint y = Mint(1);
    for (Mint i(0); (i.x) < k; i.x++) {
      y *= (n - i);
      y /= i;
    }
    return y;
  }

  // nPk @mod を計算する
  Mint nPk(Mint &n, long long k) const {
    Mint y(1);
    for (Mint i(0); (i.x) < k; i.x++) {
      y *= (n - i);
    }
    return y;
  }
};

struct node {
  int v;
  int supply;
  int amari;
};

class MyComparison {
public:
  bool operator()(node n1, node n2) {
    return (n1.supply != n2.supply) ? (n1.supply > n2.supply)
                                    : (n1.amari < n2.amari);
  }
};

std::pair<int, int> dist[304][304];
int main() {
  int N, M, L;
  std::cin >> N >> M >> L;
  std::vector<std::pair<int, int>> edges[304];
  int A, B, C;
  for (int i = 0; i < M; i++) {
    std::cin >> A >> B >> C;
    if (C > L)
      continue;
    A--;
    B--;
    edges[A].push_back(std::make_pair(B, C));
    edges[B].push_back(std::make_pair(A, C));
  }
  int Q;
  std::cin >> Q;
  std::vector<int> s(Q), t(Q);
  for (int i = 0; i < Q; i++) {
    std::cin >> s[i] >> t[i];
    s[i]--;
    t[i]--;
  }
  for (int i = 0; i < 304; i++) {
    for (int j = 0; j < 304; j++) {
      dist[i][j] = std::make_pair(1001001001, 0);
    }
  }
  for (int i = 0; i < N; i++) {
    dist[i][i] = std::make_pair(0, L);
    std::priority_queue<node, std::vector<node>, MyComparison> queue;
    queue.push((node){i, 0, L});
    while (queue.size() > 0) {
      auto entry = queue.top();
      queue.pop();
      int v = entry.v;
      int amari = entry.amari;
      int supply = entry.supply;
      // if ((dist[i][v].first<supply) || (dist[i][v].first==supply &&
      // dist[i][v].second>amari)) continue;
      for (auto cand : edges[v]) {
        int next_amari = amari - cand.second;
        int next_supply = supply;
        int vv = cand.first;
        if (next_amari < 0) {
          next_amari = L - cand.second;
          next_supply++;
        }
        if ((next_supply < dist[i][vv].first) ||
            (next_supply == dist[i][vv].first &&
             next_amari > dist[i][vv].second)) {
          dist[i][vv].first = next_supply;
          dist[i][vv].second = next_amari;
          queue.push((node){vv, next_supply, next_amari});
          // printf("dist[%d][%d]=(%d, %d)\n", i, vv, dist[i][vv].first,
          // dist[i][vv].second);
        }
      }
    }
  }
  for (int i = 0; i < Q; i++) {
    if (dist[s[i]][t[i]].first == 1001001001) {
      printf("-1\n");
    } else {
      printf("%d\n", dist[s[i]][t[i]].first);
    }
  }
  return 0;
}

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <vector>

long long mod = 1e9 + 7;
class Mint {
public:
  long long x;
  Mint(){}; // 引数なしでも定義できるように引数なしコンストラクタも用意しておく
  Mint(long long a) {
    x = a % mod;
    while (x < 0) {
      x += mod;
    }
  };
  Mint &operator+=(const Mint &a) {
    x += a.x;
    x %= mod;
    return *this;
  }
  Mint &operator-=(const Mint &a) {
    x += (mod - a.x);
    x %= mod;
    return *this;
  }
  Mint &operator*=(const Mint &a) {
    x *= a.x;
    x %= mod;
    return *this;
  }

  // a^n mod を計算する
  // Useaeg: Mint z = y.pow(y, n);
  Mint pow(Mint a, long long n) const {
    Mint y(1);
    Mint b(a);
    while (n > 0) {
      if (n & 1)
        y *= b;
      b *= b;
      n >>= 1;
    }
    return y;
  }

  // a^{-1} mod を計算する
  Mint modinv(const Mint &a) const { return pow(a, mod - 2); }

  Mint &operator/=(const Mint &a) {
    x *= modinv(a).x;
    x %= mod;
    return *this;
  }

  Mint operator+(Mint &a) const {
    Mint y(*this);
    y += a;
    return y;
  }

  Mint operator-(Mint &a) const {
    Mint y(*this);
    y -= a;
    return y;
  }

  Mint operator*(Mint &a) const {
    Mint y(*this);
    y *= a;
    return y;
  }

  Mint operator/(Mint &a) const {
    Mint y(*this);
    y /= a;
    return y.x;
  }

  // nCk @mod を計算する
  Mint nCk(Mint &n, const long long k) const {
    Mint y = Mint(1);
    for (Mint i(0); (i.x) < k; i.x++) {
      y *= (n - i);
      y /= i;
    }
    return y;
  }

  // nPk @mod を計算する
  Mint nPk(Mint &n, long long k) const {
    Mint y(1);
    for (Mint i(0); (i.x) < k; i.x++) {
      y *= (n - i);
    }
    return y;
  }
};

struct node {
  int v;
  int supply;
  int amari;
};

class MyComparison {
public:
  bool operator()(node n1, node n2) {
    return (n1.supply != n2.supply) ? (n1.supply > n2.supply)
                                    : (n1.amari < n2.amari);
  }
};

std::pair<int, int> dist[304][304];
int main() {
  int N, M, L;
  std::cin >> N >> M >> L;
  std::vector<std::pair<int, int>> edges[304];
  int A, B, C;
  for (int i = 0; i < M; i++) {
    std::cin >> A >> B >> C;
    if (C > L)
      continue;
    A--;
    B--;
    edges[A].push_back(std::make_pair(B, C));
    edges[B].push_back(std::make_pair(A, C));
  }
  int Q;
  std::cin >> Q;
  std::vector<int> s(Q), t(Q);
  for (int i = 0; i < Q; i++) {
    std::cin >> s[i] >> t[i];
    s[i]--;
    t[i]--;
  }
  for (int i = 0; i < 304; i++) {
    for (int j = 0; j < 304; j++) {
      dist[i][j] = std::make_pair(1001001001, 0);
    }
  }
  for (int i = 0; i < N; i++) {
    dist[i][i] = std::make_pair(0, L);
    std::priority_queue<node, std::vector<node>, MyComparison> queue;
    queue.push((node){i, 0, L});
    while (queue.size() > 0) {
      auto entry = queue.top();
      queue.pop();
      int v = entry.v;
      int amari = entry.amari;
      int supply = entry.supply;
      if ((dist[i][v].first < supply) ||
          (dist[i][v].first == supply && dist[i][v].second > amari))
        continue;
      for (auto cand : edges[v]) {
        int next_amari = amari - cand.second;
        int next_supply = supply;
        int vv = cand.first;
        if (next_amari < 0) {
          next_amari = L - cand.second;
          next_supply++;
        }
        if ((next_supply < dist[i][vv].first) ||
            (next_supply == dist[i][vv].first &&
             next_amari > dist[i][vv].second)) {
          dist[i][vv].first = next_supply;
          dist[i][vv].second = next_amari;
          queue.push((node){vv, next_supply, next_amari});
          // printf("dist[%d][%d]=(%d, %d)\n", i, vv, dist[i][vv].first,
          // dist[i][vv].second);
        }
      }
    }
  }
  for (int i = 0; i < Q; i++) {
    if (dist[s[i]][t[i]].first == 1001001001) {
      printf("-1\n");
    } else {
      printf("%d\n", dist[s[i]][t[i]].first);
    }
  }
  return 0;
}