-
Notifications
You must be signed in to change notification settings - Fork 0
/
Undefined.java
132 lines (116 loc) · 3.72 KB
/
Undefined.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Undefined
// You will be given a graph from the console.
// Your task is to find the shortest path and print it as a sequence from S
// source vertex to D destination vertex and then on the second line,
// the weight of that path, be careful there might be negative cycles if there
// are printed "Undefined".
// 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 path found separated by spaces and on the second
// line the weight of that path, or if there is no path, message "Negative Cycle
// Detected".
// Input1
// 5
// 8
// 1 2 -1
// 1 3 4
// 2 3 3
// 2 4 2
// 2 5 2
// 4 2 1
// 4 3 5
// 5 4 -3
// 1
// 4
// Output1
// 1 2 5 4
// -2
// Input2
// 5
// 8
// 1 2 -1
// 1 3 4
// 2 3 3
// 2 4 2
// 2 5 2
// 4 2 -1
// 4 3 5
// 5 4 -3
// 1
// 4
// Output2
// Undefined
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class Undefined {
public static class Edge {
int from, to, weight;
public Edge(int from, int to, int weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer;
int nodes = Integer.parseInt(reader.readLine());
int edges = Integer.parseInt(reader.readLine());
List<Edge> graph = new ArrayList<>();
int[] dist = new int[nodes];
int[] prev = new int[nodes];
Arrays.fill(dist, Integer.MAX_VALUE);
Arrays.fill(prev, -1);
for (int i = 0; i < edges; i++) {
tokenizer = new StringTokenizer(reader.readLine());
int from = Integer.parseInt(tokenizer.nextToken()) - 1;
int to = Integer.parseInt(tokenizer.nextToken()) - 1;
int weight = Integer.parseInt(tokenizer.nextToken());
graph.add(new Edge(from, to, weight));
}
tokenizer = new StringTokenizer(reader.readLine());
int source = Integer.parseInt(tokenizer.nextToken()) - 1;
tokenizer = new StringTokenizer(reader.readLine());
int destination = Integer.parseInt(tokenizer.nextToken()) - 1;
dist[source] = 0;
for (int i = 1; i <= nodes - 1; ++i) {
for (Edge edge : graph) {
if (dist[edge.from] != Integer.MAX_VALUE && dist[edge.from] + edge.weight < dist[edge.to]) {
dist[edge.to] = dist[edge.from] + edge.weight;
prev[edge.to] = edge.from;
}
}
}
for (Edge edge : graph) {
if (dist[edge.from] != Integer.MAX_VALUE && dist[edge.from] + edge.weight < dist[edge.to]) {
System.out.println("Undefined");
return;
}
}
if (dist[destination] == Integer.MAX_VALUE) {
System.out.println("Undefined");
return;
}
List<Integer> path = new ArrayList<>();
for (int at = destination; at != -1; at = prev[at]) {
path.add(at + 1);
}
StringBuilder sb = new StringBuilder();
for (int i = path.size() - 1; i >= 0; i--) {
sb.append(path.get(i)).append(" ");
}
System.out.println(sb.toString().trim());
System.out.println(dist[destination]);
}
}