結果

問題 No.2712 Play more!
ユーザー 👑 emthrmemthrm
提出日時 2024-03-31 14:55:07
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,936 bytes
コンパイル時間 3,142 ms
コンパイル使用メモリ 259,600 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-31 14:55:17
合計ジャッジ時間 4,230 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 3 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 22 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 9 ms
6,676 KB
testcase_12 WA -
testcase_13 AC 12 ms
6,676 KB
testcase_14 WA -
testcase_15 AC 62 ms
6,676 KB
testcase_16 AC 6 ms
6,676 KB
testcase_17 AC 3 ms
6,676 KB
testcase_18 AC 6 ms
6,676 KB
testcase_19 AC 3 ms
6,676 KB
testcase_20 AC 19 ms
6,676 KB
testcase_21 AC 10 ms
6,676 KB
testcase_22 AC 35 ms
6,676 KB
testcase_23 AC 5 ms
6,676 KB
testcase_24 AC 6 ms
6,676 KB
testcase_25 AC 3 ms
6,676 KB
testcase_26 AC 7 ms
6,676 KB
testcase_27 WA -
testcase_28 AC 4 ms
6,676 KB
testcase_29 AC 8 ms
6,676 KB
testcase_30 AC 68 ms
6,676 KB
testcase_31 AC 4 ms
6,676 KB
testcase_32 AC 4 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename CostType>
struct Edge {
  CostType cost;
  int src, dst;

  explicit Edge(const int src, const int dst, const CostType cost = 0)
      : cost(cost), src(src), dst(dst) {}

  auto operator<=>(const Edge& x) const = default;
};

template <typename CostType>
struct BellmanFord {
  const CostType inf;
  std::vector<CostType> dist;

  vector<bool> is_updated_;

  BellmanFord(const std::vector<std::vector<Edge<CostType>>>& graph,
              const CostType inf = std::numeric_limits<CostType>::max())
      : inf(inf), is_updated_(graph.size()), is_built(false), graph(graph) {}

  bool has_negative_cycle(const int s) {
    is_built = true;
    const int n = graph.size();
    dist.assign(n, inf);
    dist[s] = 0;
    prev.assign(n, -1);
    for (int step = 0; step < n; ++step) {
      fill(is_updated_.begin(), is_updated_.end(), false);
      bool is_updated = false;
      for (int i = 0; i < n; ++i) {
        if (dist[i] == inf) continue;
        for (const Edge<CostType>& e : graph[i]) {
          if (dist[e.dst] > dist[i] + e.cost) {
            dist[e.dst] = dist[i] + e.cost;
            prev[e.dst] = i;
            is_updated = true;
            is_updated_[e.dst] = true;
          }
        }
      }
      if (!is_updated) return false;
    }
    return true;
  }

  std::vector<int> build_path(int t) const {
    assert(is_built);
    std::vector<int> res;
    for (; t != -1; t = prev[t]) {
      res.emplace_back(t);
    }
    std::reverse(res.begin(), res.end());
    return res;
  }

 private:
  bool is_built;
  std::vector<int> prev;
  std::vector<std::vector<Edge<CostType>>> graph;
};

int main() {
  int n, m; cin >> n >> m;
  vector<int> A(n);
  for (int& A_i : A) cin >> A_i;
  vector<vector<Edge<ll>>> graph(n);
  while (m--) {
    int a, b, c; cin >> a >> b >> c; --a; --b;
    graph[a].emplace_back(a, b, c - A[a]);
  }
  BellmanFord<ll> bellman_ford(graph);
  bellman_ford.has_negative_cycle(0);
  if (bellman_ford.is_updated_[n - 1]) {
    cout << "inf\n";
  } else {
    cout << -bellman_ford.dist[n - 1] + A[n - 1] << '\n';
  }
  return 0;
}
0