题目链接:https://www.luogu.org/problemnew/show/P1807
求最大路?就是把权值取相反数跑最短路。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 100000;
const int inf = 0x7fffffff;
bool vis[maxn];
struct edge{
int from, next, len, to;
}e[maxn];
int head[maxn], cnt, dis[maxn], ans, m, n, u, v, w;
queue<int> q;
void add(int u, int v, int w)
{
e[++cnt].from = u;
e[cnt].to = v;
e[cnt].len = w;
head[u] = cnt;
}
int SPFA()
{
while(!q.empty())
{
int now = q.front();
q.pop();
vis[now] = 0;
for(int i = head[now]; i; i = e[i].next)
{
if(dis[e[i].to] > dis[now] + e[i].len)
{
dis[e[i].to] = dis[now] + e[i].len;
if(!vis[e[i].to])
{
vis[e[i].to] = 1;
q.push(e[i].to);
}
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d",&u,&v,&w);
w = w*-1;
add(u,v,w);
}
int s = 1;
dis[s] = 0;
q.push(s);
vis[s] = 1;
SPFA();
if(dis[n] != 0)
printf("%d",abs(dis[n]));
else
printf("-1");
return 0;
}