結果

問題 No.2712 Play more!
コンテスト
ユーザー ttkkggww
提出日時 2024-05-14 13:47:34
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WJ  
(最新)
AC  
(最初)
実行時間 -
コード長 1,272 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
最終ジャッジ日時 2026-10-16 23:08:33
ジャッジサーバーID
(参考情報)
judge1_0 / judge5_0
このコードへのチャレンジ
(要ログイン)

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
int N,M;
vector<ll> A;
using P = pair<ll,ll>;
vector<vector<P>> G;
vector<vector<P>> nG;


void solve(){
	nG = vector<vector<P>>(N);
	for(int i = 0;i<N;i++){
		for(auto &[to,cost]:G[i]){
			nG[i].push_back({to,-A[to]+cost});
		}
	}
	vector<ll> dist(N,1e18);
	dist[0] = -A[0];
	for(int t= 0;t<N*2;t++){
		for(int i = 0;i<N;i++){
			for(auto [to,cost]:nG[i]){
				if(dist[to]>dist[i]+cost){
					dist[to] = dist[i]+cost;
				}
			}
		}
	}
	vector<int> updated(N);
	ll INF = 1e18;
	for(int t = 0;t<N;t++){
		for(int i = 0;i<N;i++){
			for(auto [to,cost]:nG[i]){
				if(dist[to]>dist[i]+cost){
					dist[to] = dist[i]+cost;
					updated[to] = 1;
				}
			}
		}
	}
	for(int t = 0;t < N;t++){
		for(int i = 0;i<N;i++){
			for(auto [to,cost]:nG[i]){
				if(updated[i]){
					updated[to] = 1;
				}
			}
		}
	}
	if(updated[N-1]){
		cout<< "inf" << endl;
	}
	else{
		cout << -dist[N-1] << endl;
	}
	
}

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> N >> M;
	A.resize(N);
	for(int i = 0; i < N; ++i) cin >> A[i];
	G.resize(N);
	for(int i = 0; i < M; ++i){
		ll a,b,c;
		cin >> a >> b >> c;
		--a; --b;
		G[a].push_back({b,c});
	}
	solve();
}
0