The shortest path in the graph, where the cost depends on the history of the passage

My goal is to find the shortest path (least cost) between given cities (peaks) connected with roads (edges). Each road and each city has a fee (cost), which must be paid before entering this path / city.

If it were a whole task, I would use Dijkstra's algorithm to find the shortest path (and add the cost of the city to the cost of the road connected directly in front of it).

BUT:

Cities have something like partnership agreements - when you visit and pay a fee in one of them, entry to other cities in this partnership is free .

So, the cost of the vertex (the edges associated with it) depends on the vertices already traveled.

Is there any algorithm for this kind of problem?

Thank.

+4
source share
3 answers

You can reduce a given cover issue. This means that your NP problem is difficult and you should not expect to find an effective solution (in general).

, , , , , (, / , ). 2 ^ P * (N + M) , P - , N M - .

:

, S = {s [1],..., s [n]} S: S [1], S [1], S [2],..., S [N]. , S.

, , . START, END (S [i], t) t S [i]. :

  • START (S [i], s [1]) S [i] s [1] S [i]
  • (S [i], s [n]) END S [i] s [n] S [i].
  • (S [i], s [k]) (S [i '], s [k + 1]) k 1... n-1 S [i] S [ '], .

- 1, (S [i], s) 1. / (S [i], s), (S [i], t) . /, .

START END S [i], S. 1 + n + p, p - .

+3

Dijkstra , .

, , , - , .

class TruckState {
    City current;
    List<City> visited;
}

: , ( , ?). , Set, :

class TruckState {
    City current;
    Set<City> visited;
}

. , . , Set Agreements. , .

class TruckState {
    City current;
    Set<Agreement> unlocked;
}

[EDIT]: , : . , . :

class TruckState {
    City current;
    Map<Agreement, City> visited;
}

: A- , . , , - . , , , Heurisitic 0. ...

+1

, .

Dijkstra - , , , , node.
node , ( , ).

0 , , .

0
source

Source: https://habr.com/ru/post/1662770/


All Articles