-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caravans.java
184 lines (137 loc) · 4.7 KB
/
Caravans.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
import java.math.RoundingMode;
public class Caravans
{
public static final boolean DEBUG = true;
static List<Integer>[] graph;
static int n, m;
public static void main(String[] argv) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = getLine(br);
int pos = input.indexOf(" ");
n = Integer.parseInt(input.substring(0,pos));
m = Integer.parseInt(input.substring(pos+1));
graph = new List[n+1];
while (m-- > 0) {
input = getLine(br);
int p = input.indexOf(" ");
int from = Integer.parseInt(input.substring(0,p));
int to = Integer.parseInt(input.substring(p+1));
addEdge(from, to);
}
input = getLine(br);
int p1 = input.indexOf(" ");
int p2 = input.indexOf(" ", p1+1);
int s = Integer.parseInt(input.substring(0,p1));
int f = Integer.parseInt(input.substring(p1+1,p2));
int r = Integer.parseInt(input.substring(p2+1));
Record[] patches = dijkstra(s,f);
Record[] fromDan = dijkstra(r,-1);
System.out.println(enumerateAllPathes(fromDan, patches, f));
}
static int enumerateAllPathes(Record[] fromDan, Record[] patches, int to) {
Stack<Integer> stack = new Stack<>();
stack.push(to);
int globalMax = Integer.MIN_VALUE;
int localMin = Integer.MAX_VALUE;
while (stack.size() > 0) {
int node = stack.pop();
Record r = patches[node];
if (r.from.size() == 0) {
globalMax = Math.max(globalMax, localMin);
//System.out.println(node+" / "+localMin);
localMin = Integer.MAX_VALUE;
}
else {
int distanceFromDan = fromDan[node].distance;
localMin = Math.min(localMin, distanceFromDan);
//System.out.print(node+"("+distanceFromDan+") ");
for(Integer n : r.from) {
stack.push(n);
}
}
}
return globalMax;
}
static Record[] dijkstra(int source, int dest) {
Queue<Integer> queue = new LinkedList<>();
queue.add(source);
queue.add(null);
Record[] table = new Record[n+1];
Record src = new Record(source);
src.distance = 0;
src.marked=true;
table[source] = src;
boolean found = false;
while (queue.size()>0) {
Integer node = queue.poll();
if (node == null) {
if (found)
break;
else
continue;
}
for (Integer neighbour: graph[node]) {
if (table[neighbour] == null) {
table[neighbour] = new Record(neighbour);
}
Record neighbourRecord = table[neighbour];
int currentDistance = neighbourRecord.distance;
int newDistance = table[node].distance + 1;
if (currentDistance > newDistance) {
neighbourRecord.distance = newDistance;
neighbourRecord.from.clear();
neighbourRecord.from.add(node);
}
else if (currentDistance == newDistance) {
neighbourRecord.from.add(node);
}
if (neighbour == dest)
found = true;
if (!table[neighbour].marked) {
queue.add(neighbour);
table[neighbour].marked = true;
}
}
queue.add(null);
}
return table;
}
static class Record
{
int node;
Set<Integer> from;
int distance;
boolean marked;
public Record(int n) {
distance = Integer.MAX_VALUE;
node = n;
from = new HashSet<>();
marked = false;
}
}
static void addEdge(int a, int b) {
if (graph[a]==null)
graph[a] = new ArrayList<>();
if (graph[b]==null)
graph[b] = new ArrayList<>();
graph[a].add(b);
graph[b].add(a);
}
public static void warn(String s) {
if (DEBUG) {
System.out.println(s);
}
}
public static String getLine(BufferedReader br) {
String s = null;
try {
s = br.readLine();
}
catch (Exception e) {
}
return s;
}
}