結果

問題 No.2712 Play more!
ユーザー 0214sh70214sh7
提出日時 2024-03-31 15:41:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 2,858 bytes
コンパイル時間 2,731 ms
コンパイル使用メモリ 207,160 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-03 12:10:52
合計ジャッジ時間 4,113 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 34 ms
6,676 KB
testcase_08 AC 33 ms
6,676 KB
testcase_09 AC 34 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 23 ms
6,676 KB
testcase_12 AC 22 ms
6,676 KB
testcase_13 AC 19 ms
6,676 KB
testcase_14 AC 30 ms
6,676 KB
testcase_15 AC 80 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 14 ms
6,676 KB
testcase_25 AC 7 ms
6,676 KB
testcase_26 AC 8 ms
6,676 KB
testcase_27 AC 13 ms
6,676 KB
testcase_28 AC 5 ms
6,676 KB
testcase_29 AC 12 ms
6,676 KB
testcase_30 AC 51 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 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> PP;
// #define MOD 1000000007
#define MOD 998244353
#define INF 2305843009213693951
//#define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i,n) for(ll i=0;i<(n);++i)
#define OREP(i,n) for(ll i=1;i<=(n);++i)
#define RREP(i,n) for(ll i=(n)-1;i>=0;--i)
#define ORREP(i,n) for(ll i=(n);i>=1;--i)
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define GOODBYE do { cout << "-1" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug true
#define debug2 false

#define int long long

// 辺
struct Edge
{
	int from;
	int to;
	int cost;
};

// ベルマンフォード法 (1.2 負閉路の影響を受ける頂点を調べる)
// 負の閉路が存在する場合 true を返し, 負閉路の影響を受ける頂点は -INF にセットされる
// distances は頂点数と同じサイズ, 全要素 INF で初期化しておく
bool BellmanFord(const std::vector<Edge>& edges, std::vector<long long>& distances, int startIndex)
{
	distances[startIndex] = 0;

	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;
			}

			// to までの新しい距離
			const long long d = (distances[edge.from] + edge.cost);

			// d が現在の記録より小さければ更新
			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)
		{
			if (distances[edge.from] == INF)
			{
				continue;
			}

			const long long d = (distances[edge.from] + edge.cost);

			if (d < distances[edge.to])
			{
				// 負閉路の影響を受ける頂点を -INF に
				distances[edge.to] = -INF;
			}
		}
	}

	return true;
}

signed main(void){
    //cin.tie(nullptr);
    //ios::sync_with_stdio(false);
    
    ll N,M;
    cin >> N >> M;
    vector<ll> A(N);
    REP(i,N){
        cin >> A[i];
    }

    vector<ll> U(M),V(M),C(M);
    std::vector<Edge> E;
    REP(i,M){
        cin >> U[i] >> V[i] >> C[i];
        U[i]--;
        V[i]--;

        E.push_back(Edge{U[i],V[i],C[i]-A[V[i]]});
        
    }
    
    vector<long long> dis(N,INF);
    bool b = BellmanFord(E,dis,0);
    
    ll Ans = 0;
    if(dis[N-1]==-INF){
        std::cout << "inf" << endl;
        return 0;
    }

    Ans += A[0]-dis[N-1];
    cout << Ans << endl;

    return 0;
    
}
0