private static void Dijkstra(IntersectionHeap priorityQueue) { Intersection current; while (priorityQueue.Size != 0) { current = priorityQueue.DeleteMax(); foreach (Corridor corridor in current.corridors) { if (corridor.Factor * current.bestFactor > corridor.NextIntersection.bestFactor) { corridor.NextIntersection.bestFactor = corridor.Factor * current.bestFactor; priorityQueue.InsertOrChange(corridor.NextIntersection); } } } }
static void Main(string[] args) { Dictionary <int, Intersection> intersections; IntersectionHeap priorityQueue; string output = ""; IntersectionComp comparer = new IntersectionComp(); Scanner input = new Scanner(); int n = input.NextInt(); int m = input.NextInt(); while (n * m != 0) { intersections = new Dictionary <int, Intersection>(); priorityQueue = new IntersectionHeap(comparer, m); for (int i = 0; i < n; i++) { Intersection intersection = new Intersection(i); intersections.Add(i, intersection); } for (int i = 0; i < m; i++) { int interKey1 = input.NextInt(); int interKey2 = input.NextInt(); float factor = input.NextFloat(); Intersection inter1, inter2; intersections.TryGetValue(interKey1, out inter1); intersections.TryGetValue(interKey2, out inter2); inter1.AddCorridor(inter2, factor); inter2.AddCorridor(inter1, factor); } Intersection start; intersections.TryGetValue(0, out start); start.bestFactor = 1f; priorityQueue.InsertOrChange(start); Dijkstra(priorityQueue); Intersection end; intersections.TryGetValue(n - 1, out end); output = output + end.bestFactor.ToString("0.0000") + "\n"; n = input.NextInt(); m = input.NextInt(); } Console.Write(output); Console.Read(); }