rt,SPFA WA on #1 #7
#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,f=1;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-')
f=-1;
ch=getchar();
}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
void write(int x){
if(x<0)
putchar('-');
if(x>9)
write(x/10);
putchar(x%10+'0');
}
int f,n,m,pos,s,w,t,head[30005],cnt[30005],dis[30005];
bool vis[30005];
struct node{
int u,v,w,next;
}e[50005];
void addEdge(int u,int v,int w){
e[pos]={u,v,w,head[u]};
head[u]=pos++;
}
void SPFA(){
queue<int>q;
memset(dis,-0x3f,sizeof(dis));
dis[1]=0;
q.push(1);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];~i;i=e[i].next){
int u=e[i].u,v=e[i].v,w=e[i].w;
if(dis[v]<dis[u]+w){
dis[v]=dis[u]+w;
if(!vis[v])
vis[v]=true,q.push(v);
}
}
}
}
int main(){
memset(head,-1,sizeof(head));
n=read(),m=read();
for(int i=1;i<=m;i++){
s=read(),w=read(),t=read();
addEdge(s,w,t);
}
SPFA();
if(dis[n]==-0x3f3f3f3f3f3f3f3f)
printf("-1\n");
else
printf("%d",dis[n]);
return 0;
}