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: Runtime Error)

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

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
        int pointCount = inpt[0];

        canReach = new bool[pointCount+1];
        for (int i = 0; i < inpt[1]; i++)
        {
            long[] inpt2 = Reader.ReadLine().Split(' ').Select(a => long.Parse(a)).ToArray();
            int fromPoint = (int)inpt2[0];
            int toPoint = (int)inpt2[1];
            if (!Road.ContainsKey(fromPoint))
            {
                Road.Add(fromPoint, new Dictionary<int, long>());
            }
            Road[fromPoint][toPoint] = inpt2[2];
            if (!RoadRev.ContainsKey(toPoint))
            {
                RoadRev.Add(toPoint, new Dictionary<int, long>());
            }
            RoadRev[toPoint][fromPoint] = inpt2[2];
        }

        //　ゴール頂点を含むループがあるか確認
        Queue<int> que = new Queue<int>();
        canReach[pointCount] = true;
        RoadRev[pointCount].ToList().ForEach(a =>
        {
            canReach[a.Key] = true;
            que.Enqueue(a.Key);
        });
        while (que.Count > 0)
        {
            int current = que.Dequeue();
            if (!RoadRev.ContainsKey(current))
            {
                continue;
            }
            RoadRev[current].ToList().ForEach(a =>
            {
                if (!canReach[a.Key])
                {
                    canReach[a.Key] = true;
                    que.Enqueue(a.Key);
                }
            });
        }
        bool[] flags = new bool[pointCount+1];
        flags[1] = true;
        long ans = GetAns(1, flags);
        if (ans == long.MaxValue)
        {
            Console.WriteLine("inf");
        }
        else
        {
            Console.WriteLine(ans);
        }
    }



    private Dictionary<int, Dictionary<int, long>> Road = new Dictionary<int, Dictionary<int, long>>();
    private Dictionary<int, Dictionary<int, long>> RoadRev = new Dictionary<int, Dictionary<int, long>>();
    private bool[] canReach;

    private Dictionary<int, long> dic = new Dictionary<int, long>();

    private long GetAns(int pos, bool[] flag)
    {
        if (dic.ContainsKey(pos))
        {
            return dic[pos];
        }
        if (!Road.ContainsKey(pos))
        {
            if (pos == flag.Length - 1)
            {
                dic.Add(pos, 0);
                return 0;

            }
            else
            {
                dic.Add(pos, long.MinValue);
                return long.MinValue;
            }
        }
        long ans = long.MinValue;
        if (pos == flag.Length - 1)
        {
            ans = 0;
        }
        foreach (int nextPos in Road[pos].Keys)
        {
            if(!canReach[nextPos]) {
                continue;
            }
            if (flag[nextPos])
            {
                if (IsPositiveRoop(pos, nextPos))
                {
                    ans = long.MaxValue;
                    break;
                }
                else
                {
                    continue;
                }
            }
            else
            {
                flag[nextPos] = true;
                long rete = GetAns(nextPos, flag);
                flag[nextPos] = false;
                if (rete == long.MaxValue)
                {
                    ans = long.MaxValue;
                    break;
                }
                else if (rete == long.MinValue)
                {
                    continue;
                } else if(ans == long.MinValue) 
                {
                    ans = rete + Road[pos][nextPos];
                }
                else
                {
                    ans = Math.Max(ans, rete + Road[pos][nextPos]);
                }
            }
        }
        dic[pos] = ans;
        return ans;
    }


    private bool IsPositiveRoop(int pos1, int pos2)
    {
        Queue<int> que = new Queue<int>();
        Nullable<long>[] score = new Nullable<long>[canReach.Length];
        score[pos1] = 0;
        que.Enqueue(pos1);
        while (que.Count > 0)
        {
            int current = que.Dequeue();
            if (!Road.ContainsKey(current))
            {
                continue;
            }
            foreach (int next in Road[current].Keys)
            {
                long nextVal = score[current].Value + Road[current][next];
                if (score[next] == null || (score[next].Value < 0 && nextVal >= 0))
                {
                    score[next] = nextVal;
                    if (next != pos2)
                    {
                        que.Enqueue(current);
                    }
                }
            }
        }
        long max1 = score[pos2].Value;
        score = new Nullable<long>[canReach.Length];
        score[pos2] = 0;
        que.Enqueue(pos2);
        while (que.Count > 0)
        {
            int current = que.Dequeue();
            if (!Road.ContainsKey(current))
            {
                continue;
            }
            foreach (int next in Road[current].Keys)
            {
                long nextVal = score[current].Value + Road[current][next];
                if (score[next] == null || (score[next].Value < 0&& nextVal>=0))
                {
                    score[next] = nextVal;
                    if (next != pos1)
                    {
                        que.Enqueue(current);
                    }
                }
            }
        }
        long max2 = score[pos1].Value;
        return (max1 + max2 >= 0);

    }

    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


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



";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}