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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
// #include <algorithm>
// #include <bitset>
// #include <deque>
// #include <iostream>
// #include <cfloat>
// #include <cmath>
// #include <cstdarg>
// #include <cstdio>
// #include <cstdlib>
// #include <cstring>
// #include <map>
// #include <numeric>
// #include <queue>
// #include <set>
// #include <sstream>
// #include <stack>
// #include <utility>
// #include <vector>
// #include "ane.cpp"

const int INF  = 1e9;
const long long INFLL = 1e18;
const int NMAX = 1005;
const int MMAX = 2005;
const int MOD  = 1e9 + 7;
using namespace std;

// comment to disable debug functions
// #define DEBUG





//
//  library begin
//

// frequently used macros
#define SORT(v) sort(begin(v), end(v))
#define FIND(v,x) find(begin(v), end(v), (x))
#define MEMNEXT(from, to) do{ memmove((to), (from), sizeof(from)); \
                            memset((from), 0, sizeof(from)); } while(0)
#ifdef DEBUG
#define DUMP(x) do{ std::cerr << "(DUMP) " << (#x) << ": " << x << std::endl; }while(0)
#else
#define DUMP(x) do{}while(0)
#endif

// frequent used aliases
typedef long long ll;
typedef pair<int, int> p;
typedef pair<ll, int> lp;
typedef pair<ll, ll> llp;
typedef vector<int> vec;
typedef vector<vec> mat;

// frequently used constants
static const int di[] = {-1, 0, 1, -1, 1, -1, 0, 1};
static const int dj[] = {-1, -1, -1, 0, 0, 1, 1, 1};

// freawuently used structs
struct edge{
    int to,cost;
};

// printf for debug
void debug(const char* format, ...){
    #ifndef DEBUG
    return;
    #else
    va_list arg;
    va_start(arg, format);
    vprintf(format, arg);
    va_end(arg);
    #endif
}

// fill of multi dimensions
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

// Union-Find Tree
class UnionFindTree{
    struct node{
        int par;
    };
    std::vector<node> T;

    public:
    void init(int num){
        T.resize(num+1);    // ignore T[0]
        for (int i = 1; i <= num; ++i)
        {
            T[i].par = i;
        }
    }
    void unite(int x, int y){
        T[find(y)].par = find(x);
    }
    int find(int x){
        if (T[x].par == x) return x;
        else return T[x].par = find(T[x].par);
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
};

// Segment Tree for Range Minimum Query
    // **********************************************************
    // *** important: all functions' variable,                ***
    // *** such as "index", "l", "r", etc., must be 0-origin. ***
    // **********************************************************
    // ********************************************
    // *** important: NMAX must be a power of 2.***
    // ********************************************
template<typename T>
class SegmentTree{
    private:
    ll N;
    T INF_VAL;
    T dat[NMAX * 2]; // 0 origin, A[i] = dat[i + N]

    T _query(ll l, ll r, ll l_resp, ll r_resp, ll i_dat){
        debug("query(%lld, %lld, %lld, %lld, %lld) called\n",
            l, r, l_resp, r_resp, i_dat);
        if (r < l_resp || r_resp < l) return INF_VAL;
        else if(l <= l_resp && r_resp <= r) return dat[i_dat];
        else return min(_query(l ,r, l_resp, (l_resp + r_resp) / 2, i_dat * 2),
                        _query(l, r, (l_resp + r_resp) / 2 + 1, r_resp, i_dat * 2 + 1));
    }

    public:
    void init(ll _N, T _inf_val){
        N = 1; while(N < _N) N *= 2;
        INF_VAL = _inf_val;
        Fill(dat, _inf_val);
    }
    T get(ll index){
        return dat[index + N];
    }
    void set(int index, T val){
        int i_dat = index + N;
        dat[i_dat] = val;
        for (i_dat /= 2; i_dat > 0; i_dat /= 2)
        {
            dat[i_dat] = min(dat[i_dat * 2], dat[i_dat * 2 + 1]);
        }
    }
    T query(ll l, ll r){
        return _query(l, r, 0, N - 1, 1);
    }
    void dump(){
        cout << "*** SegTree dump begin ***\n";
        cout << "N = " << N << ", INF_VAL = " << INF_VAL << endl;
        for (int i = 1; i < N * 2; i *= 2)
        {
            for (int j = i; j < i * 2; ++j)
            {
                if(dat[j] == INF_VAL) cout << "INF ";
                else cout << dat[j] << " ";
            }
            cout << endl;
        }
        cout << "*** SegTree dump end ***\n";
    }
};

// Binary Indexed Tree for Range Sum Query
    // *******************************************
    // *** important: all functions' variable, ***
    // *** such as "i", must be 1-origin.      ***
    // *******************************************
template<typename T>
class BinaryIndexedTree{
    private:
    ll N;
    T dat[NMAX + 1]; // 1 origin, A[i] = sum(i) - sum(i-1)

