結果

問題 No.2712 Play more!
ユーザー tnakao0123tnakao0123
提出日時 2024-04-30 11:18:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 155 ms / 2,000 ms
コード長 1,711 bytes
コンパイル時間 532 ms
コンパイル使用メモリ 63,744 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-30 11:18:51
合計ジャッジ時間 1,734 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 40 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 155 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 4 ms
6,940 KB
testcase_13 AC 6 ms
6,940 KB
testcase_14 AC 4 ms
6,944 KB
testcase_15 AC 6 ms
6,940 KB
testcase_16 AC 4 ms
6,944 KB
testcase_17 AC 4 ms
6,940 KB
testcase_18 AC 4 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 4 ms
6,944 KB
testcase_21 AC 6 ms
6,940 KB
testcase_22 AC 7 ms
6,944 KB
testcase_23 AC 4 ms
6,940 KB
testcase_24 AC 4 ms
6,944 KB
testcase_25 AC 4 ms
6,940 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 10 ms
6,944 KB
testcase_29 AC 5 ms
6,940 KB
testcase_30 AC 5 ms
6,940 KB
testcase_31 AC 10 ms
6,944 KB
testcase_32 AC 10 ms
6,940 KB
testcase_33 AC 1 ms
6,944 KB
testcase_34 AC 1 ms
6,940 KB
testcase_35 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

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], rnbrs[MAX_N];
ll ds[MAX_N], rds[MAX_N];
bool fs[MAX_N];

/* subroutines */

bool dijkstra(int n, int st, vpii nbrs[], ll ds[], 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});
    rnbrs[b].push_back({a, c});
  }

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

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