Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define endl '\n'
- using namespace std;
- long long solve(int N, vector<vector<int>> Edges)
- {
- int M = Edges.size();
- long long dist[N+2] = { 0 };
- for(int i=2; i<N+2; i++)
- {
- dist[i] = -1e18;
- }
- for(int i=1; i<=N-1; i++)
- {
- for(int j=0; j<M; j++)
- {
- long long u = Edges[j][0], v = Edges[j][1], d = Edges[j][2];
- if(dist[u] != -1e18)
- {
- dist[v] = max(dist[v], dist[u] + d);
- }
- }
- }
- for(int j=0; j<M; j++)
- {
- long long u = Edges[j][0], v = Edges[j][1], d = Edges[j][2];
- if(dist[u]!=-1e18)
- {
- if(dist[u] + d > dist[v])
- {
- return -1;
- }
- }
- }
- return dist[N];
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0);
- int t=1;
- //cin>>t;
- while(t--)
- {
- int N, M;
- cin>>N>>M;
- vector<vector<int>> edges;
- int bozo;
- cin>>bozo;
- for(int i=0; i<M; i++)
- {
- int a, b, c;
- cin>>a>>b>>c;
- edges.push_back({a, b, c});
- }
- cout<<solve(N, edges)<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment