import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        int X = Integer.parseInt(st.nextToken());
        int Y = Integer.parseInt(st.nextToken());

        int[][] graph = new int[N+1][N+1];
        for (int i=0; i<N+1; i++) {
            for (int j=0; j<N+1; j++) {
                if (i==j) graph[i][j] = 0;
                else graph[i][j] = 999;
            }
        }

        for (int i=0; i<M; i++) {
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
            graph[a][b] = c;
        }


        for (int k=1; k<N+1; k++) {
            for (int i=1; i<N+1; i++) {
                for (int j=1; j<N+1; j++) {
                    if (graph[i][k] != 999 && graph[k][j] != 999) {
                        graph[i][j] = Math.min(graph[i][j], graph[i][k]+graph[k][j]);
                    }
                }
            }
        }

        
        boolean isNegativeCycle = false;
        for (int i=1; i<N+1; i++) {
            if(graph[i][i] <0){
                isNegativeCycle = true;
                break;   
                }
        }

        System.out.println(isNegativeCycle ? "Inf" : graph[X][Y]);

        // for (int i=1; i<N+1; i++) {
        //         for (int j=1; j<N+1; j++) {
        //         System.out.print(graph[i][j] + " ");
        //         }
        //     System.out.println();
        // }
        

        // StringBuilder sb = new StringBuilder();
        
        // for (int q=0; q<Q; q++) {
        //     st = new StringTokenizer(br.readLine());
        //     int d = Integer.parseInt(st.nextToken());
        //     int e = Integer.parseInt(st.nextToken());
        //     sb.append(graph[d][e]).append("\n");
        // }

        
        
        
        

        
        
       
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: