Problem:
Score : 400 points

Problem Statement

There is a directed graph with N vertices and M edges.
The i-th edge (1≤i≤M) points from vertex a_i to vertex b_i, and has a weight c_i.
We will play the following single-player game using this graph and a piece.

Initially, the piece is placed at vertex 1, and the score of the player is set to 0.
The player can move the piece as follows:

When the piece is placed at vertex a_i, move the piece along the i-th edge to vertex b_i. After this move, the score of the player is increased by c_i.

The player can end the game only when the piece is placed at vertex N.
The given graph guarantees that it is possible to traverse from vertex 1 to vertex N.

When the player acts optimally to maximize the score at the end of the game, what will the score be?
If it is possible to increase the score indefinitely, print inf.

Constraints

2≤N≤1000

1≤M≤min(N(N-1),2000)

1≤a_i,b_i≤N (1≤i≤M)

a_i≠b_i (1≤i≤M)

a_i≠a_j or b_i≠b_j (1≤i<j≤M)

-10^9≤c_i≤10^9 (1≤i≤M)

c_i is an integer.

In the given graph, there exists a path from vertex 1 to vertex N.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf.

Sample Input 1

3 3
1 2 4
2 3 3
1 3 5

Sample Output 1

7

There are two ways to move the piece to vertex N=3:

vertex 1 → vertex 2 → vertex 3 : score 4+3=7

vertex 1 → vertex 3 : score 5

Thus, the maximum possible score at the end of the game is 7.

Sample Input 2

2 2
1 2 1
2 1 1

Sample Output 2

inf

It is possible to increase the score indefinitely by alternating between vertex 1 and 2.

Sample Input 3

6 5
1 2 -1000000000
2 3 -1000000000
3 4 -1000000000
4 5 -1000000000
5 6 -1000000000

Sample Output 3

