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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;

//System.out.println();
public class Main implements Runnable { //クラス名はScoreAttack

	public static void main(String[] args) {
		Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.exit(1));
		new Thread(null, new Main(), "", 16 * 1024 * 1024).start(); //16MBスタックを確保して実行
	}

	public void run() {
		FastScannerScoreAttack sc = new FastScannerScoreAttack(System.in);

		try {
			int N = sc.nextInt();
			int M = sc.nextInt();
			int[][] adj = new int[M][3];

			for (int i = 0; i < M; i++) {
				adj[i][0] = sc.nextInt() - 1;
				adj[i][1] = sc.nextInt() - 1;
				int cost = sc.nextInt();
				// 最長を求める問題なので、マイナスに変換すると、最小のマイナス値が返却される。なので、マイナスをつける。
				// ベルマンフォード法は最小経路を求めるアルゴリズムであるため。
				adj[i][2] = -cost;

			}

			BellmanFord bel = new BellmanFord(N, adj);
			Response res = bel.bellmanford(0, N - 1);

			if (res.result) {
				System.out.println(-res.costs);//マイナスをつけて符号を逆転
			} else {
				System.out.println("inf");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

//有効グラムのみ対応
class BellmanFord {

	private Edge[] edges;
	private int n;

	/*
	 * n 頂点数
	 * adj 有効グラフの配列
	 */
	public BellmanFord(int v, int[][] adj) {

		this.n = v;
		//辺情報の配列を作る
		List<Edge> list = new ArrayList<Edge>();
		for (int i = 0; i < adj.length; i++) {
			list.add(new Edge(adj[i][0], adj[i][1], adj[i][2]));
		}

		//Source > Targetの順に並べなおす。
		Collections.sort(list, new Comparator<Edge>() {
			@Override
			public int compare(Edge a, Edge b) {
				if (a.source < b.source) {
					return -1;
				} else if (a.source > b.source) {
					return 1;
				} else {
					//ここに来るということは、sourceが同じだということなので、次の条件で比較する。
					return a.target - b.target;
				}
			}
		});

		for (Edge e : list) {
			System.out.println((e.source + 1) + " " + (e.target + 1) + " " + e.cost);
		}

		this.edges = list.toArray(new Edge[0]); //配列に変換

	}

	/*
	 * ベルマン-フォード法[単一始点最短経路(Single Source Shortest Path)]
	 * s : start地点
	 * g : goal地点
	 */

	Response bellmanford(int s, int g) {
		long[] distance = new long[n]; //始点からの最短距離
		int[] count = new int[n]; //更新カウント(負の閉路チェック用)

		Arrays.fill(distance, Long.MAX_VALUE); //各頂点までの距離を初期化(INF 値)
		distance[s] = 0; //始点の距離は０

		boolean negative = false; //負の閉路フラグ
		boolean update = true; //更新フラグ

		System.out.println(edges);
		while (update && !negative) {
			update = false;
			for (Edge e : edges) {
				System.out.println((e.source + 1) + " " + (e.target + 1) + " " + e.cost);
				//接続元＋接続先までの距離
				if (distance[e.source] != Integer.MAX_VALUE && distance[e.source] + e.cost < distance[e.target]) {
					System.out.println(" Pass1  " + distance[e.source] + " " + e.cost + " " + distance[e.target]);
					distance[e.target] = distance[e.source] + e.cost; //現在記録されている距離より小さければ更新
					update = true;

					if (++count[e.target] >= n) { //負の閉路チェック
						System.out.println(" Pass2");
						negative = true;
						break;
					}
				}
			}
		}

		Response res = new Response();
		if (negative) {
			res.result = false;
			res.costs = Long.MIN_VALUE;
			return res; //負の閉路があったときは、-INF(Integer.MIN_VALUE)を返す
		}
		res.result = true;
		res.costs = distance[g];
		return res; //到達できなかったときは、INF(Integer.MAX_VALUE)になる
	}

	//辺情報の構造体
	class Edge {
		public int source = 0; //接続元ノード
		public int target = 0; //接続先ノード
		public int cost = 0; //重み

		public Edge(int source, int target, int cost) {
			this.source = source;
			this.target = target;
			this.cost = cost;
		}

		public int compareTo(Edge o) {
			return this.cost - o.cost;
		}
	}
}

//Response
class Response {
	public boolean result;
	public long costs;
}

//高速なScanner
class FastScannerScoreAttack {
	private BufferedReader reader = null;
	private StringTokenizer tokenizer = null;

	public FastScannerScoreAttack(InputStream in) {
		reader = new BufferedReader(new InputStreamReader(in));
		tokenizer = null;
	}

	public String next() {
		if (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public String nextLine() {
		if (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				return reader.readLine();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken("\n");
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}

	public int[] nextIntArray(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = nextInt();
		return a;
	}

	public long[] nextLongArray(int n) {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = nextLong();
		return a;
	}
}