#!/usr/bin/perl -w use strict; use Data::Dumper; @ARGV = 'roads.dat'; my (@lines,@edge,%verts); while(<>){ chomp; push @lines, [split ",", $_]; } map push(@edge, load(@{$_})), @lines; foreach my $edge(@edge){ my($from,$to)=($verts{$edge->{From}},$verts{$edge->{To}}); push @{$from->{Edges}}, $edge; push @{$to->{Edges}}, $edge; print map "$_->{Cost} " , @{$verts{"East Jordan"}->{Edges}}; #print ${$edge}{To}; ${$to}{Cost}=${$from}{Cost}=99999999; } push my @queue, $verts{"East Jordan"}; ${$verts{"Ellsworth"}}{Cost}=0; while(@queue ne 0){ my $town = shift @queue; my $cost = $town->{Cost}; print " | "; print map "$_->{Cost} ", @{$verts{$town}->{Edges}}; foreach(@{$verts{$town}->{Edges}}){ print"[runs]"; my $other = $verts{opposite($_,$town)}; if($cost+$_->{Cost} < $other->{Cost}){ $other->{Cost}=$cost+$_->{Cost}; $other->{Last}=$_; push @queue, $other; } } } print map "$_->{Name} $_->{Cost} $_->{From} $_->{To}\n", @edge; print Dumper(\%verts,\@edge); sub opposite{ if($verts{${$_[0]}{From}} eq $_[1]){ $_[0]->{To}; } elsif ($verts{$_[0]->{To}} eq $_[1]){ $_[0]->{From}; } else { die "opposite failed, ",$!; } } sub load{ {Name => $_[0], Cost => $_[1], From => "$_[2]", To => "$_[3]",}; }