結果

問題 No.2712 Play more!
ユーザー a1048576
提出日時 2024-04-06 19:24:43
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 371 ms / 2,000 ms
コード長 1,305 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 184,852 KB
実行使用メモリ 199,424 KB
最終ジャッジ日時 2024-10-01 04:00:48
合計ジャッジ時間 6,727 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int n,m;
  cin >> n >> m;
  vector<vector<pair<int,int>>> a(n*2);
  vector<vector<int>> b(n*2);
  for(int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[i].push_back({n+i,-x});
    b[n+i].push_back(i);
  }
  while(m--) {
    int x,y,w;
    cin >> x >> y >> w;
    x--,y--;
    a[x+n].push_back({y,w});
    b[y].push_back(x+n);
  }
  vector<bool> vis(n*2,false);
  vis[n*2-1] = true;
  deque<int> Q;
  Q.push_back(n*2-1);
  while(Q.size() > 0) {
    int x = Q.front();
    Q.pop_front();
    int l = b[x].size();
    for(int i = 0; i < l; i++) {
      int y = b[x][i];
      if(!vis[y]) {
        vis[y] = true;
        Q.push_back(y);
      }
    }
  }
  vector<vector<int>> dp(n*2+1,vector<int>(n*2,1e18));
  dp[0][0] = 0;
  for(int r = 0; r < n*2; r++) {
  	for(int i = 0; i < n*2; i++) dp[r+1][i] = dp[r][i];
    for(int i = 0; i < n*2; i++) {
      int l = a[i].size();
      for(int j = 0; j < l; j++) {
        int y,w;
        tie(y,w) = a[i][j];
        if(dp[r+1][y] > dp[r][i]+w) {
          if(r == n*2-1 && vis[y]) {
            cout << "inf" << endl;
            return 0;
          }
          dp[r+1][y] = min(dp[r+1][y],dp[r][i]+w);
        }
      }
    }
  }
  cout << dp[n*2][n*2-1]*-1 << endl;
}
0