F.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 101;
  4. int a[MAXN][MAXN];
  5. int n, m;
  6. void Floyd(){
  7. for(int k = 1; k <= n; k++){
  8. for(int i = 1; i <= n; i++){
  9. for(int j = 1; j <= n; j++){
  10. if(a[i][j] > a[i][k] + a[k][j]){
  11. a[i][j] = a[i][k] + a[k][j];
  12. }
  13. }
  14. }
  15. }
  16. }
  17. int main(){
  18. while(cin >> n >> m){
  19. for(int i = 1; i <= n; i++){
  20. for(int j = 1; j <= n; j++){
  21. if(i != j)
  22. a[i][j] = 9999999;
  23. else a[i][j] = 0;
  24. }
  25. }
  26. for(int i = 1; i <= m; i++){
  27. int x, y, z;
  28. cin >> x >> y >> z;
  29. if(a[x][y] > z)
  30. a[x][y] = a[y][x] = z;
  31. }
  32. Floyd();
  33. for(int i = 1; i <= n; i++){
  34. for(int j = 1; j <= n; j++){
  35. if(j!=1){
  36. cout << " ";
  37. }
  38. cout << a[i][j];
  39. }
  40. cout << endl;
  41. }
  42. }
  43. }