結果
| 問題 |
No.2712 Play more!
|
| コンテスト | |
| ユーザー |
arad
|
| 提出日時 | 2024-03-31 13:50:23 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 811 ms / 2,000 ms |
| コード長 | 2,938 bytes |
| コンパイル時間 | 5,916 ms |
| コンパイル使用メモリ | 321,232 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-09-30 23:45:01 |
| 合計ジャッジ時間 | 11,043 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#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);
}
}
arad