answer
The longest road template.
Just a short-circuit on the template of the most symbolic change it \ (+ \) the initial value assigned to \ (--1 \) can be.
Attention must be a one-way side, otherwise there has been no positive ring on the longest road, like there has been no negative loop on most short-circuited.
Only use \ (SPFA \) to write.
(Anyway, there is always the topic of people cancer card
Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <queue>
#define itn int
#define gI gi
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
return f * x;
}
int n, m, tot, head[200003], nxt[200003], edge[200003], ver[200003];
int dis[200003], vis[200003];
inline void add(int u, int v, int w)
{
ver[++tot] = v, edge[tot] = w, nxt[tot] = head[u], head[u] = tot;
}
inline void SPFA()//真正的SPFA
{
queue <int> q;
q.push(1);
dis[1] = 0;
vis[1] = 1;
while (!q.empty())
{
int u = q.front(); q.pop();
vis[u] = 0;
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i], w = edge[i];
if (dis[v] < dis[u] + w)//注意符号
{
dis[v] = dis[u] + w;
if (!vis[v]) {q.push(v); vis[v] = 1;}
}
}
}
}
int main()
{
n = gi(), m = gI();
for (int i = 1; i <= m; i+=1)
{
int u = gi(), v = gI(), w = gi();
add(u, v, w);//单向边
}
memset(dis, -1, sizeof(dis));//赋初值
SPFA();
printf("%d\n", dis[n]);
return 0;
}