Submission #6815964


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define range(i,a,n) for(int i=(a);i<(n);i++)

#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};

template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S acm(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
void YN(bool b){cout<<(b?"YES":"NO")<<"\n";}
void Yn(bool b){cout<<(b?"Yes":"No")<<"\n";}
void yn(bool b){cout<<(b?"yes":"no")<<"\n";}

ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s;a=0;} return os;}

template<class T=ll>struct Graph{
	int n;
	vector<vector<tuple<ll,T>>>edge;
	Graph(int N=1):n(N){edge.resize(n);}
	void add(ll f,ll t,T c,bool d=false){
		edge[f].emplace_back(t,c);
		if(!d)edge[t].emplace_back(f,c);
	}
	void view(){
		rep(i,n)for(auto&e:edge[i])
			puta(i,"=>",get<0>(e),", cost :",get<1>(e));
	}
};

template<class T=ll>struct BellmanFord:Graph<T>{
	using Graph<T>::Graph;
	BellmanFord(int N=1):Graph<T>::Graph(N){}
	const T mval=numeric_limits<T>::max()/2;
	vector<T>dist(ll s){
		vector<T> ret(this->n,mval);
		ret[s]=0;
		rep(i,2*this->n){
			rep(f,this->n)for(auto&e:this->edge[f]){
				ll t; T c; tie(t,c)=e;
				if(ret[f]!=mval && chmin(ret[t],ret[f]+c) && i+1>=this->n){
					ret[t]=-mval;
				}
			}
		}
		return ret;
	}
	T dist(ll s,ll t){return dist(s)[t];}
};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n,m,p;
	cin>>n>>m>>p;
	BellmanFord<ll> g(n);
	rep(i,m){
		ll a,b,c;
		cin>>a>>b>>c;
		a--;b--;
		g.add(a,b,-(c-p),true);
	}
	ll ans=g.dist(0,n-1);
	ll out=0;
	if(ans==-4611686018427387903)out=-1;
	else if(ans>0)out=0;
	else out=-ans;
	cout<<out<<endl;;
}

Submission Info

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

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 KB
a02 AC 1 ms 256 KB
a03 AC 1 ms 256 KB
b04 AC 1 ms 256 KB
b05 AC 1 ms 256 KB
b06 AC 1 ms 256 KB
b07 AC 1 ms 256 KB
b08 AC 1 ms 256 KB
b09 AC 32 ms 384 KB
b10 AC 32 ms 384 KB
b11 AC 32 ms 384 KB
b12 AC 30 ms 384 KB
b13 AC 30 ms 384 KB
b14 AC 32 ms 384 KB
b15 AC 2 ms 512 KB
b16 AC 2 ms 384 KB
b17 AC 2 ms 512 KB
b18 AC 32 ms 384 KB
b19 AC 33 ms 384 KB
b20 AC 32 ms 384 KB
b21 AC 32 ms 384 KB
b22 AC 18 ms 384 KB
b23 AC 38 ms 384 KB
b24 AC 18 ms 384 KB
b25 AC 11 ms 384 KB
b26 AC 108 ms 512 KB
b27 AC 108 ms 512 KB
b28 AC 109 ms 512 KB
b29 AC 110 ms 512 KB
b30 AC 109 ms 512 KB
b31 AC 112 ms 512 KB
b32 AC 93 ms 512 KB
b33 AC 91 ms 512 KB
b34 AC 90 ms 512 KB
b35 AC 92 ms 512 KB
b36 AC 92 ms 512 KB
b37 AC 94 ms 512 KB
b38 AC 92 ms 512 KB
b39 AC 90 ms 512 KB
b40 AC 90 ms 512 KB
b41 AC 92 ms 512 KB
b42 AC 89 ms 512 KB
b43 AC 110 ms 512 KB
b44 AC 143 ms 512 KB
b45 AC 103 ms 512 KB
b46 AC 126 ms 512 KB
b47 AC 99 ms 512 KB
b48 AC 24 ms 384 KB
b49 AC 32 ms 384 KB
b50 AC 32 ms 384 KB
b51 AC 32 ms 384 KB
b52 AC 33 ms 384 KB
b53 AC 32 ms 384 KB
b54 AC 36 ms 512 KB
b55 AC 55 ms 512 KB
b56 AC 58 ms 512 KB