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: Time Limit Exceeded)

fn main() {
  let (n, m): (usize, usize) = get::read_tuple();
  
  let edges: Vec<(usize, usize, isize)> = get::read_3tuples(m);
  let edges = edges.iter().map(|&(a,b,c)|(a-1,b-1,-c)).collect::<Vec<_>>();
  
  let g = Graph::new_labeled(n, &edges);
  let dist = g.bellman_ford(0);
  let dist2 = g.bellman_ford2(0);
  
  if dist[n-1]==dist2[n-1] {
    println!("{}", -dist[n-1])
  } else {
    println!("inf")
  }
}

struct Graph {
  size: usize,
  adj: Vec<Vec<(usize,isize)>>
}

impl Clone for Graph {
  fn clone(&self) -> Graph {
    Graph {
      size: self.size,
      adj: self.adj.clone()
    }
  }
}

#[allow(dead_code)]
impl Graph {
  fn new_nonlabeled(n: usize, edges: &[(usize,usize)]) -> Graph {
    let edges = edges.iter().map(|&(a,b)|(a,b,0isize)).collect::<Vec<_>>();
    Graph::new_labeled(n, &edges)
  }

  fn new_labeled(n: usize, edges: &[(usize,usize,isize)]) -> Graph {
    let mut g: Vec<Vec<(usize,isize)>> = vec![vec![]; n];
    for &(a, b, c) in edges {
      g[a].push((b,c));
    }
    Graph {size: n, adj: g}
  }

  fn is_connected(&self) -> bool {
    self.dfs(0).len() == self.size
  }

  fn size(&self) -> usize {
    self.size
  }

  fn edges_in(&self, v: usize) -> Vec<(usize, isize)> {
    let mut buf = vec![];
    for (i, next) in self.adj.iter().enumerate() {
      for &(j, x) in next {
        if j == v {buf.push((i, x))}
      }
    }
    buf
  }

  fn bellman_ford(&self, s: usize) -> Vec<isize> {
    let inf = isize::max_value() >> 1;
    let mut bf: Vec<isize> = vec![inf; self.size];
    bf[s] = 0;

    for _ in 1 .. self.size {
      for i in 0 .. self.size {
        let es = self.edges_in(i);
        let cand = es.iter().map(|&(v,c)|bf[v]+c).min().unwrap_or(inf);
        bf[i] = std::cmp::min(bf[i], cand);
      }
    }
    bf   
  }

  fn bellman_ford2(&self, s: usize) -> Vec<isize> {
    let inf = isize::max_value() >> 1;
    let mut bf: Vec<isize> = vec![inf; self.size];
    bf[s] = 0;

    for _ in 1 .. self.size*2 {
      for i in 0 .. self.size {
        let es = self.edges_in(i);
        let cand = es.iter().map(|&(v,c)|bf[v]+c).min().unwrap_or(inf);
        bf[i] = std::cmp::min(bf[i], cand);
      }
    }
    bf   
  }

  fn dijkstra(&self, s: usize) -> Vec<isize> {
    use std::collections::BinaryHeap;

    let inf = isize::max_value();
    let mut dk: Vec<isize> = vec![inf; self.size];
    dk[s] = 0;

    let mut pq = BinaryHeap::new();
    pq.push((0,s));

    while let Some((acc,v)) = pq.pop() {
      let acc = -acc;
      for &(w,c) in &self.adj[v] {
        let cand = acc + c;
        if cand < dk[w] {
          dk[w] = cand;
          pq.push((-cand,w));
        }
      }
    }
    dk
  }

  fn warshall_floyd(&self) -> Vec<Vec<isize>> {
    let inf = isize::max_value() >> 1;
    let mut wf: Vec<Vec<isize>> = vec![vec![inf; self.size]; self.size];
    for i in 0 .. self.size {wf[i][i] = 0}

    for (i, next) in self.adj.iter().enumerate() {
      for &(j, x) in next {
        wf[i][j] = x
      }
    }
    for k in 0 .. self.size {
      for i in 0 .. self.size {
        for j in 0 .. self.size {
          wf[i][j] = std::cmp::min(wf[i][j], wf[i][k] + wf[k][j]);
        }
      }
    }
    wf
  }

