The5threic1-1

City Problem

Jun 1st, 2024
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | Source Code | 0 0
  1. #include<bits/stdc++.h>
  2. #define endl '\n'
  3. using namespace std;
  4. long long solve(int N, vector<vector<int>> Edges)
  5. {
  6.     int M = Edges.size();
  7.     long long dist[N+2] = { 0 };
  8.     for(int i=2; i<N+2; i++)
  9.     {
  10.         dist[i] = -1e18;
  11.     }
  12.     for(int i=1; i<=N-1; i++)
  13.     {
  14.         for(int j=0; j<M; j++)
  15.         {
  16.             long long u = Edges[j][0], v = Edges[j][1], d = Edges[j][2];
  17.             if(dist[u] != -1e18)
  18.             {
  19.                 dist[v] = max(dist[v], dist[u] + d);
  20.             }
  21.         }
  22.     }
  23.     for(int j=0; j<M; j++)
  24.     {
  25.         long long u = Edges[j][0], v = Edges[j][1], d = Edges[j][2];
  26.         if(dist[u]!=-1e18)
  27.         {
  28.             if(dist[u] + d > dist[v])
  29.             {
  30.                 return -1;
  31.             }
  32.         }  
  33.     }
  34.     return dist[N];
  35. }
  36. int main()
  37. {
  38.     ios::sync_with_stdio(false);
  39.     cin.tie(0);
  40.     int t=1;
  41.     //cin>>t;
  42.     while(t--)
  43.     {
  44.         int N, M;
  45.         cin>>N>>M;
  46.         vector<vector<int>> edges;
  47.         int bozo;
  48.         cin>>bozo;
  49.         for(int i=0; i<M; i++)
  50.         {
  51.             int a, b, c;
  52.             cin>>a>>b>>c;
  53.             edges.push_back({a, b, c});
  54.         }
  55.         cout<<solve(N, edges)<<endl;
  56.     }
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment