我知道像dijkstra,prim,kruskal这样的算法,但我不知道如何解决以下问题。
我有一个包含整数值的城市列表。 我有一个函数,根据通过的距离范围计算给定城市的距离。如果你转移到一个城市和20公里的距离将返回距离城市那个距离的所有城市。
条件:
很抱歉,如果我不能很好地解释我。
修改
我添加了一些伪代码来试图帮助你解决问题:
//Get array of cities ordered ASC by value and each city is marked as parent or not.
$cities = getCitiesByValue();
foreach($cities as $city) {
if($city->isParent) {
//Get near(20km) PARENT cities ordered ASC
$nearCities = findNearBy($city, $distance = 20);
foreach($nearCities as $nearCity) {
if($nearCity->value > $city->value) {
$city->setParent($nCity);
$city->setAsChild;
}
}
}
elseif($city->isChild) {
//Get near(50km) PARENT cities ordered ASC
$nearCities = findNearBy($city, $distance = 50);
foreach($nearCities as $nearCity) {
if($nearCity->value > $city->value) {
$city->setParent($nCity);
}
}
}
}