title: Floyd-warshall for longest distance for undirected graph
body: <p>I want to find the largest distance between any two vertices of a weighted undirected graph using Floyd-warshall algorithm. For this i have made few changes:</p>
  
  <ol>
  <li><p>I add negative weights instead of positive.</p></li>
  <li><p>Then i find out the shortest path.</p></li>
  </ol>
  
  <p>But it does not give me the correct output. Can someone point out the mistake i am making.</p>
  
  <pre><code>class TestClass {
      public static void main(String args[] ) throws Exception {
          Scanner sc = new Scanner(System.in);
          int testcases=sc.nextInt();
          for(int t=0;t&lt;testcases;t++)
          {
              int nodes=sc.nextInt();
              int edges=sc.nextInt();
              int[][] dist_mat=new int[nodes][nodes];
              for(int i=0;i&lt;nodes;i++)
              {
                  for(int j=0;j&lt;nodes;j++)
                  {
                      if(i!=j)
                      {
                          dist_mat[i][j]=Integer.MAX_VALUE;
                      }
                  }
              }
              for(int i=0;i&lt;edges;i++)
              {
                  int source=sc.nextInt();
                  int dest=sc.nextInt();
                  dist_mat[source-1][dest-1]=-sc.nextInt();
                  dist_mat[dest-1][source-1]=dist_mat[source-1][dest-1];
              }
  
              for(int k=0;k&lt;nodes;k++)
              {
                  for(int i=0;i&lt;nodes;i++)
                  {
                      for(int j=0;j&lt;nodes;j++)
                      {
  
                          if(i!=j &amp;&amp; j!=k &amp;&amp; i!=k &amp;&amp; dist_mat[i][j]&gt;dist_mat[i][k]+dist_mat[k][j])
                          {
                              if(dist_mat[i][k]&lt;Integer.MAX_VALUE &amp;&amp; dist_mat[k][j]&lt;Integer.MAX_VALUE)
                                      dist_mat[i][j]=Integer.min(dist_mat[i][j],dist_mat[i][k]+dist_mat[k][j]);
                              if(dist_mat[j][k]&lt;Integer.MAX_VALUE &amp;&amp; dist_mat[k][i]&lt;Integer.MAX_VALUE)
                                      dist_mat[j][i]=Integer.min(dist_mat[j][i],dist_mat[j][k]+dist_mat[k][i]);
                          }
  
                      }
                  }
              }   
          }
      }
  </code></pre>
  
  <p>The same input is :-</p>
  
  <p>1[number of test cases]</p>
  
  <p>5 4 [number of nodes,number of edges]</p>
  
  <p>1 2 4 [first node, second node, weight]</p>
  
  <p>3 2 3 [first node, second node, weight]</p>
  
  <p>2 5 2 [first node, second node, weight]</p>
  
  <p>4 1 1 [first node, second node, weight]</p>