結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 55);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

bool flag = 0;
//bellman_ford
template <typename T>
std::vector<ll> bellman_ford(T &G, int start, int lim = -1){
    // T -> vector<vector<pair<ll, int or ll>>> 
    // edge pair<ll, ll> -> pair<cost, to>
    int n = G.size();
    std::vector<ll> dis(n, (1LL << 59));
    dis[start] = 0;
    
    lim = 20000;

    for(int i = 0; i < lim; i ++) {// 2N 回やると検出とか楽
        bool update = 0;
        for(int j = 0; j < n; j ++) {
            for(auto [cost, to] : G[j]) {
                if(dis[j] + cost < dis[to]) {
                    dis[to] = dis[j] + cost;
                    if(i >= 10000 and to == n - 1) {
                        if(dis[to] < n * (1LL << 30)) {
                            flag = 1;
                            return dis;
                        }
                    }
                    
                    update = 1;
                }
            }
        }
        if(!update) break;
    }
    return dis; 
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    vector<vector<pair<ll, ll>>> v(n * 2);
    rep(i, m) {
        ll x, y, c;
        cin >> x >> y >> c;
        x --; y --;
        v[x * 2 + 1].push_back({c, y * 2});
    }
    rep(i, n) v[i*2].push_back({-a[i], i*2+1});

    auto ret = bellman_ford(v, 0);
    if(flag) {
        cout << "inf" << endl;
        return 0;
    }
    assert(-(1LL << 30) * n <= ret.back() and ret.back() <= (1LL << 30) * n);
    cout << - ret.back() << endl;
    return 0;
}
0