  fn coloring2(&self) -> Option<(Vec<usize>, Vec<usize>)> {
    fn paint(v: usize, p: bool, g: &Graph, cv: &mut Vec<Option<bool>>) -> bool {
      match cv[v] {
        None => {
          let next = &g.adj[v];
          cv[v] = Some(p);
          next.iter().all(|&(w,_)|paint(w,!p,g,cv))},
        Some(q) => {q == p}
      }
    }

    let mut canvas: Vec<Option<bool>> = vec![None; self.size];
    let ans = paint(0, false, self, &mut canvas);
    let bs = canvas.iter().enumerate().filter(|&(_,&v)|v==Some(false)).map(|(i,_)|i).collect::<Vec<_>>();
    let ws = canvas.iter().enumerate().filter(|&(_,&v)|v==Some(true)).map(|(i,_)|i).collect::<Vec<_>>();
    if ans {Some((bs,ws))} else {None}
  }

  fn dfs(&self, s: usize) -> Vec<usize> {
    fn go(g: &Graph, current: usize, mut path: &mut Vec<usize>, mut visited: &mut Vec<bool>) {
      for &(next, _) in &g.adj[current] {
        if visited[next] {
          continue
        } else {
          visited[next] = true;
          path.push(next);
          go(&g, next, &mut path, &mut visited)
        }
      }
    }

    let mut path = vec![s];
    let mut visited = vec![false; self.adj.len()];
    visited[s] = true;
    go(&self, s, &mut path, &mut visited);
    path
  }

  fn cut(&mut self, v: usize, w: usize) {
    self.adj[v].retain(|&(t,_)| t != w);
    self.adj[w].retain(|&(t,_)| t != v);
  }
}

#[allow(dead_code)]
mod get {
  use std::io::*;
  use std::str::*;

  pub fn read<T: FromStr>() -> T {
    let mut buf = String::new();
    stdin().read_line(&mut buf).ok();
    buf.trim().parse::<T>().ok().unwrap()
  }

  pub fn reads<T: FromStr>(n: usize) -> Vec<T> {
    let mut vec: Vec<T> = vec![];
    for _ in 0 .. n {
      vec.push(read());
    }
    vec
  }

  pub fn read_tuple<T1: FromStr, T2: FromStr>() -> (T1, T2) {
    let mut buf = String::new();
    stdin().read_line(&mut buf).ok();
    let mut it = buf.trim().split_whitespace();
    let x = it.next().unwrap().parse::<T1>().ok().unwrap();
    let y = it.next().unwrap().parse::<T2>().ok().unwrap();
    (x, y)
  }

  pub fn read_tuples<T1: FromStr, T2: FromStr>(n: usize) -> Vec<(T1, T2)> {
    let mut vec: Vec<(T1, T2)> = vec![];
    for _ in 0 .. n {
      vec.push(read_tuple());
    }
    vec
  }

  pub fn read_3tuple<T1: FromStr, T2: FromStr, T3: FromStr>() -> (T1, T2, T3) {
    let mut buf = String::new();
    stdin().read_line(&mut buf).ok();
    let mut it = buf.trim().split_whitespace();
    let x = it.next().unwrap().parse::<T1>().ok().unwrap();
    let y = it.next().unwrap().parse::<T2>().ok().unwrap();
    let z = it.next().unwrap().parse::<T3>().ok().unwrap();
    (x, y, z)
  }

  pub fn read_3tuples<T1: FromStr, T2: FromStr, T3: FromStr>(n: usize) -> Vec<(T1, T2, T3)> {
    let mut vec: Vec<(T1, T2, T3)> = vec![];
    for _ in 0 .. n {
      vec.push(read_3tuple());
    }
    vec
  }

  pub fn read_vec<T: FromStr>() -> Vec<T> {
    let mut buf = String::new();
    stdin().read_line(&mut buf).ok();
    buf.trim().split_whitespace().map(|t| t.parse::<T>().ok().unwrap()).collect()
  }

  pub fn read_mat<T: FromStr>(h: usize) -> Vec<Vec<T>> {
    let mut mat: Vec<Vec<T>> = vec![];
    for _ in 0 .. h {
      mat.push(read_vec());
    }
    mat
  }
}