-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestPath.java
118 lines (100 loc) · 3.23 KB
/
LongestPath.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Longest Path
// You will be given a graph from the console, your task is to find the longest path to the D destination vertex and then print the weight of that path.
// Input
// • The input comes from the console.
// First is an integer the number of nodes,
// then the number of edges, after that, each edge on a new line in the following format "{source} {destination} {weight}".
// Then you will read two integers on a separate line, the source, and destination nodes.
// Output
// • Print on a single line the weight of that path.
// input:
// 6
// 11
// 1 2 5
// 1 3 3
// 2 4 6
// 2 3 2
// 3 5 4
// 3 6 2
// 3 4 7
// 4 6 1
// 4 5 3
// 5 6 4
// 6 2 1
// 1
// 6
// output: 21
// input2:
// 4
// 4
// 1 2 5
// 1 3 3
// 3 4 6
// 4 2 4
// 1
// 2
// output2: 13
import java.io.*;
import java.util.*;
public class LongestPath {
public static class Edge {
int destination;
int weight;
public Edge(int destination, int weight) {
this.destination = destination;
this.weight = weight;
}
}
public static void topologicalSort(int v, boolean visited[], Stack<Integer> stack, List<List<Edge>> graph) {
visited[v] = true;
for (Edge edge : graph.get(v)) {
if (!visited[edge.destination]) {
topologicalSort(edge.destination, visited, stack, graph);
}
}
stack.push(v);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int nodes = Integer.parseInt(br.readLine().trim());
int edges = Integer.parseInt(br.readLine().trim());
List<List<Edge>> graph = new ArrayList<>();
for (int i = 0; i <= nodes; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < edges; i++) {
String[] line = br.readLine().split(" ");
int source = Integer.parseInt(line[0]);
int destination = Integer.parseInt(line[1]);
int weight = Integer.parseInt(line[2]);
graph.get(source).add(new Edge(destination, weight));
}
int source = Integer.parseInt(br.readLine().trim());
int destination = Integer.parseInt(br.readLine().trim());
Stack<Integer> stack = new Stack<Integer>();
boolean[] visited = new boolean[nodes + 1];
for (int i = 1; i <= nodes; i++) {
if (!visited[i]) {
topologicalSort(i, visited, stack, graph);
}
}
int[] distances = new int[nodes + 1];
Arrays.fill(distances, Integer.MIN_VALUE);
distances[source] = 0;
while (!stack.isEmpty()) {
int u = stack.pop();
if (distances[u] != Integer.MIN_VALUE) {
for (Edge edge : graph.get(u)) {
if (distances[edge.destination] < distances[u] + edge.weight) {
distances[edge.destination] = distances[u] + edge.weight;
}
}
}
}
bw.write(String.valueOf(distances[destination]));
bw.newLine();
bw.flush();
bw.close();
}
}