显示代码纯文本
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=1501;
struct list{
int x,w;
list *next;
list(int a,int b,list *p)
{
x=a, w=b, next=p;
}
}*G[maxn];
int x,y,z,h,t,m,n,k;
int dist[maxn],q[maxn*maxn>>5];
bool v[maxn];
int main()
{
freopen("longest.in","r",stdin);
freopen("longest.out","w",stdout);
scanf("%d%d",&n,&m);
dist[n]=-1;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
G[x]=new list(y,z,G[x]);
}
for(q[h=t=0]=1;h<=t;h++){
int x=q[h];v[x]=false;
for(list *p=G[x];p;p=p->next)
if(dist[p->x]<(k=dist[x]+p->w))
{
dist[p->x]=k;
if(!v[p->x])q[++t]=p->x,v[p->x]=true;
}
}
printf("%d\n",dist[n]);
return 0;
}