fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. int main()
  6. {
  7. int n, e;
  8. cin>>n>>e;
  9. long long int graph[n+1][n+1];
  10. for(int i = 1; i <= n; i++)
  11. {
  12. for(int j = 1; j <= n; j++)
  13. {
  14. if(i == j)graph[i][j] = 0;
  15. else graph[i][j] = INT_MAX;
  16. }
  17. }
  18.  
  19. int u, v, w;
  20. for(int i = 1; i <= e; i++)
  21. {
  22. cin>>u>>v>>w;
  23. graph[u][v] = w;
  24. }
  25. cout<<"A0"<<endl;
  26. for(int i = 1; i <= n; i++)
  27. {
  28. for(int j = 1; j <= n; j++)
  29. {
  30. cout<<graph[i][j]<<" ";
  31. }
  32. cout<<endl;
  33. }
  34.  
  35.  
  36.  
  37. for(int k = 1; k <= n; k++)
  38. {
  39. cout<<"A"<<k<<endl;
  40. for(int i = 1; i <= n; i++)
  41. {
  42. for(int j = 1; j <= n; j++)
  43. {
  44. //if(i == 1&&k==1)cout<<graph[i][j]<<" "<<graph[i][k]+graph[k][j]<<endl;
  45. graph[i][j] = min(graph[i][j], graph[i][k]+graph[k][j]);
  46.  
  47. }
  48.  
  49. }
  50. for(int p = 1; p <= n; p++)
  51. {
  52. for(int q = 1; q <= n; q++)
  53. {
  54. cout<<graph[p][q]<<" ";
  55. }
  56. cout<<endl;
  57. }
  58. }
  59.  
  60.  
  61.  
  62. }
  63.  
Success #stdin #stdout 0s 5284KB
stdin
4 7                                                                              1 2 3                                                                            2 1 8                                                                            2 3 2                                                                            1 4 7                                                                            4 1 2                                                                            3 1 5                                                                            3 4 1 
stdout
A0
0 3 2147483647 7 
8 0 2 2147483647 
5 2147483647 0 1 
2 2147483647 2147483647 0 
A1
0 3 2147483647 7 
8 0 2 15 
5 8 0 1 
2 5 2147483647 0 
A2
0 3 5 7 
8 0 2 15 
5 8 0 1 
2 5 7 0 
A3
0 3 5 6 
7 0 2 3 
5 8 0 1 
2 5 7 0 
A4
0 3 5 6 
5 0 2 3 
3 6 0 1 
2 5 7 0