Advertisement
Guest User

dsadas

a guest
Jul 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. long long n,t,m,a,b,c,tab[255][255],inf=1000000007;
  6. int main()
  7. {
  8. cin>>t;
  9.  
  10. for(int j=0;t>j;j++)
  11. {
  12.  
  13. cin>>n;
  14. cin>>m;
  15.     for(int x=1;n>=x;x++)
  16.     {
  17.         for(int y=1;n>=y;y++)
  18.         {
  19.             if(x!=y)
  20.                 tab[x][y]=inf;
  21.             else
  22.                 tab[x][y]=0;
  23.         }
  24.     }
  25. for(int i=0;m>i;i++)
  26. {
  27.     cin>>a;
  28.     cin>>b;
  29.     cin>>c;
  30.     tab[a][b]=min(tab[a][b],c);
  31.     tab[b][a]=min(tab[b][a],c);
  32. }
  33. for(int f=1;n>=f;f++)
  34. for(int g=1;n>=g;g++)
  35. for(int h=1;n>=h;h++)
  36. {
  37.     if(tab[g][h]>tab[g][f]+tab[f][h])
  38.     {
  39.         tab[g][h]=tab[g][f]+tab[f][h];
  40.         tab[h][g]=tab[g][f]+tab[f][h];
  41.     }
  42. }
  43. for(int i=1;n>=i;i++)
  44. {
  45.     for(int j=1;n>=j;j++)
  46.     {
  47.         if(tab[i][j]!=inf)
  48.         cout<<tab[i][j]<<" ";
  49.         else
  50.             cout<<"-1"<<" ";
  51.     }
  52.     cout<<endl;
  53. }
  54. }
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement