title: How to find the shortest path cost?
body: <p>I have Two Arrays </p>
  
  <pre><code>string[] city = {"A","B","C","D"}
  </code></pre>
  
  <p>And the cost of connecting them say </p>
  
  <pre><code>int[] cost ={ 2,1,3,2,4,3}
  </code></pre>
  
  <p>The task is to find the shortest path cost which will be 6 here.</p>
  
  <p>Why?</p>
  
  <pre><code>A-&gt;B = 2
  A-&gt;C = 1
  A-&gt;D = 3
  --------- = 6 (cheapest)
  
  B-&gt;C = 2
  B-&gt;D = 4
  B-&gt;A = 2
  -------- = 8
  
  C-&gt;D = 3
  C-&gt;A = 1
  C-&gt;B = 2
  ------------ = 6 (cheapest)
  
  D-&gt;A =3
  D-&gt;B = 4
  D-&gt;C = 3
  ------------- = 10
  </code></pre>
  
  <p>and so on..total 16(2^4) such combination will appear.</p>
  
  <p>I am refereing some questions in SO + others but not able to understand.</p>
  
  <p>How to do it without taking help of any third partry library?And please provide an easy way of doing so!!</p>
  
  <p>My attempt(not very correct)</p>
  
  <pre><code>public static int minimum_cost(string[] input1, int[] input2)
  {
      int i = 0;
      int j = 0;
      int k = 0;
      int counter = 0;
      int len = input2.Length;
      var storage = new int[input1.Length * input2.Length];
  
      for (i = 0; i &lt; len - 2; i++)
      {
          for (j = i + 1; j &lt; len - 1; j++)
          {
              for (k = j + 1; k &lt; len; k++)
              {
                  var m1 = input2[i];
                  var m2 = input2[j];
                  var m3 = input2[k];
  
                  storage.SetValue(m1 + m2 + m3, counter);                     
                  counter++;
              }                   
          }              
      }
      return storage.Take(counter).Min();
  }
  </code></pre>
  
  <p><strong>Invocation</strong></p>
  
  <pre><code>var input1 = new string[] { "A", "B", "C", "D" };
  var input2 = new int[] { 2, 3, 1, 2, 4, 3 };
  var res = minimum_cost(input1, input2); 
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/ge7DF.png" alt="enter image description here"></p>
  
  <p>Thanks in advance.</p>