結果

問題 No.2712 Play more!
ユーザー うえこうえこ
提出日時 2024-03-31 14:38:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 2,269 bytes
コンパイル時間 1,670 ms
コンパイル使用メモリ 172,128 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-03 12:09:56
合計ジャッジ時間 3,286 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 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 30 ms
6,676 KB
testcase_08 AC 31 ms
6,676 KB
testcase_09 AC 31 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 22 ms
6,676 KB
testcase_12 AC 22 ms
6,676 KB
testcase_13 AC 19 ms
6,676 KB
testcase_14 AC 29 ms
6,676 KB
testcase_15 AC 81 ms
6,676 KB
testcase_16 AC 15 ms
6,676 KB
testcase_17 AC 5 ms
6,676 KB
testcase_18 AC 9 ms
6,676 KB
testcase_19 AC 5 ms
6,676 KB
testcase_20 AC 31 ms
6,676 KB
testcase_21 AC 24 ms
6,676 KB
testcase_22 AC 27 ms
6,676 KB
testcase_23 AC 11 ms
6,676 KB
testcase_24 AC 13 ms
6,676 KB
testcase_25 AC 7 ms
6,676 KB
testcase_26 AC 7 ms
6,676 KB
testcase_27 AC 13 ms
6,676 KB
testcase_28 AC 5 ms
6,676 KB
testcase_29 AC 13 ms
6,676 KB
testcase_30 AC 50 ms
6,676 KB
testcase_31 AC 7 ms
6,676 KB
testcase_32 AC 6 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 ll=long long;
const long long INF=1LL<<60;
struct Edge
{
    int from;
    int to;
    long long cost;
};


// 頂点Nにつながる負の閉路が存在する場合trueを返す
bool BellmanFord(const std::vector<Edge>& edges,std::vector<long long>& distances)
{
    for(size_t i=0;i<distances.size();++i)
    {
        bool changed=false;
        //全ての辺の操作
        for(const auto& edge:edges){
            
            // (INF+cost)はINFなので処理しない
            if(distances[edge.from]==INF){
                continue;
            }
            const long long d = distances[edge.from]+edge.cost;
            
            if(d<distances[edge.to]){
                distances[edge.to]=d;
                changed=true;
            }
        }
        
        if(!changed){
            return false;
        }
    }
    
        //この検証フェーズなしだと負の閉路のあるなしを検出
        //あればゴール地点が負の閉路に繋がっているかを検出

    //検証フェーズ
    //負の閉路からつながる頂点を全て-INFにする
    
    for(size_t i=0;i<distances.size();i++){
        for(const auto& edge:edges){
            
            // (INF+cost)はINFなので処理しない
            if(distances[edge.from]==INF){
                continue;
            }
            const long long d = distances[edge.from]+edge.cost;
            
            if(d<distances[edge.to]){
                distances[edge.to]=-INF;
            }
        }
    }

        //検証フェーズ終わり

    //頂点回数分だけループしても定まらないのは、負の閉路が存在するから
    if(distances.back()==-INF){
        return true;
    }
    return false;
}

int main(){
    ll n,m;
    std::cin >> n >> m;
    std::vector<ll>A(n);
    for(auto&a:A)std::cin >> a;
    std::vector<Edge>edges(m);
    for(auto&edge:edges){
        std::cin >> edge.from >> edge.to >> edge.cost;
        --edge.from;--edge.to;
        edge.cost-=A[edge.to];
    }
    std::vector<ll>distances(n,INF);
    distances[0]=-A[0];
    if(BellmanFord(edges, distances)){
        std::cout << "inf\n";
    }
    else{
        std::cout << -distances.back() << '\n';
    }
}
0