Submission #6841583


Source Code Expand

Copy
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;

template <typename T> T id(T b) {return b;};
template <class It> bool all(It f,It l){return std::all_of(f,l,id<bool>);}
template <class It> bool any(It f,It l){return std::any_of(f,l,id<bool>);}
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }

struct edge { int from, to, cost; };

const int MAX_N = 2500;
const int MAX_M = 5000;
const ll INF = 10000000000LL;
int n,m,p;
edge edges[MAX_M];
vi g[MAX_N];
ll d[MAX_N];
bool vis[MAX_N];
bool rt[MAX_N];
bool rf[MAX_N];

bool dfs1(int u) {
  if (vis[u]) return rf[u];

  vis[u] = true;

  if (u == n-1) return rf[u] = true;

  for (int v : g[u]) {
    rf[u] |= dfs1(v);
  }

  return rf[u];
}

void dfs2(int u) {
  if (rt[u]) return;

  rt[u] = true;

  for (int v : g[u]) dfs2(v);
}

int main()
{
  cin >> n >> m >> p;
  forn(i,m) {
    int a,b,c; cin >> a >> b >> c;
    a--,b--;
    edges[i].from = a; edges[i].to = b; edges[i].cost = p-c;
    g[a].push_back(b);
  }

  fill_n(d,n,INF);
  d[0] = 0;

  forn(i,n-1) {
    forn(j,m) {
      edge e = edges[j];
      if (d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
      }
    }
  }

  forn(i,n) dfs1(i);
  dfs2(0);

  bool nl = false;
  forn(i,m) {
    edge e = edges[i];
    if (d[e.to] > d[e.from] + e.cost) {
      d[e.to] = d[e.from] + e.cost;
      nl |= rt[e.to] && rf[e.from];
    }
  }


  if (nl)
    cout << -1 << endl;
  else
    cout << max(0LL,-d[n-1]) << endl;

  return 0;
}

Submission Info

Submission Time
Task E - Coins Respawn
User sash0
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2144 Byte
Status AC
Exec Time 70 ms
Memory 512 KB

Judge Result

Set Name Sample All after_contest
Score / Max Score 0 / 0 500 / 500 0 / 0
Status
AC × 3
AC × 56
AC × 3
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49, b50, b51, b52, b53, b54, b55, b56
after_contest after_contest_57, after_contest_58, after_contest_59
Case Name Status Exec Time Memory
a01 AC 1 ms 384 KB
a02 AC 1 ms 384 KB
a03 AC 1 ms 384 KB
after_contest_57 AC 14 ms 512 KB
after_contest_58 AC 14 ms 512 KB
after_contest_59 AC 14 ms 512 KB
b04 AC 1 ms 256 KB
b05 AC 1 ms 256 KB
b06 AC 1 ms 256 KB
b07 AC 1 ms 384 KB
b08 AC 1 ms 384 KB
b09 AC 10 ms 512 KB
b10 AC 14 ms 512 KB
b11 AC 10 ms 512 KB
b12 AC 10 ms 512 KB
b13 AC 10 ms 512 KB
b14 AC 10 ms 512 KB
b15 AC 5 ms 384 KB
b16 AC 5 ms 384 KB
b17 AC 5 ms 384 KB
b18 AC 14 ms 512 KB
b19 AC 14 ms 512 KB
b20 AC 10 ms 512 KB
b21 AC 10 ms 512 KB
b22 AC 11 ms 512 KB
b23 AC 14 ms 512 KB
b24 AC 11 ms 512 KB
b25 AC 1 ms 384 KB
b26 AC 20 ms 512 KB
b27 AC 20 ms 512 KB
b28 AC 19 ms 512 KB
b29 AC 20 ms 512 KB
b30 AC 20 ms 512 KB
b31 AC 19 ms 512 KB
b32 AC 20 ms 512 KB
b33 AC 20 ms 512 KB
b34 AC 21 ms 512 KB
b35 AC 22 ms 512 KB
b36 AC 23 ms 512 KB
b37 AC 23 ms 512 KB
b38 AC 19 ms 512 KB
b39 AC 19 ms 512 KB
b40 AC 19 ms 512 KB
b41 AC 20 ms 512 KB
b42 AC 19 ms 512 KB
b43 AC 66 ms 512 KB
b44 AC 64 ms 512 KB
b45 AC 70 ms 512 KB
b46 AC 58 ms 512 KB
b47 AC 50 ms 512 KB
b48 AC 10 ms 384 KB
b49 AC 14 ms 512 KB
b50 AC 14 ms 512 KB
b51 AC 10 ms 512 KB
b52 AC 14 ms 512 KB
b53 AC 10 ms 512 KB
b54 AC 18 ms 384 KB
b55 AC 26 ms 512 KB
b56 AC 31 ms 512 KB