fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner in = new Scanner(System.in);
  13. int n;
  14. n = in.nextInt();
  15. int [][] a = new int[n][n];
  16. for( int i=0 ; i<n ; i++ )
  17. {
  18. for( int j=0 ; j<n ; j++ )
  19. {
  20. a[i][j] = in.nextInt();
  21. }
  22. }
  23.  
  24. for( int k=0 ; k<n ; k++ )
  25. {
  26. for( int i=0 ; i<n ; i++ )
  27. {
  28. for( int j=0 ; j<n ; j++ )
  29. {
  30. if( i!=j )
  31. {
  32. a[i][j] = Math.min(a[i][j], a[i][k]+a[k][j]);
  33. }
  34. }
  35. }
  36. }
  37. for( int i=0 ; i<n ; i++ )
  38. {
  39. for( int j=0 ; j<n ; j++ )
  40. {
  41. System.out.print(a[i][j]);
  42. if(j+1 == n)
  43. {
  44. System.out.print("\n");
  45. }
  46. else {
  47. System.out.print(' ');
  48. }
  49. }
  50. }
  51. }
  52. }
Success #stdin #stdout 0.07s 2184192KB
stdin
2
3 2
1 1
stdout
3 2
1 1