結果

問題 No.2712 Play more!
ユーザー tnakao0123tnakao0123
提出日時 2024-04-30 11:15:50
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,550 bytes
コンパイル時間 680 ms
コンパイル使用メモリ 63,288 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-20 05:37:18
合計ジャッジ時間 2,280 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2712.cc:  No.2712 Play more! - yukicoder
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 2500;
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<pii> vpii;

/* global variables */

int as[MAX_N], es[MAX_N];
vpii nbrs[MAX_N];
ll ds[MAX_N];
bool fs[MAX_N];

/* subroutines */

bool dijkstra(int n, int st, bool lpf = false) {
  fill(es, es + n, 0);
  fill(ds, ds + n, LINF);
  ds[st] = 0;

  priority_queue<pli> q;
  q.push({0, st});

  while (! q.empty()) {
    auto [ud, u] = q.top(); q.pop();
    ud = -ud;
    if (ds[u] != ud || es[u] >= n) continue;

    if (lpf && u == st && ds[u] < 0) return true;

    for (auto [v, c]: nbrs[u]) {
      ll vd = ud + c - as[u];
      int ve = es[u] + 1;
      if (ds[v] > vd || (ds[v] == vd && es[v] > ve)) {
	ds[v] = vd, es[v] = ve;
	q.push({-vd, v});
      }
    }
  }

  return false;
}

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < n; i++) scanf("%d", as + i);

  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--, b--;
    nbrs[a].push_back({b, c});
  }

  dijkstra(n, 0);
  for (int u = 0; u < n; u++) fs[u] = (ds[u] < LINF);
  for (int u = 0; u < n; u++)
    if (fs[u] && dijkstra(n, u, true)) { puts("inf"); return 0; }

  dijkstra(n, 0);
  printf("%lld\n", -ds[n - 1] + as[n - 1]);
  
  return 0;
}
0