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 Rust (1.15.1) (judge verdict: Wrong Answer)

use std::collections::BinaryHeap;
use std::collections::VecDeque;
use std::cmp;

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.read();
    let m: usize = sc.read();

    let mut graph = vec![vec![]; n];
    let mut unweighted = vec![vec![]; n];
    for _ in 0..m {
        let a: usize = sc.read::<usize>() - 1;
        let b: usize = sc.read::<usize>() - 1;
        let c: i64 = sc.read();
        graph[a].push((b, c));
        unweighted[a].push(b);
    }

    let scc = strongly_connected_components::decompose(&unweighted);
    let mut groups: Vec<Vec<usize>> = vec![vec![]; n];
    for i in 0..n {
        groups[scc[i]].push(i);
    }

    let mut inf = vec![false; n];
    let mut dist: Vec<i64> = vec![std::i64::MIN; n];
    for i in 0..n {
        if groups[i].is_empty() { continue; }
        let start = groups[i][0];
        let mut heap = BinaryHeap::new();
        dist[start] = 0;
        heap.push((start, 0));
        while !heap.is_empty() {
            let (v, cost) = heap.pop().unwrap();
            if v == start && cost > 0 {
                inf[i] = true;
                break;
            }
            for e in &graph[v] {
                let (to, c) = *e;
                if scc[to] != scc[start] { continue; }
                if dist[to] >= dist[v] + c { continue; }
                dist[to] = dist[v] + c;
                heap.push((to, dist[to]));
            }
        }
    }

    let forward = bfs(0, &unweighted);
    let backward = bfs(n - 1, &unweighted);
    for i in 0..n {
        if forward[i] && backward[i] && inf[scc[i]] {
            println!("inf");
            return;
        }
    }

    let mut heap = BinaryHeap::new();
    let mut dist = vec![std::i64::MIN; n];
    dist[0] = 0;
    heap.push((0, 0));
    while !heap.is_empty() {
        let (v, _) = heap.pop().unwrap();
        for e in &graph[v] {
            let (to, c) = *e;
            if dist[to] >= dist[v] + c { continue; }
            dist[to] = dist[v] + c;
            heap.push((to, dist[to]));
        }
    }
    println!("{}", dist[n - 1]);
    return;
}

fn bfs(from: usize, graph: &Vec<Vec<usize>>) -> Vec<bool> {
    let n = graph.len();
    let mut deque = VecDeque::new();
    let mut ok = vec![false; n];
    ok[from] = true;
    deque.push_back(from);
    while !deque.is_empty() {
        let v = deque.pop_front().unwrap();
        for to in &graph[v] {
            if ok[*to] { continue; }
            ok[*to] = true;
            deque.push_back(*to);
        }
    }
    return ok;
}

pub mod strongly_connected_components {
    use std::collections::VecDeque;

    pub fn decompose(graph: &Vec<Vec<usize>>) -> Vec<usize> {
        let mut vs = Vec::new();
        let num_v = graph.len();
        let mut cmp = vec![0; num_v];

        let mut reverse_graph = vec![vec![]; num_v];
        for i in 0..num_v {
            for v in &graph[i] {
                reverse_graph[*v].push(i);
            }
        }
        let mut used = vec![false; num_v];

        let mut stack = VecDeque::new();
        let mut added = vec![false; num_v];
        for i in 0..num_v {
            if used[i] { continue; }
            stack.push_front(i);
            while !stack.is_empty() {
                let v = stack.pop_front().unwrap();
                stack.push_front(v);
                used[v] = true;
                let mut pushed = false;
                for j in (0..graph[v].len()).rev() {
                    let u = graph[v][j];
                    if !used[u] {
                        stack.push_front(u);
                        pushed = true;
                    }
                }
                if !pushed {
                    stack.pop_front();
                    if !added[v] {
                        vs.push(v);
                        added[v] = true;
                    }
                }
            }
        }

        used = vec![false; num_v];
        let mut k = 0;
        vs.reverse();
        for i in &vs {
            let i = *i;
            if used[i] { continue; }
            stack.push_front(i);
            used[i] = true;
            cmp[i] = k;
            while !stack.is_empty() {
                let v = stack.pop_front().unwrap();
                stack.push_front(v);
                let mut pushed = false;
                for u in &reverse_graph[v] {
                    let u = *u;
                    if used[u] { continue; }
                    used[u] = true;
                    cmp[u] = k;
                    stack.push_front(u);
                    pushed = true;
                }
                if !pushed {
                    stack.pop_front();
                }
            }
            k += 1;
        }

        return cmp;
    }
}

struct Scanner {
    ptr: usize,
    length: usize,
    buf: Vec<u8>,
    small_cache: Vec<u8>,
}

impl Scanner {
    fn new() -> Scanner {
        Scanner { ptr: 0, length: 0, buf: vec![0; 1024], small_cache: vec![0; 1024] }
    }

    fn load(&mut self) {
        use std::io::Read;
        let mut s = std::io::stdin();
        self.length = s.read(&mut self.buf).unwrap();
    }

    fn byte(&mut self) -> u8 {
        if self.ptr >= self.length {
            self.ptr = 0;
            self.load();
            if self.length == 0 {
                self.buf[0] = b'\n';
                self.length = 1;
            }
        }

        self.ptr += 1;
        return self.buf[self.ptr - 1];
    }

    fn is_space(b: u8) -> bool { b == b'\n' || b == b'\r' || b == b'\t' || b == b' ' }

    fn read<T>(&mut self) -> T where T: std::str::FromStr, T::Err: std::fmt::Debug, {
        let mut b = self.byte();
        while Scanner::is_space(b) {
            b = self.byte();
        }

        for pos in 0..self.small_cache.len() {
            self.small_cache[pos] = b;
            b = self.byte();
            if Scanner::is_space(b) {
                return String::from_utf8_lossy(&self.small_cache[0..(pos + 1)]).parse().unwrap();
            }
        }

        let mut v = self.small_cache.clone();
        while !Scanner::is_space(b) {
            v.push(b);
            b = self.byte();
        }
        return String::from_utf8_lossy(&v).parse().unwrap();
    }
}