結果
問題 |
No.2712 Play more!
|
ユーザー |
👑 |
提出日時 | 2024-03-31 14:39:55 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 140 ms / 2,000 ms |
コード長 | 3,036 bytes |
コンパイル時間 | 3,754 ms |
コンパイル使用メモリ | 263,332 KB |
最終ジャッジ日時 | 2025-02-20 17:21:43 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#include<bits/stdc++.h> #include<atcoder/all> #define rep(i,n) for(int i=0;i<n;i++) using namespace std; using namespace atcoder; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vl; typedef vector<vector<int>> vvi; typedef vector<vector<long long>> vvl; typedef long double ld; typedef pair<int, int> P; ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;} template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;} template <int m> ostream& operator<<(ostream& os, const dynamic_modint<m>& a) {os << a.val(); return os;} template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;} template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} template<typename T> void chmin(T& a, T b){a = min(a, b);} template<typename T> void chmax(T& a, T b){a = max(a, b);} long long const INF = 1001001001001001001; template<typename T> struct Edge{ T cost; int from, to; bool operator<(const Edge<T>& right) const{ return this->cost < right.cost; } }; template<typename T> struct BellmanFord{ bool initialized; int n; int m; vector<T> dist; vector<Edge<T>> G; BellmanFord(int _n) : initialized(false), n(_n), m(0), dist(_n, INF) {} void add_edge(int from, int to, T cost){ G.push_back((Edge<T>){cost, from, to}); m++; } void init(int start){ initialized = true; sort(G.begin(), G.end()); dist[start] = 0; rep(time, n + 5){ bool change = false; rep(i, m){ auto e = G[i]; if(dist[e.from] + e.cost < dist[e.to]){ dist[e.to] = dist[e.from] + e.cost; change = true; } } if(!change) break; if(change) if(time > n){ rep(i, n) dist[i] = -INF; return; } } } T get_dist(int from, int to){ if(!initialized) init(from); return dist[to]; } }; int main(){ int n, m; cin >> n >> m; vector<long long> a(n); cin >> a; BellmanFord<long long> graph(2 * n); rep(i, n) graph.add_edge(i, n + i, -a[i]); vector<tuple<int, int, int>> G; vector<vector<int>> retro(n); rep(i, m){ int u, v, c; cin >> u >> v >> c; u--; v--; G.emplace_back(u, v, c); retro[v].push_back(u); } vector<int> need(n); queue<int> q; q.push(n - 1); while(q.size()){ int from = q.front(); q.pop(); if(need[from]) continue; need[from] = 1; for(int to : retro[from]){ q.push(to); } } // cout << need; for(auto [u, v, c] : G){ if(need[u] == 1 and need[v] == 1) graph.add_edge(n + u, v, c); } long long ans = graph.get_dist(0, 2 * n - 1); if(ans == -INF) cout << "inf\n"; else cout << -ans << "\n"; return 0; }