-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaiza5.php
48 lines (39 loc) · 1.03 KB
/
paiza5.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
<?php
list($N, $M, $X, $Y) = array_map('intval', explode(' ', trim(fgets(STDIN))));
$map = [];
for($i = 0; $i < $M; $i++) {
list($a, $b) = array_map('intval', explode(' ', trim(fgets(STDIN))));
$map[$a][] = $b;
$map[$b][] = $a;
}
function bfs($start, $map, $N) {
$distances = array_fill(1, $N + 1, PHP_INT_MAX);
$queue = new SplQueue();
$queue->enqueue($start);
$distances[$start] = 0;
while(!$queue->isEmpty()) {
$current = $queue->dequeue();
if(!isset($map[$current])) {
continue;
}
foreach($map[$current] as $neighbor) {
if($distances[$neighbor] == PHP_INT_MAX) {
$queue->enqueue($neighbor);
$distances[$neighbor] = $distances[$current] + 5;
}
}
}
return $distances;
}
$distanceFromX = bfs($X, $map, $N);
$distanceFromY = bfs($Y, $map, $N);
for($i = 0; $i < $N; $i++) {
if($distanceFromX[$i] < $distanceFromY[$i]) {
echo "X is closer\n";
} elseif ($distanceFromX[$i] > $distanceFromY[$i]){
echo "Y is closer\n";
} else {
echo "Same\n";
}
}
?>