Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Константин Чеботарев #16

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,46 @@ class Navigator {
* @param {*} cities
*/
constructor(cities) {
if (!Array.isArray(cities)) {
throw new Error("Города не массив");
}
this.cities = cities;
}

/**
* Ищет кратчайший путь от точки А до точки B
* @param {string} pointA
* @param {string} pointB
* @param {number} consumtion
* @param {number} consumption
*/
buildPath(pointA, pointB, consumtion) {
buildPath(pointA, pointB, consumption) {

const prices = this.cities.map(city => ({ ...city, price: undefined }));
const roads = prices.map(city => ({ ...city, road: undefined }));
const start = roads.find(city => city.name === pointA);
const end = roads.find(city => city.name === pointB);
start.road = 0;
start.price = 0;


const dijkstra = (cityA, roads) => {
///cityB === cityC.name
for (const cityB in cityA.paths) {
const cityC = roads.find(city => city.name === cityB)
if (cityB in cityA.paths && (!cityC.road || cityC.road > (cityA.paths[cityB] + cityA.road))) {
cityC.road = cityA.paths[cityB] + cityA.road;
cityC.price = cityA.price + consumption * cityA.petrolPrice * cityA.paths[cityB];

dijkstra(cityC, roads);
}
}
}
dijkstra(start, roads);
if (!end.road){
throw new Error('Дороги из пункта А в пункт Б не существет!');
}
return { distance: end.road,
sum: end.price };
}
}

Expand Down
Loading