rootUser

http://imgur.com/Olb579C

Aug 28th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. /*
  2.  
  3. Problem :
  4. http://imgur.com/Olb579C
  5. or
  6. https://i.imgsafe.org/34372dd270.jpg
  7.  
  8. Input:
  9. 6
  10. 12
  11. 1 2
  12. 0
  13. 2 1
  14. 5
  15. 2 3
  16. 10
  17. 3 2
  18. 0
  19. 3 4
  20. 0
  21. 4 3
  22. 15
  23. 4 5
  24. 0
  25. 5 4
  26. 25
  27. 5 6
  28. 5
  29. 6 5
  30. 0
  31. 6 1
  32. 0
  33. 1 6
  34. 10
  35.  
  36.  
  37. */
  38.  
  39.  
  40.  
  41.  
  42. #include<iostream>
  43. using namespace std;
  44.  
  45. int vertex,edge,source;
  46. int adjacency_matrix[100][100],weight_matrix[100][100];
  47. int parent[100],cost[100],visited[100];
  48.  
  49. void inputGraph();
  50. int rotateClockWise();
  51. int rotateAntiClockWise();
  52.  
  53. int main(void)
  54. {
  55.     inputGraph();
  56.     int i=rotateClockWise(),j=rotateAntiClockWise();
  57.     if(i<j)
  58.     {
  59.         cout<<endl<<"Clockwise rotation cost "<<i<<" is lesser than Anti clockwise rotation cost "<<j<<endl;
  60.     }
  61.     else
  62.     {
  63.         cout<<endl<<"Anti Clockwise rotation cost "<<j<<" is lesser than clock wise rotation cost "<<i<<endl;
  64.     }
  65.     return 0;
  66. }
  67. void inputGraph()
  68. {
  69.     cout<<"Total vertex : ";
  70.     cin>>vertex;
  71.     cout<<"Total edge   : ";
  72.     cin>>edge;
  73.     int i,j;
  74.     //make all weight infinity
  75.     for(i=1; i<=vertex; i++)
  76.     {
  77.         for(j=1; j<=vertex; j++)
  78.         {
  79.             weight_matrix[i][j]=999999;
  80.         }
  81.     }
  82.     // scan adjacency matrix and weight
  83.     for(i=1; i<=edge; i++)
  84.     {
  85.         int start,finish;
  86.         cout<<"Enter start and end node for edge "<<i<<" : ";
  87.         cin>>start;
  88.         cin>>finish;
  89.         adjacency_matrix[start][finish]=1;
  90.         cout<<"Enter weight for edge "<<i<<" : ";
  91.         cin>>weight_matrix[start][finish];
  92.     }
  93.     cout<<"The adjacency matrix is : "<<endl;
  94.     for(i=1; i<=vertex; i++)
  95.     {
  96.         for(j=1; j<=vertex; j++)
  97.         {
  98.             cout<<adjacency_matrix[i][j]<<" ";
  99.         }
  100.         cout<<endl;
  101.     }
  102.     cout<<"The weight matrix is : "<<endl;
  103.     for(i=1; i<=vertex; i++)
  104.     {
  105.         for(j=1; j<=vertex; j++)
  106.         {
  107.             cout<<weight_matrix[i][j]<<"\t";
  108.         }
  109.         cout<<endl;
  110.     }
  111. }
  112. int rotateClockWise()
  113. {
  114.     int start=1,finish=vertex,sum=0;
  115.     int i;
  116.     for(i=1;i<=vertex;i++)
  117.     {
  118.         if(i==vertex)
  119.         {
  120.             sum=sum+weight_matrix[finish][start];
  121.         }
  122.         else
  123.         {
  124.             sum=sum+weight_matrix[i][i+1];
  125.         }
  126.     }
  127.     cout<<endl<<"Clockwise rotation cost : "<<sum<<endl;
  128.     return sum;
  129. }
  130. int rotateAntiClockWise()
  131. {
  132.     int start=1,finish=vertex,sum=0;
  133.     int i;
  134.     for(i=vertex;i>=1;i--)
  135.     {
  136.         if(i==vertex)
  137.         {
  138.             sum=sum+weight_matrix[start][finish];
  139.         }
  140.         else
  141.         {
  142.             sum=sum+weight_matrix[i][i-1];
  143.         }
  144.     }
  145.     cout<<endl<<"Anti-Clockwise rotation cost : "<<sum<<endl;
  146.     return sum;
  147. }
Add Comment
Please, Sign In to add comment