Language: C++

#include "bits/stdc++.h"
#include <unordered_set>
#define rep(i,n) for(int i = 0; i < n; i++)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define vll vector<vector<long long>>
#define vl vector<long long>
#define vi vector<int>
#define vii vector<vector<int>>
#define pb push_back
#define pf push_front
#define ld long double
#define Sort(a) sort(a.begin(),a.end())
#define cSort(a,cmp) sort(a.begin(),a.end(),cmp)
#define reSort(a) sort(a.rbegin(), a.rend())
static const ll llMAX = numeric_limits<long long>::max();
static const int intMAX = numeric_limits<int>::max();
static const ll  llMIN = numeric_limits<long long>::min();
static const int intMIN = numeric_limits<int>::min();
static const ll d_5 = 100000;
static const ll d9_7 = 1000000007;
static const ll d_9 = 1000000000;
static const double PI=3.14159265358979323846;
template<class T>
T gcd(T a,T b){
  if(a==0){
    return b;

  }else if(b==0){
    return a;
  }
  while(1) {
    if(a < b) swap(a, b);
    if(!b) break;
    a %= b;
  }
  return a;
}
ll digitpower(ll a,ll b){//aのb乗を計算
  if(b==1){
    return a;
  }else if(b==0){
    return 1;
  }
  int mode=0;
  if(mode==0){
    if(b%2==1){
      ll tmp = digitpower(a,(b-1)/2);
      tmp%=d9_7;
      tmp*=tmp;
      tmp%=d9_7;
      tmp*=a;
      return (tmp)%d9_7;
    }else{
      ll tmp = digitpower(a,(b)/2);
      tmp%=d9_7;
      tmp*=tmp;
      tmp%=d9_7;
      return (tmp)%d9_7;
    }
  }else{
    if(b%2==1){
      ll tmp = digitpower(a,(b-1)/2);
      tmp*=tmp;
      tmp*=a;
      return (tmp);
    }else{
      ll tmp = digitpower(a,(b)/2);
      tmp*=tmp;
      return (tmp);
    }
  }
  return 0;
  
}
template<class T>
void Printvector(std::vector<T> &a){
  int size = a.size();
  rep(i,size){
    cout<<a[i]<<" ";
  }
  cout<<endl;
}
template<class T>
void Printvector(std::vector<std::vector<T> > &a){
  int size = a.size();
  rep(i,size){
    int size2=a[i].size();
    rep(j,size2){
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }
  cout<<endl;
}
unordered_set<ll> primes(ll n){
  unordered_set<ll> ret;
  for(ll i=2;i*i<=n;i++){
    if(n%i==0){
      ret.insert(i);
      ret.insert(n/i);
    }
  }
  return ret;
}
long long modinv(long long a, long long m) {//modの逆元
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m; 
    if (u < 0) u += m;
    return u;
}
struct c{
  ll a;
  ll b;
};
template<class T>
vector<T> getaccum(vector<T> a){
  int size=a.size();
  vector<T> ans(size);
  ans[0]=a[0];
  for(int i=0;i<size-1;i++){
    ans[i+1]=ans[i]+a[i+1];
    //ans[i+1]%=d9_7;
  }
  return ans;
}
// 比較関数を定義
bool cmp( const c& left, const c& right ) {
    return left.b>right.b;
}
template<class T>
/*

このBaseedgeクラスをなぜかクラスとして認識してくれない

*/
class Baseedge {
  public:
    int number;
    int v1;
    int v2;
    T length;
    T cost;
    Baseedge(int inputfrom , int inputto, T inputlength,T inputcost,int inputnumber){
      this->v1 = inputfrom;
      this->v2 = inputto;
      this->number = inputnumber;
      this->length = inputlength;
      this->cost = inputcost;
    }
    virtual int nextvertex(int from)=0;
};
template<class T>
class Uniedge : public Baseedge<T> {
  public:

    Uniedge(int inputfrom , int inputto, T inputlength,T inputcost,int inputnumber) : 
      Baseedge<T>(inputfrom,inputto,inputlength,inputcost, inputnumber){
        ;
      }
    
    int nextvertex(int from) override {
      return this->v2;
    }
};
template<class T>
class Biedge : public Baseedge<T> {
  public:

    Biedge(int inputfrom , int inputto, T inputlength,T inputcost,int inputnumber) : 
      Baseedge<T>(inputfrom,inputto,inputlength,inputcost, inputnumber){
        ;
      }
    int nextvertex(int from) override {
      if(this->v1==from){
        return this->v2;
      }else if(this->v2==from){
        return this->v1;
      }else if(this->v1 == this->v2){
        return this->v1;
      }else{
        printf("ERROR Invalid number \n");
        return -1;

      }
    }
};
template<class T>
class graph {
  public:
  int n;//ノード数
  
  vector<Baseedge<ll>* > es;
  struct vertex{
    int number;
    set<int> edges;
  };
  vector<vertex> vs;
  //constructor
  graph(int input){
    n=input;
    vs=vector<vertex>(n);
    for(int i=0;i<n;i++){
      vs[i].number=i;
    }
  }
  void bind_uni(int from,int to,T len,T cost){
    //1方向につなぐ
    int num=es.size();
    Baseedge<T>* newedge = new Uniedge<T>(from,to,len,cost,num);
    es.push_back(newedge);
    vs[from].edges.insert(num);
  }
  void bind_bi(int v1,int v2,T len,T cost){
    //2方向につなぐ
    int num=es.size();
    Baseedge<T>* newedge = new Biedge<T>(v1,v2,len,cost,num);
    es.push_back(newedge);
    vs[v1].edges.insert(num);
    vs[v2].edges.insert(num);
  }
  void DFS(int now,T length,T cost,int pre=-1){
    for(auto i:vs[now].edges){
      int next=es[i]->nextvertex(now);
      if(next==pre)continue;
      DFS(next,length+es[i]->length,cost+es[i]->cost,now);
    }
  }
  void BFS(int ini){
    struct opera{
      int now;
      int pre;
      T len;
      T cost;
    };
    queue<opera> opes;
    opes.push_back({ini,-1,0,0});
    while(opes.size()>0){
      int now=opes.front().now;
      int pre = opes.front().pre;
      T len = opes.front().len;
      T cost = opes.front().cost;
      for(auto i:vs[now].edges){
        int next=es[i]->nextvertex(now);
        if(next==pre)continue;
        opes.push({next,now,len+es[i]->length,cost+es[i]->cost});
      }
      opes.pop();
    }
  }
  //スタートからゴールに向かう頂点を列挙
  unordered_set<int> Find_relatedvertex(int ini,int goal){
    struct opera{
      int now;
      int pre;
      unordered_set<int> past;
    };
    queue<opera> opes;
    opes.push_back({ini,-1,{}});
    unordered_set<int> ret;
    ret.insert(goal);
    while(opes.size()>0){
      int now=opes.front().now;
      int pre=opes.front().pre;
      unordered_set<int> pasts=opes.front().past;
      if(ret.find(now)!=ret.end()){
        opes.pop();
        continue;
      }
      if(goal==now){
        for(auto i:pasts){
          ret.insert(i);
        }
        opes.pop();
        continue;
      }
      unordered_set<int> tmp = pasts;
      for(auto i:vs[now].edges){
        int next=es[i]->nextvertex(now);
        if(next==pre)continue;
        tmp.insert(next);
        opes.push({next,now,tmp});
        tmp.erase(next);
      }
      opes.pop();
    }
    return ret;
  }
  unordered_set<int> need;
  void Bellman_Ford(int ini,int edgenum){
    struct opera{
      int now;
      int pre;
      T len;
      T cost;
    };
    queue<opera> opes;
    opes.push({ini,-1,0,0});
    int cnt=0;
    const ll INF =1<<60;
    vector<T> vertexcost(n,INF);
    vertexcost[0]=0;
    int roopflag=0;
    while(opes.size()>0 && roopflag==0){
      int now=opes.front().now;
      int pre = opes.front().pre;
      T len = opes.front().len;
      T cost = opes.front().cost;
      if(vertexcost[now]==INF){
        vertexcost[now]=cost;
      }else{
        vertexcost[now]=max(cost,vertexcost[now]);
      }
      if(G.need.find(now)==G.need.end()){
        opes.pop();
        continue;
      }
      //
      cnt++;
      if(cnt>=(edgenum+1)*(n+1)){
        //負経路、ループ検出
        cout<<-1<<endl;
        roopflag=1;
        break;
      }
      for(auto i:vs[now].edges){
        int next=es[i]->nextvertex(now);
        if(next==pre && es[i]->v1!=es[i]->v2)continue;
        if(cost+es[i]->cost <= vertexcost[next] && vertexcost[next]!=INF)continue;
        opes.push({next,now,len+es[i]->length,cost+es[i]->cost});
      }
      opes.pop();
    }
    if(roopflag==0){
      //閉路がないので解答出力
      cout<<vertexcost[n-1]<<endl;
    }
  }
};
int main(void){
  ll n,m,p;
  cin>>n>>m>>p;
  graph<ll> G(n);
  rep(i,m){
    ll c;
    int a,b;
    cin>>a>>b>>c;
    a--;b--;
    G.bind_uni(a,b,1,c-p);
  }
  G.need=G.Find_relatedvertex(0,n-1);
  G.Bellman_Ford(0,m);
  return 0;
}

//clang++ -g -fsanitize=address -fno-omit-frame-pointer 1.cpp -o test.exe

//<<std::setprecision(30)

//重複削除
 /* std::sort(vec.begin(), vec.end());
 
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());*/

  //ペアの全探索
  /*do{
		int s=0;
		for(int i=0;i<n/2;i++)s+=v[i]^v[i+n/2];
		r=max(s,r);
		reverse(v.begin()+n/2,v.end());
	}while(next_permutation(v.begin(),v.end()));*/


Compiler output:
a.cc: In member function 'void graph<T>::Bellman_Ford(int, int)':
a.cc:306:20: warning: left shift count >= width of type [-Wshift-count-overflow]
  306 |     const ll INF =1<<60;
      |                   ~^~~~
a.cc:306:20: warning: left shift count >= width of type [-Wshift-count-overflow]
a.cc:320:10: error: 'G' was not declared in this scope
  320 |       if(G.need.find(now)==G.need.end()){
      |          ^
a.cc: In instantiation of 'std::unordered_set<int> graph<T>::Find_relatedvertex(int, int) [with T = long long int]':
a.cc:357:30:   required from here
  357 |   G.need=G.Find_relatedvertex(0,n-1);
      |          ~~~~~~~~~~~~~~~~~~~~^~~~~~~
a.cc:265:10: error: 'class std::queue<graph<long long int>::Find_relatedvertex(int, int)::opera, std::deque<graph<long long int>::Find_relatedvertex(int, int)::opera, std::allocator<graph<long long int>::Find_relatedvertex(int, int)::opera> > >' has no member named 'push_back'
  265 |     opes.push_back({ini,-1,{}});
      |     ~~~~~^~~~~~~~~
a.cc: In instantiation of 'void graph<T>::Bellman_Ford(int, int) [with T = long long int]':
a.cc:358:17:   required from here
  358 |   G.Bellman_Ford(0,m);
      |   ~~~~~~~~~~~~~~^~~~~
a.cc:306:20: warning: left shift count >= width of type [-Wshift-count-overflow]
  306 |     const ll INF =1<<60;
      |                   ~^~~~