From 2ac0bd53e5ad19b366936e353f5ff5abed3b4fe9 Mon Sep 17 00:00:00 2001 From: sejalphatangare <91753180+sejalphatangare@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:41:23 +0530 Subject: [PATCH] Create HighScore.java --- HighScore.java | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 HighScore.java diff --git a/HighScore.java b/HighScore.java new file mode 100644 index 0000000..4a8e357 --- /dev/null +++ b/HighScore.java @@ -0,0 +1,75 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + +class Custom { + int from; + int to; + int weight; + + public Custom(int from, int to, int weight) { + this.from = from; + this.to = to; + this.weight = weight; + } +} + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int max = (int) 1e17; + int min = (int) -1e17; + + int n = scanner.nextInt(); + int m = scanner.nextInt(); + + ArrayList edges = new ArrayList<>(); + + for (int i = 0; i < m; i++) { + int from = scanner.nextInt(); + int to = scanner.nextInt(); + int weight = scanner.nextInt(); + weight *= -1; + edges.add(new Custom(from, to, weight)); + } + + int[] dist = new int[n + 1]; + Arrays.fill(dist, max); + dist[1] = 0; + + // Bellman-Ford Algorithm + for (int i = 0; i < n; i++) { + for (Custom e : edges) { + int u = e.from; + int v = e.to; + int w = e.weight; + if (dist[u] == max) { + continue; + } + dist[v] = Math.min(dist[v], dist[u] + w); + dist[v] = Math.max(dist[v], min); + } + } + + for (int i = 0; i < n; i++) { + for (Custom e : edges) { + int u = e.from; + int v = e.to; + int w = e.weight; + if (dist[u] == max) { + continue; + } + dist[v] = Math.max(dist[v],min); + if (dist[v] > dist[u] + w) { + dist[v] = min; + } + } + } + + if (dist[n] == min || dist[n] == max) { + System.out.println(-1); + } else { + System.out.println(-1 * dist[n]); + } + } +}