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 Scala (2.11.7) (judge verdict: Accepted)


import collection.mutable.ArrayBuffer
import scala.reflect.ClassTag
import scala.language.implicitConversions

trait Graph { 
    case class Edge[T:Numeric](from : Int, to : Int, cost : T)

    class Edges[T:Numeric](val data : ArrayBuffer[Edge[T]]) {
        def size = data.size
    }
    object Edges {
        def apply[T:Numeric](xs : Edge[T]*) = new Edges(ArrayBuffer(xs:_*))
        implicit def toUnderLying[T](x:Edges[T]) = x.data
    }

    class WeightedGraph[T:Numeric](val size : Int, val data : Array[Edges[T]]) {
        def this(size : Int) = this(size, Array.fill(size)(Edges()))
    }
    object WeightedGraph {
        // def apply[T:Numeric](size : Int) = new WeightedGraph[T](size)
        implicit def toUnderLying[T](x:WeightedGraph[T]) = x.data
    }

    class UnWeightedGraph[T:Numeric](val size : Int, val data: Array[ArrayBuffer[T]]) {
        def this(size : Int) = this(size, Array.fill(size)(ArrayBuffer[T]()))
    }
    object UnWeightedGraph {
        // def apply[T:Numeric](size : Int) = new UnWeightedGraph[T](size)
        implicit def toUnderLying[T](x:UnWeightedGraph[T]) = x.data
    }
   
    class Matrix[T:Numeric:ClassTag](val row : Int, val col : Int, val data : Array[Array[T]]) {
        def this(row : Int, col : Int) = this(row, col, Array.fill(row, col)(implicitly[Numeric[T]].zero))
        def this(size : Int) = this(size, size)
    }
    object Matrix {
        implicit def toUnderLying[T](x:Matrix[T]) = x.data
    }
}

// object BellmanFord extends Graph {
//     import Edges._

//     // return ArrayBuffer() when find a negative cycle
//     def bellmanFord[T:ClassTag](edges : Edges[T], V : Int, s : Int, INF : T)(implicit num : Numeric[T]) : Array[T] = {
//         val dist = Array.fill[T](V)(INF)
//         dist(s) = num.zero
//         for (i <- 1 to V) {
//             for (e <- edges) {
//                 if(dist(e.from) != INF 
//                     && num.lt(num.plus(dist(e.from), e.cost), dist(e.to))) {
//                         dist(e.to) = num.plus(dist(e.from), e.cost)
//                         if (i == V && e.to == V-1) return Array.empty
//                     }
//             }
//         }
//         dist
//     }
// }

object BellmanFord extends Graph {
    import Edges._

    // return ArrayBuffer() when find a negative cycle
    def bellmanFord(edges : ArrayBuffer[(Int, Int, Long)], V : Int, s : Int, INF : Long) : Array[Long] = {
        val dist = Array.fill(V)(INF)
        dist(s) = 0L
        for (i <- 1 to V) {
            for ((from, to, cost) <- edges) {
                if(dist(from) != INF 
                    && (dist(from) + cost) < dist(to)) {
                        dist(to) = (dist(from) + cost)
                        if (i == V && to == V-1) return Array.empty
                    }
            }
        }
        dist
    }
}

import BellmanFord._

object Main {
    def main(args: Array[String]) = {
        val sc = new java.util.Scanner(System.in)
        val N, M = sc.nextInt
        val edges = ArrayBuffer[(Int, Int, Long)]()
        for (_ <- 1 to M) {
            val s, t = sc.nextInt
            val c : Long = sc.nextLong
            edges += ((s-1, t-1, -c))
        }
        // println(edges.toList)
        val res = bellmanFord(edges, N, 0, Long.MaxValue)
        // println(res.toList)
        if (res.isEmpty) println("inf")
        else {
            println(-res(N-1))
        }
    }
}