#include <bits/stdc++.h>
#define MAXN 1501
using namespace std;
int n,m,u,v,w,cnt,head[MAXN],in[MAXN],out[MAXN],t[MAXN];
struct edge{
int nxt,to,w;
}e[50001];
void add(int from,int to,int w)
{
out[from]++;
in[to]++;
e[++cnt]={head[from],to,w};
head[from]=cnt;
}
void bfs()
{
queue <int> q;
q.push(1);
while(!q.empty())
{
int op=q.front();
q.pop();
for(int i=head[op];i;i=e[i].nxt)
{
int eto=e[i].to;
t[eto]=max(t[eto],t[op]+e[i].w);
in[eto]--;
if(!in[eto]) q.push(eto);
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("P1807_1.in","r",stdin);
#endif
memset(t,-128,sizeof(t));
t[1]=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
bfs();
if(t[n]==t[0]) printf("-1");
else printf("%d",t[n]);
}
个人记录