Submission #6838554


Source Code Expand

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001; //check the limits, dummy

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);    
    int N; cin >> N;
    
    int M; cin >> M;
    
    ll P; cin >> P;
    vector<vpl > graph(N);
    vector<vi > graph2(N);
    F0R(i, M) {
		int A, B, C; cin >> A >> B >> C; A--; B--; C-=P;
		graph[A].pb(mp(B, C)); 
		graph2[B].pb(A);
	}
	
	bool foundEnd[N]; F0R(i, N) foundEnd[i] = false;
	queue<int> q;
	q.push(N-1);
	foundEnd[N-1] = true;
	while (!q.empty()) {
		int cur = q.front(); q.pop();
		F0R(i, sz(graph2[cur])) {
			int nxt = graph2[cur][i];
			if (!foundEnd[nxt]) {
				foundEnd[nxt] = true;
				q.push(nxt);
			}
		}
	}
	
	ll dist[N];
	F0R(i, N) {
		dist[i] = -1000000000000;
	}
	dist[0] = 0;
	
	bool found[N]; F0R(i, N) found[i] = false;
	
	found[0] = true;
	
	F0R(i, N) {
		F0R(j, N) {
			if (dist[j] == -1000000000000) continue;
			F0R(k, sz(graph[j])) {
				int nxt = graph[j][k].f;
				found[nxt] = true;
				if (dist[nxt] < dist[j] + graph[j][k].s) {
					dist[nxt] = dist[j]+graph[j][k].s;
				}
			
			}
		}
	}
	
	/*F0R(i, N) {
		cout << dist[i] << " ";
	} cout << endl;*/
	
	F0R(i, N) {
		F0R(j, sz(graph[i])) {
		
			int k = graph[i][j].f;
			if (dist[k] == -1000000000000 || dist[i] == -1000000000000) continue;
			if (dist[k] < dist[i] + graph[i][j].s && foundEnd[k]) {
				cout << -1 << endl; return 0;
			}
		}
	}
	
	cout << max(0LL, dist[N-1]) << endl;
	
    return 0;
}

// read the question correctly (ll vs int)
// template by bqi343

Submission Info

Submission Time
Task E - Coins Respawn
User Geothermal
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2715 Byte
Status AC
Exec Time 91 ms
Memory 768 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 56
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
Case Name Status Exec Time Memory
a01 AC 1 ms 256 KiB
a02 AC 1 ms 256 KiB
a03 AC 1 ms 256 KiB
b04 AC 1 ms 256 KiB
b05 AC 1 ms 256 KiB
b06 AC 1 ms 256 KiB
b07 AC 1 ms 256 KiB
b08 AC 1 ms 256 KiB
b09 AC 22 ms 512 KiB
b10 AC 23 ms 512 KiB
b11 AC 24 ms 512 KiB
b12 AC 22 ms 512 KiB
b13 AC 23 ms 512 KiB
b14 AC 23 ms 512 KiB
b15 AC 2 ms 512 KiB
b16 AC 2 ms 512 KiB
b17 AC 2 ms 512 KiB
b18 AC 21 ms 512 KiB
b19 AC 23 ms 512 KiB
b20 AC 24 ms 512 KiB
b21 AC 23 ms 512 KiB
b22 AC 8 ms 512 KiB
b23 AC 23 ms 512 KiB
b24 AC 8 ms 512 KiB
b25 AC 7 ms 384 KiB
b26 AC 53 ms 640 KiB
b27 AC 52 ms 640 KiB
b28 AC 53 ms 640 KiB
b29 AC 54 ms 640 KiB
b30 AC 53 ms 640 KiB
b31 AC 53 ms 640 KiB
b32 AC 51 ms 640 KiB
b33 AC 51 ms 640 KiB
b34 AC 53 ms 640 KiB
b35 AC 54 ms 640 KiB
b36 AC 56 ms 640 KiB
b37 AC 56 ms 640 KiB
b38 AC 53 ms 640 KiB
b39 AC 53 ms 640 KiB
b40 AC 52 ms 640 KiB
b41 AC 52 ms 640 KiB
b42 AC 52 ms 768 KiB
b43 AC 71 ms 640 KiB
b44 AC 91 ms 640 KiB
b45 AC 54 ms 640 KiB
b46 AC 75 ms 640 KiB
b47 AC 9 ms 640 KiB
b48 AC 17 ms 512 KiB
b49 AC 24 ms 512 KiB
b50 AC 25 ms 512 KiB
b51 AC 23 ms 512 KiB
b52 AC 25 ms 512 KiB
b53 AC 24 ms 512 KiB
b54 AC 23 ms 640 KiB
b55 AC 35 ms 640 KiB
b56 AC 38 ms 640 KiB