Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- class colonysets
- {
- public:
- struct coordinates
- {
- int x,y;
- int index;
- };
- class adjacency
- {
- public:
- float distance;
- bool traversed;
- };
- coordinates colonies[10]; //Max Number of Colonies would be 10 according to Question
- float length[10];
- int path[10];
- adjacency adjMat[10][10];
- int correctPath[10];
- float leastLength;
- int n;
- colonysets(int tn) //Constructor
- {
- n=tn;
- for(int i=0;i<n;i++)
- length[i]=0;
- /****Receiving Inputs*****/
- for(int i=0;i<n;i++)
- {
- cin>>colonies[i].x;
- cin>>colonies[i].y;
- colonies[i].index=i;
- }
- /*****ADjacency Matrix Initialization*****/
- for(int i=0;i<n;i++)
- for(int j=0;j<n;j++)
- {
- if(i==j)
- adjMat[i][j].distance=0;
- else
- {
- float distance;
- float temp;
- temp=((colonies[i].x-colonies[j].x)*(colonies[i].x-colonies[j].x))+((colonies[i].y-colonies[j].y)*(colonies[i].y-colonies[j].y));
- distance=sqrt(temp);
- adjMat[i][j].distance=distance;
- }
- }
- /** ADJ MAT PRINT**/
- /*for(int i=0; i<n;i++)
- {
- cout<<endl;
- for(int j=0;j<n;j++)
- cout<<"\t"<<adjMat[i][j].distance;
- }*/
- /*****Distance Calculation For all possible Paths****/
- for(int i=0;i<n;i++)
- {
- for(int l=0;l<n;l++)
- for(int m=0;m<n;m++)
- {
- path[l]=-1;
- adjMat[l][m].traversed=false;
- }
- for(int d=0;d<n;d++)
- adjMat[d][i].traversed=true;
- path[0]=i;
- int k=1;
- int count=0;
- for(int j=path[0];count<n;j=path[k-1],count++)
- {
- float templength=32767;
- for(int c=0;c<n;c++)
- {
- if((adjMat[j][c].traversed==false)&&(adjMat[j][c].distance<templength)&&(adjMat[j][c].distance!=0))
- {
- templength=adjMat[j][c].distance;
- path[k]=c;
- }
- }
- for(int a=0;a<n;a++)
- adjMat[a][path[k]].traversed=true;
- //cout<<endl<<path[k-1];
- k++;
- if(templength!=32767)
- length[i]+=templength;
- }
- // cout<<endl<<length[i];
- }
- /*****Least Distance Calculation *****/
- for(int i=0;i<n-1;i++)
- for(int j=i+1;j<n;j++)
- if(length[i]>length[j])
- {
- float temp=length[i];
- length[i]=length[j];
- length[j]=temp;
- }
- leastLength=length[0];
- /*****Detection of Path with Least Distance*****/
- for(int i=0;i<n;i++)
- length[i]=0;
- for(int i=0;i<n;i++)
- {
- for(int l=0;l<n;l++)
- for(int m=0;m<n;m++)
- {
- path[l]=-1;
- adjMat[l][m].traversed=false;
- }
- for(int d=0;d<n;d++)
- adjMat[d][i].traversed=true;
- path[0]=i;
- int k=1;
- int count=0;
- for(int j=path[0];count<n;j=path[k-1],count++)
- {
- float templength=32767;
- for(int c=0;c<n;c++)
- {
- if((adjMat[j][c].traversed==false)&&(adjMat[j][c].distance<templength)&&(adjMat[j][c].distance!=0))
- {
- templength=adjMat[j][c].distance;
- path[k]=c;
- }
- }
- for(int a=0;a<n;a++)
- adjMat[a][path[k]].traversed=true;
- k++;
- if(templength!=32767)
- length[i]+=templength;
- }
- if(length[i]==leastLength)
- {
- for(int y=0;y<n;y++)
- correctPath[y]=path[y];
- goto terminate;
- }
- }
- terminate:
- int dummy;
- }
- //Distance Calculator
- float distance_calc(int x1,int y1,int x2,int y2)
- {
- float distance;
- float temp;
- temp=((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2));
- distance=sqrt(temp);
- return distance;
- }
- void output()
- {
- /*for(int i=0;i<n;i++)
- cout<<"\n=>"<<correctPath[i]; */
- for(int i=0;i<n-1;i++)
- {
- cout<<"\nTUNNEL FROM ("<<colonies[correctPath[i]].x<<","<<colonies[correctPath[i]].y<<") TO ("<<colonies[correctPath[i+1]].x<<","<<colonies[correctPath[i+1]].y<<") is ";
- cout.precision(2);
- cout.setf(ios::fixed,ios::floatfield);
- cout<<distance_calc(colonies[correctPath[i]].x,colonies[correctPath[i]].y,colonies[correctPath[i+1]].x,colonies[correctPath[i+1]].y)<<" m";
- }
- cout<<"\nTOTAL LENGTH OF TUNNEL is "<<leastLength<<" m";
- }
- colonysets(){}
- };
- int main()
- {
- colonysets colonyset[500]; //Assuming Number of Data Sets Would be <500. Hope so!
- int setcount=0;
- int n;
- cin>>n;
- while(n!=0)
- {
- colonyset[setcount]=colonysets(n);
- setcount++;
- cin>>n;
- }
- cout<<"OUTPUT IS";
- for(int i=0;i<setcount;i++)
- {
- cout<<"\nSET "<<i+1;
- colonyset[i].output();
- }
- // system("PAUSE"); USE IF YOU NEED TO LOOK AT OUTPUT. Equivalent to getch().
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement