結果

問題 No.2712 Play more!
ユーザー KKT89
提出日時 2024-03-31 14:13:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 309 ms / 2,000 ms
コード長 1,638 bytes
コンパイル時間 3,532 ms
コンパイル使用メモリ 233,892 KB
最終ジャッジ日時 2025-02-20 17:05:06
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,m; cin >> n >> m;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    vector<vector<pair<int,ll>>> g(n);
    vector<vector<int>> rg(n);
    for (int i = 0; i < m; ++i) {
        int x,y,z; cin >> x >> y >> z;
        x -= 1, y -= 1;
        g[x].push_back({y, z});
        rg[y].push_back(x);
    }
    vector<bool> ok(n);
    queue<int> q; q.push(n-1); ok[n-1] = true;
    while (q.size()) {
        int s = q.front(); q.pop();
        for (int t : rg[s]) {
            if (!ok[t]) {
                ok[t] = true;
                q.push(t);
            }
        }
    }
    vector<ll> dp(n, -1e18);
    dp[0] = a[0];
    priority_queue<pair<ll,int>> pq;
    pq.push({dp[0], 0});
    vector<int> cnt(n);
    while (pq.size()) {
        auto p = pq.top(); pq.pop();
        ll score = p.first, j = p.second;
        if (dp[j] != score) continue;
        cnt[j] += 1;
        if (cnt[j] >= 10*n) {
            cout << "inf" << endl;
            return 0;
        }
        for (auto to : g[j]) {
            ll nxt = score - to.second + a[to.first];
            int t = to.first;
            if (!ok[t]) continue;
            if (dp[t] < nxt) {
                dp[t] = nxt;
                pq.push({nxt, t});
            }
        }
    }
    cout << dp.back() << endl;
}
0