943,706 Members | Top Members by Rank

Ad:
Feb 22nd, 2009
1

A Nice Shortest path problem !

Expand Post »
Hello everyone! i found some days ago a really nice algorithmic problem, and i am trying to solve it (implement it in pascal in the end).
I know it is a shortest path problem, (it is obvious) but i still can't seem to find any proper approach to it!
Any help (thoughts..) will be appreciated!

So here is the problem:
We have a mail man that is trying to deliver mails to some ports between his home and his office.
The home of the mail man, is at the point (0,0) (cartesian coordinates), and the office at the point (A,B).
In between there are the ports with coordinates X,Y.

The mail man though can do only 2 routes. One in the morning, from home to office and one in the evening from office to home. He must deliver all mails in those 2 routes.
Yet, in the morning route, he can visit only the ports which have a greater x-coordinate than his previous location, and in the evening route he can visit ports with smaller x-coordinate than his previous location.

We should make a program that calculates the minimum distance he must travel, to deliver mails to all the ports.

Input File:
First line contains an integer N, 0<N<=200, the numbers of the port.
second line contains two integers A,B , 1<=A,B<=1000 , the coordinates of the office.
The following N lines contain, the coordinates of the ports X,Y , 1<X<=A and -1000<=Y<=1000.
All coordinates are seperated with space.

OUtput: An integer, the minimun distance.

Please request clarifications if needed.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thrasas is offline Offline
4 posts
since Feb 2009
Feb 23rd, 2009
0

Re: A Nice Shortest path problem !

To save LizR the effort let me ask you:

What have got thus far? Have you developed any parts of the algorithm such as how to read the file, how to store the data for use, or how to determine the distances?
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
jsosnowski is offline Offline
68 posts
since Nov 2007
Feb 23rd, 2009
0

Re: A Nice Shortest path problem !

yes i have developed all of those! actually in two versions. one suitable to graph algorithms, and one simpler.

Here is the code for the simpler one :

Pascal and Delphi Syntax (Toggle Plain Text)
  1.  
  2. uses
  3. SysUtils;
  4.  
  5. type
  6. coordinates = record
  7. X:integer;
  8. Y:integer;
  9. end;
  10.  
  11. var
  12. space:char;
  13. N,
  14. i,j,prev1,prev2:integer;
  15. input,
  16. output:text;
  17. port:array[1..202] of coordinates;
  18. d:array [1..7,1..7] of real;
  19. temp:coordinates;
  20.  
  21.  
  22.  
  23. begin
  24.  
  25. {Input data}
  26. assign(input,'C:/evripos.in') ; {file named evripos}
  27. reset(input);
  28. readln(input,N);
  29. readln(input,port[N+2].X,space,port[N+2].Y);
  30. for i := 2 to N+1 do
  31. readln(input,port[i].X,space,port[i].Y);
  32. close(input);
  33. port[1].X:=0;
  34. port[1].Y:=0;
  35.  
  36. (*------------------------------------------------*)
  37.  
  38. (* sort coords from 0 to A*)
  39. for i := 2 to N+2 do
  40. for j := N+2 downto i do
  41. if port[j].X < port[j-1].X then
  42. begin
  43. temp:=port[j];
  44. port[j]:=port[j-1];
  45. port[j-1]:=temp;
  46. end;
  47.  
  48. (*create distance matrix*) {it is also an adjacency matrix if considered as a graph}
  49. for i := 1 to 7 do
  50. for j := 1 to 7 do
  51. d[i,j]:= sqrt(sqr(port[i].X-port[j].X)+ sqr(port[i].Y-port[j].Y));
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thrasas is offline Offline
4 posts
since Feb 2009
Feb 24th, 2009
0

Re: A Nice Shortest path problem !

So. Whats your specific problem? You've told us the homework question, you have some code.. What problem specifically are you having?
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 24th, 2009
0

Re: A Nice Shortest path problem !

my "problem" is that i don't seem to understand what shortest path is needed!

I have tried Dijkstra's algorithm but it doesn't seem to work well for this case, because we have 2 routes....

Yet, now i am working on a greedy algorithm to find all possiple paths and take the smallest... but that would be O(n!) worst case.. and that's not really good...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thrasas is offline Offline
4 posts
since Feb 2009
Feb 24th, 2009
0

Re: A Nice Shortest path problem !

As its your homework, why not ask your professor? If you havent followed the question any advice we give you could be wrong on the grounds it could be the teacher has felt they explained and we might say something in contradiction.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 24th, 2009
0

Re: A Nice Shortest path problem !

Your problem here is clearly the algorithm, not the coding. Do you actually need Dijkstra's algorithm?

If you take all the points and determine the average Y value and then split the list of points in two using the middle 'y' as the division line. Now for each group sort them by x values andyou will have the sequence order. Both groups have to traverse the same X distance and the Y distance is less than if you cross the center 'y' line. Correct???
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
jsosnowski is offline Offline
68 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Declaring a Record Type using Case.
Next Thread in Pascal and Delphi Forum Timeline: Decimal to Binary or hex





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC