Блог пользователя a_r_d

Автор a_r_d, история, 6 лет назад, По-английски

Given a weighted undirected graph. There are N cities connected by M roads. For each road, you know the fuel needed to travel that road. A small subset of cities have gas station. The price per litre at each gas station is different. The task is to travel from city A to city B in a car with fuel capacity C minimizing the total cost.

Stuck at — Create a new graph including only the cities with gas stations along with source and destination cities. The edge weights in this graph can be found by multiple runs of Dijkstra's algorithm. Now, how to proceed with different fuel price at each node? Some DP?

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

What are the restrictions of N, M and C?

One possible idea is represent each vertex as a pair (v, f), where 1 <= v <= N and 0 <= f <= C. And build a graph that, if v has a gas station then it can go from (v, f) a (v, f + a), where f + a <= C, with cost equal to a * (price of fuel in city v), and can go from (v, f) to (u, f — w) with cost 0, where w is the necessary fuel to travel from v to u. Finally, perform a Dijkstra.

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
  • well the only possible options are to fill the tank completely or fill enough to travel from current to next edge i.e weight of edge u->v . so transform the give graph G into graph G` by doubling the edges .

  • There would be two types of edges.

    • Edge 1: On movement along this edge only that much petrol is filled if the current fuel level is lower than the required fuel . otherwise no fuel is filled and its cost is calculated by multiplying rate at current node by the required fuel for just one edge.
    • Edge 2: On movement along this type of edge , complete fuel tank is filled however much is required .
  • Then Dijkstra's algorithm is performed from target node to source node.