9

I have a directed, positive weighted graph. Each edge have a cost of use. I have only A money, i want to calculate shortest paths with dijkstra algorithm, but sum of edges costs on route must be less or equal to A.

I want to do this with most smallest Dijstra modification (if I can do it with small modification of Dijkstra). I must do this in O(n*log(n)) if I can, but i think i can.

Anyone can help me with this?

Svisstack
  • 16,203
  • 6
  • 66
  • 100

2 Answers2

5

https://www.spoj.pl/problems/ROADS/

The problem was given at CEOI '98 and its official solution can be found here.

itsadok
  • 28,822
  • 30
  • 126
  • 171
IVlad
  • 43,099
  • 13
  • 111
  • 179
  • 4
    Perhaps i'm missing something here, but none of those links actually feature an algorithm that solves this problem. The SPOJ submissions contain algorithms, but we can't read the source code to them! – Tom Anderson Jan 06 '13 at 17:55
0

You don't really need to modify Dijkstra's algorithm to do this as the answer is equivalent to finding the shortest path and then accepting it if it's less than or equal to A.

Of course, you could always short circuit the inner loop if you visit a path with a cost more than A.

EDIT: With the clarification that you want to minimize cost AND distance, you can't do that without clarifying the solution you want. Do you want the cheapest path? Do you want the shortest path? Do you want some function of cost and distance? All of these determine what weighting function you should use for a particular edge.

MSN
  • 53,214
  • 7
  • 75
  • 105
  • The cost and length of an edge are two different things, check out the comments. – michalburger1 Apr 26 '10 at 01:29
  • 1
    @Bus He did not say anything about a length in his question. As far as I understand the only value associated with each edge is the cost. – Enrique Apr 26 '10 at 01:36
  • Which of the many meanings Svisstack intended is still unclear to me as well, even after zhe answered Mark's question. – outis Apr 26 '10 at 01:53
  • 1
    I think the discussion makes it clear. Mark asked: "Each edge has a length and a cost, you want to minimize the length with the extra constraint that the cost must be less than or equal to A." Svisstack answered yes. – michalburger1 Apr 26 '10 at 09:17
  • Then basically, instead of going by edge weights, use djikstra on the edge cost, and you're done oO – Rubys Apr 26 '10 at 14:53
  • @Rubys that results in the cheapest path, but not necessarily in the shortest path under the price constraint. – Christian Semrau Apr 26 '10 at 14:59
  • 1
    @Bus: it's just that it wouldn't be the first time someone didn't understand a clarifying question, or the alternate meanings and ambiguities in the question. – outis Apr 26 '10 at 15:26