Knobody

Untitled

Jun 30th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define dbg(i,j) cout<<"I am "<<i<<" = "<<endl<<j<<endl;
  3. #define dbr(name,a) cout<<name<<endl;for(auto x:a)cout<<x<<" ";cout<<endl;
  4. #define DBR(name,a) cout<<name<<endl;for(auto x:a){ for(auto y:x)cout<<y<<" ";cout<<endl;}
  5. #define dbmp(name,a) cout<<name<<endl;for(auto x:a){ cout<<x.first<<"\t"<<x.second<<endl;}
  6. #define dbp(name,a) cout<<name<<endl;cout<<a.first<<"\t"<<a.second<<endl;
  7. #define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  8. #define INF  1000005
  9. using namespace std;
  10.  
  11. typedef long long int big;
  12.  
  13. typedef  long double fig;
  14.  
  15. int n=0;
  16. int dist[50][50];
  17. int graph[50][50];
  18.  
  19. void floydWarshall ()  
  20. {  
  21.     //int dist[n][n];
  22.     /* dist[][] will be the output matrix  
  23.     that will finally have the shortest  
  24.     distances between every pair of vertices */
  25.     int  i, j, k;  
  26.  
  27.     /* Initialize the solution matrix same  
  28.     as input graph matrix. Or we can say  
  29.     the initial values of shortest distances
  30.     are based on shortest paths considering  
  31.     no intermediate vertex. */
  32.     for (i = 0; i < n; i++)  
  33.         for (j = 0; j < n; j++)  
  34.             dist[i][j] = graph[i][j];  
  35.  
  36.     /* Add all vertices one by one to  
  37.     the set of intermediate vertices.  
  38.     ---> Before start of an iteration,  
  39.     we have shortest distances between all  
  40.     pairs of vertices such that the  
  41.     shortest distances consider only the  
  42.     vertices in set {0, 1, 2, .. k-1} as
  43.     intermediate vertices.  
  44.     ----> After the end of an iteration,  
  45.     vertex no. k is added to the set of  
  46.     intermediate vertices and the set becomes {0, 1, 2, .. k} */
  47.     for (k = 0; k < n; k++)  
  48.     {  
  49.         // Pick all vertices as source one by one  
  50.         for (i = 0; i < n; i++)  
  51.         {  
  52.             // Pick all vertices as destination for the  
  53.             // above picked source  
  54.             for (j = 0; j < n; j++)  
  55.             {  
  56.                 // If vertex k is on the shortest path from  
  57.                 // i to j, then update the value of dist[i][j]  
  58.                 if (dist[i][k] + dist[k][j] < dist[i][j])  
  59.                     dist[i][j] = dist[i][k] + dist[k][j];  
  60.             }  
  61.         }  
  62.     }  
  63.  
  64.     // Print the shortest distance matrix  
  65.     //printSolution(dist);  
  66. }
  67.  
  68. int main(){
  69.     freopen("i1.txt","rt",stdin);
  70.     freopen("o1.txt","wt",stdout);
  71.     int t;
  72.     cin>>t;
  73.     int test=1;
  74.     while(t--){
  75.         int m;
  76.         cin>>n>>m;
  77.         //int arr[n][n];
  78.         for(big i=0;i<n;i++){
  79.             for(big j=0;j<n;j++){
  80.                 graph[i][j]=INF;
  81.             }
  82.         }
  83.         for(big i=0;i<m;i++){
  84.             int x,y,z;
  85.             cin>>x>>y>>z;
  86.             x--;
  87.             y--;
  88.             graph[x][y]=z;
  89.             graph[y][x]=z;
  90.         }
  91.        
  92.         //int dist[n][n];
  93.         floydWarshall();
  94.         cout<<"Case #"<<test<<": ";
  95.         /*for(int i=0;i<n;i++){
  96.             for(int j=0;j<n;j++){
  97.                 cout<<dist[i][j]<<" ";
  98.             }
  99.             cout<<"\n";
  100.         }*/
  101.         int flag=0;
  102.         int edges=0;
  103.         for(big i=0;i<n;i++){
  104.             for(big j=i+1;j<n;j++){
  105.                 if(graph[i][j]!=INF){
  106.                     if(graph[i][j]!=dist[i][j]){
  107.                         cout<<"Impossible"<<endl;
  108.                         flag=1;
  109.                     }
  110.                     else{
  111.                         edges++;
  112.                     }
  113.                 }
  114.             }
  115.         }
  116.         if(!flag){
  117.             cout<<edges<<"\n";
  118.             for(big i=0;i<n;i++){
  119.                 for(big j=i+1;j<n;j++){
  120.                     if(graph[i][j]!=INF){
  121.                         cout<<i+1<<" "<<j+1<<" "<<graph[i][j]<<"\n";
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.         test++;
  127.     }
  128.  
  129.     return 0;
  130. }
Add Comment
Please, Sign In to add comment