Facebook
From Filip, 4 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 231
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7.  
  8. float miasta[130][3];
  9. float odleglosc[130][130];
  10. float c = 1000.0;
  11.  
  12.  
  13. int x;
  14. int y;
  15.  
  16.  
  17. void najblizsze_miasto()
  18. {
  19.     for(int j = 0; j < 130; j++)
  20.     {
  21.         if(odleglosc[x][j] != 0 && miasta[j][2] == 0 && odleglosc[x][j] < c)
  22.         {
  23.             c = odleglosc[x][j];
  24.             y = j;
  25.         }
  26.     }
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32.     float wspx, wspy, a, cos;
  33.     float suma = 0.0;
  34.  
  35.  
  36.     ifstream plik;
  37.     plik.open("ch130.tsp");
  38.     plik >> cos;
  39.  
  40.  
  41.     int i = 0;
  42.  
  43.  
  44.     while( !plik.eof() )
  45.     {
  46.         plik >> a >> wspx >> wspy;
  47.  
  48.         miasta[i][0] = wspx;
  49.         miasta[i][1] = wspy;
  50.         miasta[i][2] = 0;
  51.  
  52.         i++;
  53.     }
  54.  
  55.     for(int i = 0; i < 130; i++)
  56.         for(int j = 0; j < 130; j++)
  57.             odleglosc[i][j] = sqrt(pow(miasta[j][0] - miasta[i][0], 2) + pow(miasta[j][1] - miasta[i][1], 2));
  58.  
  59.  
  60.     for(int i = 0; i < 130; i++)
  61.     {
  62.         miasta[i][2] = 1;
  63.  
  64.         x = i;
  65.  
  66.         for(int n = 0; n < 129; n++)
  67.         {
  68.  
  69.             najblizsze_miasto();
  70.  
  71.  
  72.             suma += odleglosc[x][y];
  73.  
  74.             x = y;
  75.  
  76.             miasta[y][2] = 1;
  77.  
  78.           /*  for(int p = 0; p < 130; p++)
  79.                 if(miasta[p][2] == 0)
  80.                     y = p;
  81.             */
  82.         }
  83.  
  84.         suma += odleglosc[x][i];
  85.  
  86.  
  87.         for(int c = 0; c < 130; c++) miasta[c][2] = 0;
  88.  
  89.  
  90.         cout << suma << endl;
  91.         suma = 0;
  92.     }
  93. }
  94.