-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighScore.cpp
More file actions
73 lines (69 loc) · 1.55 KB
/
Copy pathhighScore.cpp
File metadata and controls
73 lines (69 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
using namespace std;
const int maxSize = 1e5+1;
bool visited[maxSize];
#define ll long long
const ll INF = 1e18;
const ll NINF = -INF;
int main()
{
int n,m,q;
cin>>n>>m;
vector<tuple<int,int,int>>v;
int f,s,c,i;
for(i=1;i<=m;i++)
{
cin>>f>>s>>c;
v.push_back({f,s,-c});
}
ll distance[n+1];
for(i=2;i<=n;i++)
{
distance[i] = INF;
}
distance[1]=0;
for(i=1;i<=n-1;i++)
{
for(auto it:v)
{
int a,b,w;
tie(a,b,w) = it;
if(distance[a] == INF) continue;
if(distance[b]>distance[a]+w)
{
distance[b] = distance[a]+w;
}
if(distance[b]<NINF)
{
distance[b] = NINF;
}
}
}
for(i=1;i<=n-1;i++)
{
for(auto it:v)
{
int a,b,w;
tie(a,b,w) = it;
if(distance[a] == INF) continue;
// this means still a node is unvisited but may contain negative cycle connecting to the end point
// which should not be considered while considering the answer from first node
if(distance[a] == NINF)
{
distance[b] = NINF;
continue;
}
if(distance[b]>distance[a]+w)
{
distance[b] = NINF;
continue;
}
}
}
if(distance[n] == NINF)
{
cout<<-1<<endl;
return 0;
}
cout<<-distance[n];
}