-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.php
165 lines (150 loc) · 4.53 KB
/
solve.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
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
<?php
header('Content-type: text/plain');
/**
* Class City
*/
class City
{
public $name;
public $longitude;
public $latitude;
public function __construct($name, $latitude, $longitude)
{
$this->name = $name;
$this->latitude = $latitude;
$this->longitude = $longitude;
}
}
/**
* Class ShortestDistance
* Get shortest distance
*/
class ShortestDistance
{
protected $n = 0;
protected $locations = array();
protected $costMatrix = array();
protected $cities = [];
private $shortestRoutes = [];
private $shortestDistance = 0;
/**
* Solve the problem
*/
public function solve()
{
$this->cities = array_reverse($this->cities);
$currentCity = null;
$firstCity = array_pop($this->cities);
while (!empty($this->cities)) {
$currentCity = $currentCity ?: $firstCity;
$shortestDistance = INF;
$shortestRoute = [];
foreach ($this->cities as $key => $city) {
$distant = round($this->distance($currentCity, $city), 2);
if ($shortestDistance > $distant) {
$shortestDistance = $distant;
$shortestRoute = [$currentCity, $city, 'distant' => $distant];
$nearestCity = $city;
$index = $key;
}
}
if (isset($index) && isset($nearestCity)) {
unset($this->cities[$index]);
$currentCity = $nearestCity;
$this->shortestRoutes[] = $shortestRoute;
$this->shortestDistance += $shortestDistance;
} else {
break;
}
}
// back to first city
if ($currentCity) {
$distant = round($this->distance($currentCity, $firstCity), 2);
$this->shortestRoutes[] = [$currentCity, $firstCity, 'distant' => $distant];
$this->shortestDistance += $distant;
}
}
/**
* Add City
* @param $name
* @param $latitude
* @param $longitude
*/
public function addCity($name, $latitude, $longitude)
{
$this->cities[] = new City($name, $latitude, $longitude);
}
/**
* Return Routes
* @return string
*/
public function getShortestRoutes()
{
$routes = [];
foreach ($this->shortestRoutes as $route) {
$routes[] = $route[0]->name;
}
return implode("\n", $routes);
}
/**
* Return Distance
* @return int
*/
public function getShortestDistance()
{
return $this->shortestDistance;
}
/**
* Code copied from https://www.geodatasource.com/developers/php
*
* @param City $currentCity
* @param City $city
* @param string $unit
* @return float|int
*/
private function distance($currentCity, $city, $unit = 'M')
{
$lat1 = $currentCity->latitude;
$lon1 = $currentCity->longitude;
$lat2 = $city->latitude;
$lon2 = $city->longitude;
if ($lat1 == $lat2 && $lon1 == $lon2) {
return 0;
}
$theta = $lon1 - $lon2;
$dist =
sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} elseif ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
}
try {
$shortestDistance = new ShortestDistance();
$cities = fopen("cities.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while (!feof($cities)) {
$city = fgets($cities);
$location = explode(" ", $city);
$longitude = array_pop($location);
$latitude = array_pop($location);
$longitude = trim(str_replace(array("\n", "\r"), ' ', $longitude));
$shortestDistance->addCity(implode(" ", $location), (double)$latitude, (double)$longitude);
}
fclose($cities);
$shortestDistance->solve();
echo $shortestDistance->getShortestRoutes();
// echo "\nTotal distance: " . $shortestDistance->getShortestDistance() . "\n\n";
} catch (Exception $e) {
echo $e;
exit;
}