title: Dijkstra's algorithm returning incorrect values
body: <p>I've hit a wall in attempting to understand Dijkstra's algorithm. The algorithm, in short, finds the shortest distances between A and B given distances between the two.</p>
  
  <p>I will post my version of the algorithm (I haven't had much success looking online thus far), followed by the distances between nodes.</p>
  
  <pre><code>void GraphM::findShortestPath()
  {
      for (int i = 1; i &lt;= nodeCount; i++)    // From Node i...
      {
          for (int v = 1; v &lt;= nodeCount; v++) // ...through Node v...
          {
              for (int w = 1; w &lt;= nodeCount; w++) // ...to Node w
              {
                  if (!T[i][w].visited || !T[i][v].visited)
                  {
                      T[i][w].dist = min(T[i][w].dist, T[i][v].dist + C[v][w]);
                      T[i][v].visited = true;
                      T[i][w].visited = true;
                  }           
              }
          }
      }
  
      cout &lt;&lt; "1 to 2 is " &lt;&lt; T[1][2].dist &lt;&lt; endl; 
  }
  </code></pre>
  
  <p>This outputs the following:</p>
  
  <p><code>1 to 2 is 48</code></p>
  
  <p>...when it should be</p>
  
  <p><code>1 to 2 is 40</code></p>
  
  <p>The values I'm working with are as follows:</p>
  
  <pre><code>1 2 50
  1 3 20
  1 5 30
  2 4 10
  3 2 20
  3 4 40
  5 2 20
  5 4 25
  </code></pre>
  
  <p>...where, in each line, the first token is the first node, the second token is the second node, and the third token is the distance between those nodes (in the algorithm's case, these tokens would be i, v, and T[i][v].dist). In the algorithm, nodeCount is the number of nodes in the grid (5), and w is a node that we are looking for the distance to, from i. C[v][w] returns the original distance between v and w. So, if v was 5 and w was 2, C[v][w] would return 20. This is constant, whereas T[v][w].dist (for instance) can be changed.</p>
  
  <p>Any nonexistant node relationships such as C[5][3] or T[1][4].dist (at least at the outset) return INT_MAX, which is equivalent to infinity. </p>
  
  <p>Also, for anyone wondering; yes, this is a homework assignment. Unfortunately, my professor has necessitated some specific details (such as using a struct T), and she never went into much detail on how to write Dijkstra's algorithm into code, other than a somewhat vague outline. I am simply asking if someone can tell me what I am doing wrong and how to fix it, if possible. </p>
  
  <p>Any help is very much appreciated and would save me a lot of time from banging my head against a wall. </p>