-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathHIGHWAYS.cs
383 lines (325 loc) · 13.5 KB
/
HIGHWAYS.cs
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Vertex = WeightedSimpleGraph.Vertex;
// https://www.spoj.com/problems/HIGHWAYS/ #dijkstras #graph-theory #greedy #heap #shortest-path
// Finds the cheapest path between pairs of cities.
public static class HIGHWAYS
{
// This uses Dijkstra's algorithm: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm.
// We return immediately upon visiting the destination city, and we don't initialize the
// heap with all cities. We only add cities to the heap when reaching one of their neighbor
// cities. Without a pre-filled heap to rely on, we track what cities have been visited
// using an array of bools.
public static int? Solve(WeightedSimpleGraph cityGraph, Vertex startCity, Vertex endCity)
{
var pathCosts = new BinaryHeap(startCity);
bool[] visitedCities = new bool[cityGraph.VertexCount];
while (!pathCosts.IsEmpty)
{
var cheapestPath = pathCosts.Extract();
var city = cheapestPath.Key;
int pathCostToCity = cheapestPath.Value;
if (city == endCity)
return pathCostToCity;
foreach (var neighbor in city.Neighbors.Where(n => !visitedCities[n.ID]))
{
int pathCostToNeighborThroughCity = pathCostToCity + city.GetEdgeWeight(neighbor);
int currentPathCostToNeighbor;
// We know the neighboring city hasn't been visited yet, so we need to maintain its
// path cost in the heap. If it's already in the heap, see if a cheaper path exists
// to it through the city we're visiting. If it isn't in the heap yet, add it.
if (pathCosts.TryGetValue(neighbor, out currentPathCostToNeighbor))
{
if (pathCostToNeighborThroughCity < currentPathCostToNeighbor)
{
pathCosts.Update(neighbor, pathCostToNeighborThroughCity);
}
}
else
{
pathCosts.Add(neighbor, pathCostToNeighborThroughCity);
}
}
visitedCities[city.ID] = true;
}
return null;
}
}
// Undirected, weighted graph with no loops or multiple edges. The graph's vertices are stored
// in an array, with the ID of a vertex (from 0 to vertexCount - 1) corresponding to its index.
public sealed class WeightedSimpleGraph
{
public WeightedSimpleGraph(int vertexCount)
{
var vertices = new Vertex[vertexCount];
for (int id = 0; id < vertexCount; ++id)
{
vertices[id] = new Vertex(this, id);
}
Vertices = vertices;
}
public IReadOnlyList<Vertex> Vertices { get; }
public int VertexCount => Vertices.Count;
public void AddEdge(int firstVertexID, int secondVertexID, int weight)
=> AddEdge(Vertices[firstVertexID], Vertices[secondVertexID], weight);
public void AddEdge(Vertex firstVertex, Vertex secondVertex, int weight)
{
firstVertex.AddNeighbor(secondVertex, weight);
secondVertex.AddNeighbor(firstVertex, weight);
}
public sealed class Vertex : IEquatable<Vertex>
{
private readonly WeightedSimpleGraph _graph;
private readonly Dictionary<Vertex, int> _edges = new Dictionary<Vertex, int>();
internal Vertex(WeightedSimpleGraph graph, int ID)
{
_graph = graph;
this.ID = ID;
}
public int ID { get; }
public IReadOnlyCollection<Vertex> Neighbors => _edges.Keys;
public int Degree => _edges.Count;
// In this city graph, there can be multiple highways between cities--use the cheapest.
internal void AddNeighbor(Vertex neighbor, int weight)
{
int currentWeight;
if (!_edges.TryGetValue(neighbor, out currentWeight))
{
_edges.Add(neighbor, weight);
}
else if (weight < currentWeight)
{
_edges[neighbor] = weight;
}
}
public bool HasNeighbor(Vertex neighbor)
=> _edges.ContainsKey(neighbor);
public int GetEdgeWeight(Vertex neighbor)
=> _edges[neighbor];
public bool TryGetEdgeWeight(Vertex neighbor, out int edgeWeight)
=> _edges.TryGetValue(neighbor, out edgeWeight);
public override bool Equals(object obj)
=> (obj as Vertex)?.ID == ID;
public bool Equals(Vertex other)
=> other.ID == ID;
public override int GetHashCode()
=> ID;
}
}
public sealed class BinaryHeap
{
private readonly List<KeyValuePair<Vertex, int>> _keyValuePairs = new List<KeyValuePair<Vertex, int>>();
private readonly Dictionary<Vertex, int> _keyIndices = new Dictionary<Vertex, int>();
public BinaryHeap(Vertex topKey, int topValue = 0)
{
_keyValuePairs.Add(new KeyValuePair<Vertex, int>(topKey, topValue));
_keyIndices.Add(topKey, 0);
}
public int Size => _keyValuePairs.Count;
public bool IsEmpty => Size == 0;
public KeyValuePair<Vertex, int> Top => _keyValuePairs[0];
public void Add(Vertex key, int value)
=> Add(new KeyValuePair<Vertex, int>(key, value));
public void Add(KeyValuePair<Vertex, int> keyValuePair)
{
_keyValuePairs.Add(keyValuePair);
_keyIndices.Add(keyValuePair.Key, _keyValuePairs.Count - 1);
SiftUp(_keyValuePairs.Count - 1, keyValuePair);
}
public KeyValuePair<Vertex, int> Extract()
{
var top = _keyValuePairs[0];
_keyIndices.Remove(top.Key);
if (_keyValuePairs.Count == 1)
{
_keyValuePairs.RemoveAt(0);
}
else
{
var bottom = _keyValuePairs[_keyValuePairs.Count - 1];
_keyValuePairs.RemoveAt(_keyValuePairs.Count - 1);
_keyValuePairs[0] = bottom;
_keyIndices[bottom.Key] = 0;
SiftDown(0, bottom);
}
return top;
}
public bool Contains(Vertex key)
=> _keyIndices.ContainsKey(key);
public int GetValue(Vertex key)
=> _keyValuePairs[_keyIndices[key]].Value;
public bool TryGetValue(Vertex key, out int value)
{
int keyIndex;
if (_keyIndices.TryGetValue(key, out keyIndex))
{
value = _keyValuePairs[keyIndex].Value;
return true;
}
value = default(int);
return false;
}
public int Update(Vertex key, int value)
=> Update(new KeyValuePair<Vertex, int>(key, value));
public int Update(KeyValuePair<Vertex, int> keyValuePair)
{
int index = _keyIndices[keyValuePair.Key];
int oldValue = _keyValuePairs[index].Value;
_keyValuePairs[index] = keyValuePair;
// If the old value was larger than the updated value, try sifting the updated value up.
if (oldValue > keyValuePair.Value)
{
SiftUp(index, keyValuePair);
}
else
{
SiftDown(index, keyValuePair);
}
return oldValue;
}
private void SiftUp(int index, KeyValuePair<Vertex, int> keyValuePair)
{
// Stop if we don't have a parent to sift up to.
if (index == 0) return;
int parentIndex = (index - 1) / 2;
var parentKeyValuePair = _keyValuePairs[parentIndex];
// If the parent is larger, push the parent down and the value up--small rises to the
// top. We know this is okay (aka heap-preserving) because parent was smaller than the
// other child, as only one child gets out of order at a time. So both are larger than value.
if (parentKeyValuePair.Value > keyValuePair.Value)
{
_keyValuePairs[index] = parentKeyValuePair;
_keyIndices[parentKeyValuePair.Key] = index;
_keyValuePairs[parentIndex] = keyValuePair;
_keyIndices[keyValuePair.Key] = parentIndex;
SiftUp(parentIndex, keyValuePair);
}
}
private void SiftDown(int index, KeyValuePair<Vertex, int> keyValuePair)
{
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
// If both children exist...
if (rightChildIndex < _keyValuePairs.Count)
{
var leftChildKeyValuePair = _keyValuePairs[leftChildIndex];
var rightChildKeyValuePair = _keyValuePairs[rightChildIndex];
// If the left child is smaller than the right child (so left can move above right)...
if (leftChildKeyValuePair.Value < rightChildKeyValuePair.Value)
{
// And the value is greater than its left child, push the left child up and
// the value down--big falls to the bottom.
if (keyValuePair.Value > leftChildKeyValuePair.Value)
{
_keyValuePairs[index] = leftChildKeyValuePair;
_keyIndices[leftChildKeyValuePair.Key] = index;
_keyValuePairs[leftChildIndex] = keyValuePair;
_keyIndices[keyValuePair.Key] = leftChildIndex;
SiftDown(leftChildIndex, keyValuePair);
}
}
// If the right child is smaller or the same as the left child (so right can move above left)...
else
{
// And the value is greater than its right child, push the right child up and
// the value down--big falls to the bottom.
if (keyValuePair.Value > rightChildKeyValuePair.Value)
{
_keyValuePairs[index] = rightChildKeyValuePair;
_keyIndices[rightChildKeyValuePair.Key] = index;
_keyValuePairs[rightChildIndex] = keyValuePair;
_keyIndices[keyValuePair.Key] = rightChildIndex;
SiftDown(rightChildIndex, keyValuePair);
}
}
}
// If only the left child exists (and therefore the left child is the last value)...
else if (leftChildIndex < _keyValuePairs.Count)
{
var leftChildKeyValuePair = _keyValuePairs[leftChildIndex];
// And the value is greater than its left child, push the left child up and
// the value down--big falls to the bottom.
if (keyValuePair.Value > leftChildKeyValuePair.Value)
{
_keyValuePairs[index] = leftChildKeyValuePair;
_keyIndices[leftChildKeyValuePair.Key] = index;
_keyValuePairs[leftChildIndex] = keyValuePair;
_keyIndices[keyValuePair.Key] = leftChildIndex;
}
}
}
}
public static class Program
{
private static void Main()
{
int remainingTestCases = FastIO.ReadNonNegativeInt();
while (remainingTestCases-- > 0)
{
int cityCount = FastIO.ReadNonNegativeInt();
int highwayCount = FastIO.ReadNonNegativeInt();
int startCityIndex = FastIO.ReadNonNegativeInt() - 1;
int endCityIndex = FastIO.ReadNonNegativeInt() - 1;
var cityGraph = new WeightedSimpleGraph(cityCount);
for (int h = 0; h < highwayCount; ++h)
{
cityGraph.AddEdge(
firstVertexID: FastIO.ReadNonNegativeInt() - 1,
secondVertexID: FastIO.ReadNonNegativeInt() - 1,
weight: FastIO.ReadNonNegativeInt());
}
int? totalMinutes = HIGHWAYS.Solve(cityGraph,
startCity: cityGraph.Vertices[startCityIndex],
endCity: cityGraph.Vertices[endCityIndex]);
Console.WriteLine(totalMinutes?.ToString() ?? "NONE");
}
}
}
// This is based in part on submissions from https://www.codechef.com/status/INTEST.
// It's assumed the input is well-formed, so if you try to read an integer when no
// integers remain in the input, there's undefined behavior (infinite loop).
// NOTE: FastIO might not be necessary, but seems like large input.
public static class FastIO
{
private const byte _null = (byte)'\0';
private const byte _newLine = (byte)'\n';
private const byte _minusSign = (byte)'-';
private const byte _zero = (byte)'0';
private const int _inputBufferLimit = 8192;
private static readonly Stream _inputStream = Console.OpenStandardInput();
private static readonly byte[] _inputBuffer = new byte[_inputBufferLimit];
private static int _inputBufferSize = 0;
private static int _inputBufferIndex = 0;
private static byte ReadByte()
{
if (_inputBufferIndex == _inputBufferSize)
{
_inputBufferIndex = 0;
_inputBufferSize = _inputStream.Read(_inputBuffer, 0, _inputBufferLimit);
if (_inputBufferSize == 0)
return _null; // All input has been read.
}
return _inputBuffer[_inputBufferIndex++];
}
public static int ReadNonNegativeInt()
{
byte digit;
// Consume and discard whitespace characters (their ASCII codes are all < _minusSign).
do
{
digit = ReadByte();
}
while (digit < _minusSign);
// Build up the integer from its digits, until we run into whitespace or the null byte.
int result = digit - _zero;
while (true)
{
digit = ReadByte();
if (digit < _zero) break;
result = result * 10 + (digit - _zero);
}
return result;
}
}