Edsger W. Dijkstra proposed a simple, "greedy" idea: "Always visit the closest possible node that you have not finished yet." Instead of a simple Queue (First-In-First-Out), you need a generic container that always gives us the smallest item inside it.
If you have two options: Go to (Cost ) Go to (Cost ) you should process first, because it is closer. Maybe gives us a shortcut to .