Advertisement
naimularif

Travelling salesman exponential

May 22nd, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #include<iostream>
  2. #include<time.h>
  3. #include<cstdlib>
  4. #include<vector>
  5. #include<fstream>
  6. #define infinity 10000
  7. #define node int
  8. using namespace std;
  9.  
  10. class path
  11. {
  12.     public:
  13.     int path[1000];
  14.     int length;
  15. };
  16.  
  17. int mat[1000][1000];
  18. int n;
  19. int cost=infinity;
  20. vector<node> optimal;
  21.  
  22. void recur(int n, vector<node> v);
  23.  
  24. int main(void)
  25. {
  26.     ifstream myfile2;
  27.     myfile2.open("input7.txt");
  28.     cout<<"Input the size of graph: "<<endl;
  29.     myfile2>>n;
  30. //
  31. //    ofstream myfile;
  32. //    myfile.open("input.txt");
  33.  
  34. //    srand (time(NULL));
  35. //
  36. //    for(int i=0;i<n;i++)
  37. //    {
  38. //        for(int j=0;j<=i;j++)
  39. //        {
  40. //            if(i==j)
  41. //            {
  42. //                mat[i][j]=0;
  43. //                continue;
  44. //            }
  45. //
  46. //            int yes;
  47. //            yes=rand()%8;
  48. //
  49. //            if(yes>=3)
  50. //            {
  51. //                mat[i][j]=rand()%15+1;
  52. //                mat[j][i]=mat[i][j];
  53. //            }
  54. //            else
  55. //            {
  56. //                mat[i][j]=infinity;
  57. //                mat[j][i]=mat[i][j];
  58. //            }
  59. //        }
  60. //    }
  61.     for(int i=0;i<n;i++)
  62.     {
  63.         for(int j=0;j<n;j++)
  64.         {
  65.             myfile2>>mat[i][j];
  66.         }
  67.     }
  68.  
  69.     for(int i=0;i<n;i++)
  70.     {
  71.         for(int j=0;j<n;j++)
  72.         {
  73.             cout<<mat[i][j]<<'\t';
  74.         }
  75.         cout<<endl;
  76.     }
  77.  
  78. //    path p;
  79. //    p.length=0;
  80.  
  81.     vector<node> v;
  82.     v.push_back(0);
  83.  
  84.     int nod;
  85.     nod=0;
  86.  
  87.     recur(nod,v);
  88.  
  89.     cout<<"cost: "<<cost<<endl;
  90.     for(int i=0;i<optimal.size();i++)
  91.     {
  92.         cout<<optimal[i]<<' ';
  93.     }
  94. }
  95.  
  96. void recur(int nod, vector<node> v)
  97. {
  98.     int localcost;
  99.     int flag=0;
  100.     for(int i=0;i<n;i++)
  101.     {
  102.         localcost=0;
  103.         //cout<<"mat nod i = "<<mat[nod][i]<<" i "<<i<<" nod "<<nod<<endl;
  104.         //if(i!=nod && mat[nod][i]!=infinity)
  105.         if(mat[nod][i]<1000 && i!=nod)
  106.         {//cout<<"entered"<<endl;
  107.             for(int j=0;j<v.size();j++)
  108.             {
  109.                 if(v[j]==i)
  110.                 {
  111.                     flag=1;
  112.                     break;
  113.                 }
  114.             }
  115.             if(flag==0)
  116.             {
  117.                 //localcost+=mat[nod][i];
  118.                 v.push_back(i);
  119.                 if(i==n-1)
  120.                 {
  121.                     localcost=0;
  122.                     cout<<"done"<<endl;
  123.                     //cout<<"cost "<<localcost<<endl;
  124.                     for(int k=0;k<v.size();k++)
  125.                     {
  126.                         cout<<v[k]<<"    ";
  127.                         if(k>0)
  128.                         {
  129.                             localcost+=mat[v[k-1]][v[k]];
  130.                         }
  131.                     }
  132.                     cout<<"cost "<<localcost<<endl;
  133.                     if(cost>localcost)
  134.                     {
  135.                         cost=localcost;
  136.                         while(optimal.size())
  137.                         {
  138.                             optimal.pop_back();
  139.                         }
  140.                         for(int k=0;k<v.size();k++)
  141.                         {
  142.                             optimal.push_back(v[k]);
  143.                         }
  144.                     }
  145.                     return;
  146.                 }
  147.                 else
  148.                 {
  149.                     for(int l=0;l<v.size();l++)
  150.                     {
  151.                         cout<<v[l]<<' ';
  152.                     }cout<<endl;
  153.                     //cout<<"recursion "<<i<<"\n";
  154.                     recur(i,v);
  155.                 }
  156.                 v.pop_back();
  157.             }
  158.  
  159.             flag=0;
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement