Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- // x^y≡x^(y%(p-1))mod p
- // but there is a condition that x needs to be coprime to p
- ///assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z) is(65-90) and 32 for space
- typedef long long int ll;
- typedef pair<ll, ll> pii;
- typedef vector<pii> vii;
- #define pb push_back
- #define f(i,a,b) for(ll i=a;i<b;i++)
- #define fo(i,a,b) for(ll i = a; i<=b;i+=1)
- #define rf(i,a,b) for(ll i=a;i>=b;i--)
- #define vll vector<ll>
- #define tests() int test_cases ; cin >> test_cases ; while(test_cases--)
- #define ub upper_bound
- #define lb lower_bound
- #define all(v) v.begin(),v.end()
- #define MAXN 100010
- #define MOD 1000000007
- #define mod 998244353
- #define deb(x) cout << '>' << #x << ':' << x << endl;
- #define sp " "
- #define MS(x,y) fill_n(*x, sizeof x / sizeof **x, y);
- #define INF 1000000000000000000
- ll power(ll x, ll y,ll p)
- {
- ll res = 1; // Initialize result
- x = x % p;
- if (y==0)return 1;
- if(x==0)return 0;
- while (y > 0)
- {
- if (y & 1)
- res = (res*x) % p;
- y = y>>1; // y = y/2
- x = (x*x) % p; // Change x to x^2
- }
- return res;
- }
- int gcd(int a, int b) { if (a == 0)return b;return gcd(b % a, a);}
- ll modinv(ll a,ll p){return power(a,p-2,p);}
- int solve(){
- ll n,m;
- cin>>n>>m;
- tuple<int,int,int> tup[m];
- tuple<int,int,int> rev[m];
- f(i,0,m){
- int a,b,c;
- cin>>a>>b>>c;
- tup[i] = make_tuple(a,b,-c);
- rev[i] = make_tuple(b,a,-c);
- }
- bool f1 = false,f2 = false;
- ll dist[n+1];
- fo(i,1,n){
- dist[i] = INF;
- }
- dist[1] = 0;
- fo(i,1,n-1){
- fo(j,0,m-1){
- ll u,v,w;
- tie(u,v,w) = tup[j];
- dist[v] = min(dist[v], dist[u]+w);
- }
- }
- fo(j,0,m-1){
- ll u,v,w;
- tie(u,v,w) = tup[j];
- if (dist[v]>dist[u]+w)
- {
- dist[v] = dist[u]+w;
- if (dist[v]<=1e9*5000)
- {
- f1 = true;
- }
- }
- }
- ll store = dist[n];
- fo(i,1,n){
- dist[i] = INF;
- }
- dist[n] = 0;
- fo(i,1,n-1){
- fo(j,0,m-1){
- ll u,v,w;
- tie(u,v,w) = rev[j];
- if (dist[v]>dist[u]+w)
- {
- dist[v] = min(dist[v], dist[u]+w);
- }
- }
- }
- fo(j,0,m-1){
- ll u,v,w;
- tie(u,v,w) = rev[j];
- if (dist[v]>dist[u]+w)
- {
- dist[v] = dist[u]+w;
- if (dist[v]<=1e9*5000)
- {
- f2 = true;
- }
- }
- }
- if (f1 and f2)
- {
- cout<<-1<<endl;
- return 0;
- }
- else {
- cout<<-store<<endl;
- return 0;
- }
- return 0;
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(NULL);
- // #ifndef ONLINE_JUDGE
- // freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- // #endif
- // tests(){solve();}
- solve();
- return 0;
- }
Add Comment
Please, Sign In to add comment