結果
| 問題 |
No.2712 Play more!
|
| ユーザー |
emthrm
|
| 提出日時 | 2024-03-31 14:55:07 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,936 bytes |
| コンパイル時間 | 3,439 ms |
| コンパイル使用メモリ | 260,328 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-30 20:06:01 |
| 合計ジャッジ時間 | 4,071 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 WA * 7 |
ソースコード
#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;
}
emthrm