-5000000000

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace ABC061D
{
    class Program
    {
        public static void Solve(Input input)
        {
            var n = input.NextInt();
            var m = input.NextInt();

            var to = Enumerable.Range(0, n)
                .Select(_ => new List<Tuple<int, int>>())
                .ToArray();
            var rto = Enumerable.Range(0, n)
                .Select(_ => new List<Tuple<int, int>>())
                .ToArray();

            var reachableFrom1 = new bool[n];
            var reachableToN = new bool[n];

            for (int i = 0; i < m; i++)
            {
                var a = input.NextInt() - 1;
                var b = input.NextInt() - 1;
                var c = -input.NextInt();

                to[a].Add(new Tuple<int, int>(b, c));
                rto[b].Add(new Tuple<int, int>(a, c));
            }

            dfs(to, reachableFrom1, 0);
            dfs(rto, reachableToN, n - 1);

            var bf = new BellmanFord(n);
            for (int i = 0; i < n; i++)
            {
                foreach (var e in to[i])
                {
                    if (reachableFrom1[e.Item1] && reachableToN[e.Item1])
                    {
                        bf.AddPath(i, e.Item1, e.Item2);
                    }
                }
            }

            var sp = bf.ShortestPath(0);
            if (sp == null)
            {
                Console.WriteLine("inf");
            }
            else
            {
                Console.WriteLine(-sp[n - 1]);
            }
        }

        static void dfs(List<Tuple<int, int>>[] to, bool[] reachable, int s)
        {
            if (reachable[s]) return;

            reachable[s] = true;

            foreach (var e in to[s])
            {
                dfs(to, reachable, e.Item1);
            }
        }

        /// <summary>
        /// 重み付き・有向グラフの単一始点最短経路
        /// 辺の重みが負数でもOK（てか負数ないならDijkstra使って）
        /// </summary>
        class BellmanFord
        {
            public static readonly long Inf = long.MaxValue / 3;

            struct Edge
            {
                public int From;
                public int To;
                public long Cost;

                public Edge(int from, int to, long cost)
                {
                    this.From = from;
                    this.To = to;
                    this.Cost = cost;
                }
            }

            // 接続情報
            readonly List<Edge> es;
            // ノード数
            readonly int V;

            /// <summary>
            /// 
            /// </summary>
            /// <param name="v">ノード数</param>
            public BellmanFord(int v)
            {
                es = new List<Edge>();
                V = v;
            }

            /// <summary>
            /// 経路追加
            /// </summary>
            public void AddPath(int from, int to, long cost)
                => es.Add(new Edge(from, to, cost));

            /// <summary>
            /// sから各ノードへの最短経路
            /// 負の閉路がある場合、nullを返す
            /// </summary>
            public long[] ShortestPath(int s)
            {
                var E = this.es.Count();
                var d = Enumerable.Repeat(Inf, V).ToArray();

                d[s] = 0;
                int count;
                for (count = 0; count < V; count++)
                {
                    var update = false;
                    for (int i = 0; i < E; i++)
                    {
                        var e = es[i];
                        if (d[e.From] != Inf && d[e.To] > d[e.From] + e.Cost)
                        {
                            // こっちの経路の方が効率的
                            d[e.To] = d[e.From] + e.Cost;
                            update = true;
                        }
                    }

                    if (!update) break;
                }

                if (count > V - 1) return null;

                return d;
            }
        }

        #region Main
        public static void Main(string[] args)
        {
            // 出力が少ないときはこれをセットする方が時間かかるけれど
            // そのときにTLEするのはアルゴリズムが悪いせいだし、まあ良しとする
            var needsFlushOutput = true;
            if (needsFlushOutput)
            {
                // 細かく出力しないようにする
                var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
                Console.SetOut(sw);
            }

            // 通常は引数無しで、ファイルから読み出したい場合はpath指定する
            var input = new Input();

            // 仮想的に標準入力をセットする
            // テスト入力したまま提出することがままあるので……
#if DEBUG
            input.SetText("");
#endif

            Solve(input);

            Console.Out.Flush();
        }
        #endregion

        #region Competitive Template
#pragma warning disable CS0414
        static readonly int MOD = (int)1e9 + 7;
        static readonly int[] dx = { 1, 0, -1, 0 };
        static readonly int[] dy = { 0, 1, 0, -1 };
        /// <summary> 左上が原点 </summary>
        static readonly char[] dir = { 'R', 'U', 'L', 'D' };
#pragma warning restore CS0414

        public class Input
        {
            // 変な改行コードがたまに混じっているので、一応セパレート指定する
            // スペース単独指定の方がもちろん早い
            static readonly char[] separator = { ' ', '\r', '\n' };
            readonly StreamReader sr;
            readonly Queue<string> queue;

            /// <summary>
            /// 特定のファイルから読み出したい場合はpath指定する
            /// </summary>
            public Input(string path = "")
            {
                queue = new Queue<string>();

                if (string.IsNullOrEmpty(path)) { sr = new StreamReader(Console.OpenStandardInput()); }
                else { sr = new StreamReader(path); }
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public void SetText(IEnumerable<string> items)
            {
                foreach (var item in items)
                    SetText(item);
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public bool SetText(string s)
            {
                if (string.IsNullOrEmpty(s)) return false;

                foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
                    queue.Enqueue(elem);

                return true;
            }

            /// <summary>
            /// 要素が存在するか
            /// </summary>
            public bool Any() => queue.Any() || Read();

            /// <summary>
            /// 内部queueに入力からの値をsplitして格納する
            /// </summary>
            bool Read()
            {
                if (!SetText(sr.ReadLine())) return false;

                if (!queue.Any()) return Read();

                return queue.Any();
            }

            /// <summary>
            /// 次のstringを一つ読み込む
            /// </summary>
            public string Next()
            {
                if (!queue.Any() && !Read()) return "";

                return queue.Dequeue();
            }

            /// <summary>
            /// 指定個数queueにたまるまでenqueueし続ける
            /// </summary>
            bool Accumulate(int n)
            {
                while (queue.Count() < n)
                    if (!Read()) return false;

                return true;
            }

            public int NextInt() => int.Parse(Next());
            public long NextLong() => long.Parse(Next());
            public double NextDouble() => double.Parse(Next());

            /// <summary>
            /// n個の要素をparseして、それぞれにoffsetをaddした配列を返す
            /// </summary>
            T[] NextT<T>(int n, T offset, Func<string, T> parse, Func<T, T, T> add)
            {
                if (!Accumulate(n)) return null;

                var a = new T[n];

                for (int i = 0; i < n; i++)
                    a[i] = add(parse(queue.Dequeue()), offset);

                return a;
            }

            public string[] Next(int n) => NextT(n, "", x => x, (x, y) => x);
            public int[] NextInt(int n, int offset = 0) => NextT(n, offset, int.Parse, (x, y) => x + y);
            public long[] NextLong(int n, long offset = 0) => NextT(n, offset, long.Parse, (x, y) => x + y);
            public double[] NextDouble(int n, double offset = 0.0) => NextT(n, offset, double.Parse, (x, y) => x + y);
        }

        static class Utils
        {
            public static T Max<T>(params T[] objs) => objs.Max();
            public static T Min<T>(params T[] objs) => objs.Min();

            /// <summary>
            /// vでfillされたT[d1][d2]配列を作成する
            /// </summary>
            public static T[][] Create2DArray<T>(int d1, int d2, T v)
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(v, d2).ToArray()).ToArray();
            }

            /// <summary>
            /// vでfillされたT[d1][d2][d3]配列を作成する
            /// </summary>
            public static T[][][] Create3DArray<T>(int d1, int d2, int d3, T v)
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(0, d2).Select(__ =>
                    Enumerable.Repeat(v, d3).ToArray()).ToArray()).ToArray();
            }
        }
        #endregion
    }
}