Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include<cstdio>
  2. #include<limits.h>
  3. using namespace std;
  4.  
  5. int dist[1000][1000],n,m;
  6.  
  7. void init(){
  8. int from,to,weight;
  9.  
  10. scanf("%d %d",&n,&m);
  11.  
  12. for(int i=0;i<n;i++){
  13. for(int j=0;j<n;j++){
  14. dist[i][j]=INT_MAX;
  15. }
  16. dist[i][i]=0;
  17. }
  18.  
  19. for(int i=0;i<m;i++){
  20. scanf("%d %d %d",&from,&to,&weight);
  21. dist[from][to]=weight;
  22. }
  23. }
  24.  
  25. void floyd(){
  26. for(int k=0;k<n;k++){
  27. for(int i=0;i<n;i++){
  28. for(int j=0;j<n;j++){
  29. if(dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k]+dist[k][j]<dist[i][j]){///check for INT_MAX because if we add those the number will get negative and update values
  30. dist[i][j]=dist[i][k]+dist[k][j];
  31. }
  32. }
  33. }
  34. }
  35. }
  36.  
  37. void print(){
  38. for(int i=0;i<n;i++){
  39. for(int j=0;j<n;j++){
  40. printf("%d ",dist[i][j]);
  41. }
  42. printf("\n");
  43. }
  44. }
  45.  
  46.  
  47. int main(){
  48. init();
  49. floyd();
  50. print();
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement