RT,5,6T了怀疑写假了=.=
#include<bits/stdc++.h>
using namespace std;
const int maxn=1505;
const int inf=99999999;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
int n,m,start;
queue<int>Q;
int dis[maxn];
bool vis[maxn];
int head[maxn];
int tot=0;
struct node
{
int to,nxt,c;
}edge[maxn];
inline void add(int u,int v,int c)
{
edge[++tot].to=v;
edge[tot].c=c;
edge[tot].nxt=head[u];
head[u]=tot;
}
inline void SPFA()
{
for(int i=1;i<=n;i++)dis[i]=inf,vis[i]=0;
dis[1]=0,vis[1]=1,Q.push(1);
while(!Q.empty())
{
int u=Q.front();Q.pop();vis[u]=0;
for(int i=head[u];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].c)
{
dis[v]=dis[u]+edge[i].c;
if(!vis[v])vis[v]=1,Q.push(v);
}
}
}
}
int main()
{
n=read();m=read();
for(int i=1;i<=m;i++)
{
int u,v,c;
u=read();v=read();c=read();
add(u,v,-c);
}
SPFA();
if(dis[n]==inf)return puts("-1")&0;
printf("%d\n",-dis[n]);
}
方法就是边取反然后跑最短路,最后反过来输出就好了。