代码:
#include <bits/stdc++.h>
using namespace std;
struct ll
{
int x,w;
};
vector <ll> v[2010];
int dis[2010],n,m;
void spfa()
{
memset(dis , -0x3f , sizeof(dis));
queue <int> q;
q.push(1);
dis[1] = 0;
while (!q.empty())
{
int tmp = q.front();
q.pop();
for (auto i : v[tmp])
{
if (dis[i.x] < dis[tmp] + i.w)
{
dis[i.x] = dis[tmp] + i.w;
q.push(i.x);
}
}
}
if (dis[n] > 1e9) cout << "INF" << endl;
else cout << dis[n] << endl;
return;
}
int main()
{
cin >> n >> m;
for (int i = 1 ; i <= m ; i++)
{
int x,y,w;
cin >> x >> y >> w;
v[x].push_back({y , w});
}
spfa();
return 0;
}
玄一关