    public:
    void init(ll _N){
        N = _N;
        Fill(dat, 0);
    }
    void add(int i, T val){
        while(i <= N) {
            dat[i] += val;
            i += i & -i;
        }
    }
    T sum(ll i){
        T ret = 0;
        while(i > 0) {
            ret += dat[i];
            i -= i & -i; // set last HIGH bit to LOW
        }
        return ret;
    }
    T sum(ll left, ll right){
        return sum(right) - sum(left - 1);
    }
    void dump(){
        cout << "*** BITree dump begin ***\n";
        cout << "N = " << N << endl;
        for (int i = 1; i <= N; i *= 2)
        {
            cout << dat[i] << " ";
        }
        cout << "*** BITree dump end ***\n";
    }
};

//
// library end
//







int N,M,K,A[NMAX],B[NMAX],C[NMAX],D,E;
ll dp[NMAX] = {};
set<int> rt[NMAX][NMAX] = {};
std::vector<edge> G[NMAX];
string S;
vec v;

ll ans = 0;

// void solve(){
//     // main algorithm
//     for (int k = 1; k <= N; ++k)
//     {
//         debug("k = %d\n", k);
//         for (int i = 1; i <= N; ++i)
//         {
//             for (int j = 1; j <= N; ++j)
//             {
//                 if (dp[i][k] != -INFLL && dp[k][j] != -INFLL
//                     && dp[i][j] < dp[i][k] + dp[k][j])
//                 {
//                     // DUMP(dp[i][j]);
//                     dp[i][j] = dp[i][k] + dp[k][j];
//                     // DUMP(dp[i][k]);
//                     // DUMP(dp[k][j]);
//                     set<int> st;
//                     set_union(rt[i][k].begin(), rt[i][k].end(),
//                         rt[k][j].begin(), rt[k][j].end(),
//                         inserter(st, st.begin()));
//                     rt[i][j] = st;
//                     // debug("rt[%d][%d]: ", i, j);
//                     // for(auto&& i : rt[i][j]) {
//                     //     debug("%d -> %d, ",A[i], B[i]);
//                     // }
//                     // debug("\n");
//                 }
//             }
//         }
//     }
// }
void solve2(){
    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < M; ++j)
        {
            if (dp[B[j]] < dp[A[j]] + C[j])
            {
                dp[B[j]] = dp[A[j]] + C[j];
                if (i == N - 1)
                {
                    cout << "inf" << endl;
                    return;
                }
            }
        }
    }
    cout << dp[N] << endl;
}
void debug(){
    // output debug information
    // cout << "rt[1][N]:\n";
    // for(auto&& i : rt[1][N]) {
    //     debug("%d : %d -> %d\n", i, A[i], B[i]);
    // }
}
// void answer(){
//     // output answer
//     for(auto&& i : rt[1][N]) {
//         if (dp[A[i]][A[i]] > 0 || dp[B[i]][B[i]] > 0)
//         {
//             cout << "inf\n";
//             return;
//         }
//     }
//     cout << dp[1][N] << endl;
// }
int main(int argc, char const *argv[])
{
    // operate inputs

    Fill(dp, -INFLL);
    dp[1] = 0;
    scanf("%d%d", &N,&M);
    for (int i = 0; i < M; ++i)
    {
        int a,b,c;
        scanf("%d%d%d", &A[i], &B[i], &C[i]);
        // dp[A[i]][B[i]] = C[i];
        rt[A[i]][B[i]].insert(i);
    }
    solve2();
    #ifdef DEBUG
    debug();
    #endif
    // answer();

    return 0;
}