Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Problem :
- http://imgur.com/Olb579C
- or
- https://i.imgsafe.org/34372dd270.jpg
- Input:
- 6
- 12
- 1 2
- 0
- 2 1
- 5
- 2 3
- 10
- 3 2
- 0
- 3 4
- 0
- 4 3
- 15
- 4 5
- 0
- 5 4
- 25
- 5 6
- 5
- 6 5
- 0
- 6 1
- 0
- 1 6
- 10
- */
- #include<iostream>
- using namespace std;
- int vertex,edge,source;
- int adjacency_matrix[100][100],weight_matrix[100][100];
- int parent[100],cost[100],visited[100];
- void inputGraph();
- int rotateClockWise();
- int rotateAntiClockWise();
- int main(void)
- {
- inputGraph();
- int i=rotateClockWise(),j=rotateAntiClockWise();
- if(i<j)
- {
- cout<<endl<<"Clockwise rotation cost "<<i<<" is lesser than Anti clockwise rotation cost "<<j<<endl;
- }
- else
- {
- cout<<endl<<"Anti Clockwise rotation cost "<<j<<" is lesser than clock wise rotation cost "<<i<<endl;
- }
- return 0;
- }
- void inputGraph()
- {
- cout<<"Total vertex : ";
- cin>>vertex;
- cout<<"Total edge : ";
- cin>>edge;
- int i,j;
- //make all weight infinity
- for(i=1; i<=vertex; i++)
- {
- for(j=1; j<=vertex; j++)
- {
- weight_matrix[i][j]=999999;
- }
- }
- // scan adjacency matrix and weight
- for(i=1; i<=edge; i++)
- {
- int start,finish;
- cout<<"Enter start and end node for edge "<<i<<" : ";
- cin>>start;
- cin>>finish;
- adjacency_matrix[start][finish]=1;
- cout<<"Enter weight for edge "<<i<<" : ";
- cin>>weight_matrix[start][finish];
- }
- cout<<"The adjacency matrix is : "<<endl;
- for(i=1; i<=vertex; i++)
- {
- for(j=1; j<=vertex; j++)
- {
- cout<<adjacency_matrix[i][j]<<" ";
- }
- cout<<endl;
- }
- cout<<"The weight matrix is : "<<endl;
- for(i=1; i<=vertex; i++)
- {
- for(j=1; j<=vertex; j++)
- {
- cout<<weight_matrix[i][j]<<"\t";
- }
- cout<<endl;
- }
- }
- int rotateClockWise()
- {
- int start=1,finish=vertex,sum=0;
- int i;
- for(i=1;i<=vertex;i++)
- {
- if(i==vertex)
- {
- sum=sum+weight_matrix[finish][start];
- }
- else
- {
- sum=sum+weight_matrix[i][i+1];
- }
- }
- cout<<endl<<"Clockwise rotation cost : "<<sum<<endl;
- return sum;
- }
- int rotateAntiClockWise()
- {
- int start=1,finish=vertex,sum=0;
- int i;
- for(i=vertex;i>=1;i--)
- {
- if(i==vertex)
- {
- sum=sum+weight_matrix[start][finish];
- }
- else
- {
- sum=sum+weight_matrix[i][i-1];
- }
- }
- cout<<endl<<"Anti-Clockwise rotation cost : "<<sum<<endl;
- return sum;
- }
Add Comment
Please, Sign In to add comment