結果

問題 No.2712 Play more!
ユーザー aradarad
提出日時 2024-03-31 13:50:23
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 827 ms / 2,000 ms
コード長 2,938 bytes
コンパイル時間 5,390 ms
コンパイル使用メモリ 321,396 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-03 12:09:01
合計ジャッジ時間 11,072 ms
ジャッジサーバーID
(参考情報)
judge12 / 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 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 460 ms
6,676 KB
testcase_08 AC 457 ms
6,676 KB
testcase_09 AC 456 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 160 ms
6,676 KB
testcase_12 AC 414 ms
6,676 KB
testcase_13 AC 133 ms
6,676 KB
testcase_14 AC 316 ms
6,676 KB
testcase_15 AC 827 ms
6,676 KB
testcase_16 AC 73 ms
6,676 KB
testcase_17 AC 15 ms
6,676 KB
testcase_18 AC 41 ms
6,676 KB
testcase_19 AC 30 ms
6,676 KB
testcase_20 AC 214 ms
6,676 KB
testcase_21 AC 107 ms
6,676 KB
testcase_22 AC 336 ms
6,676 KB
testcase_23 AC 30 ms
6,676 KB
testcase_24 AC 95 ms
6,676 KB
testcase_25 AC 14 ms
6,676 KB
testcase_26 AC 80 ms
6,676 KB
testcase_27 AC 13 ms
6,676 KB
testcase_28 AC 14 ms
6,676 KB
testcase_29 AC 93 ms
6,676 KB
testcase_30 AC 404 ms
6,676 KB
testcase_31 AC 8 ms
6,676 KB
testcase_32 AC 12 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using VS = vector<string>;
using P = pair<ll,ll>;
using VP = vector<P>;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define out(x) cout << x << endl
#define dout(x) cout << fixed << setprecision(10) << x << endl
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)(x.size())
#define re0 return 0
#define pcnt __builtin_popcountll
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr int inf = 1e9;
constexpr ll INF = 1e18;
//using mint = modint1000000007;
using mint = modint998244353;
int di[4] = {1,0,-1,0};
int dj[4] = {0,1,0,-1};

struct Edge{
    int to;
    ll w;
    Edge(int to,ll w):to(to),w(w) {}
};

int main(){
    ll n,m;
    cin >> n >> m;
    VL a(n);
    rep(i,n) cin >> a[i];
    vector<vector<Edge>> g(n);
    vector<vector<Edge>> g2(n);
    rep(i,m){
        ll u,v,c;
        cin >> u >> v >> c;
        u--,v--;
        g[u].emplace_back(v,c-a[v]);
        g2[v].emplace_back(u,c-a[v]);
    }
    auto bfs0 = [&](ll s){
        VL res(n);
        res[s] = 1;
        queue<ll> q;
        q.emplace(s);
        while(sz(q)){
            ll v = q.front();
            q.pop();
            for(auto e:g[v]){
                if(res[e.to]) continue;
                res[e.to] = 1;
                q.emplace(e.to);
            }
        }
        return res;
    };
    auto bfs1 = [&](ll s){
        VL res(n);
        res[s] = 1;
        queue<ll> q;
        q.emplace(s);
        while(sz(q)){
            ll v = q.front();
            q.pop();
            for(auto e:g2[v]){
                if(res[e.to]) continue;
                res[e.to] = 1;
                q.emplace(e.to);
            }
        }
        return res;
    };
    VL dist0 = bfs0(0);
    VL dist1 = bfs1(n-1);
    set<ll> need;
    rep(i,n){
        if(dist0[i]==0) continue;
        if(dist1[i]==0) continue;
        need.insert(i);
    }
    bool exist_negative_cycle = false;
    vector<ll> dist(n,INF);
    dist[0] = 0;
    for(int iter = 0;iter < n;iter++){
        bool update = false;
        for(int v = 0;v < n;v++){
            if(dist[v] == INF) continue;
        
        for(auto e:g[v]){
            if(!need.count(e.to)) continue;
            if(chmin(dist[e.to],dist[v]+e.w)){
                update = true;
            }
        }
        }
        if(!update) break;
        if(iter == n-1 && update) exist_negative_cycle = true;
    }
    if(exist_negative_cycle) cout << "inf" << endl;
    else {
        ll ans = -dist[n-1];
        ans += a[0];
        out(ans);
    }
}


0