forked from trainline-eu/csa-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsa.php
executable file
·62 lines (54 loc) · 1.85 KB
/
csa.php
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
#!/usr/bin/env php
<?php
define('MAX_STATIONS', 100000);
define('DEPARTURE_STATION', 0);
define('ARRIVAL_STATION', 1);
define('DEPARTURE_TIME', 2);
define('ARRIVAL_TIME', 3);
function readTimetable($handle) {
$result = array();
while (($line = fgets($handle, 128)) !== false) {
if ($line == "\n") {
return $result;
}
$result[] = array_map('intval', explode(' ', $line));
}
return $result;
}
function compute(&$timetable, $departureStation, $arrivalStation, $departureTime) {
$inConnection = array_fill(0, MAX_STATIONS, PHP_INT_MAX);
$earliestArrival = array_fill(0, MAX_STATIONS, PHP_INT_MAX);
$earliestArrival[$departureStation] = $departureTime;
foreach ($timetable as $i => $connection) {
if (
$connection[DEPARTURE_TIME] >= $earliestArrival[$connection[DEPARTURE_STATION]] &&
$connection[ARRIVAL_TIME] < $earliestArrival[$connection[ARRIVAL_STATION]]
) {
$inConnection[$connection[ARRIVAL_STATION]] = $i;
$earliestArrival[$connection[ARRIVAL_STATION]] = $connection[ARRIVAL_TIME];
}
}
if ($inConnection[$arrivalStation] === PHP_INT_MAX) {
echo "NO_SOLUTION\n";
return;
}
$route = [];
$lastConnectionIndex = $inConnection[$arrivalStation];
while ($lastConnectionIndex !== PHP_INT_MAX) {
$connection = $timetable[$lastConnectionIndex];
$route[] = $connection;
$lastConnectionIndex = $inConnection[$connection[DEPARTURE_STATION]];
} ;
foreach (array_reverse($route) as $row) {
printf("%d %d %d %d\n", $row[DEPARTURE_STATION], $row[ARRIVAL_STATION], $row[DEPARTURE_TIME], $row[ARRIVAL_TIME]);
}
echo "\n";
}
$fh = fopen('php://stdin', 'r');
$timetable = readTimetable($fh);
while (($line = fgets($fh, 128)) !== false) {
if ($line !== "\n") {
list($departureStation, $arrivalStation, $departureTime) = array_map('intval', explode(' ', $line));
compute($timetable, $departureStation, $arrivalStation, $departureTime);
}
}